Zmień parametr niestandardowego atrybutu w czasie wykonywania

Potrzebuję zmienić parametry atrybutu w czasie wykonywania. Uprościłem mój problem do prostego przykładu.

Klasa atrybutów:

<code>    [AttributeUsage(AttributeTargets.Property)]
    public class MyAttribute : Attribute
    {
        public string Name { get; set; }
    }
</code>

Prosty element, który ozdobił właściwości atrybutami:

<code>    public class MyEntity
    {
        [MyAttribute(Name="OldValue1")]
        public string Data1{ get; set; }

        [MyAttribute(Name = "OldValue2")]
        public string Data2 { get; set; }
    }
</code>

Stworzyłem instancję klasy MyEntity. Mogę zmienić wartość właściwości obiektu, ale nie mogę zmienić wartości właściwości atrybutu Nazwa na obiekcie. Czy to możliwe?

Wartość właściwości na obiekcie obiektu, którą mogę zmienić za pomocą tej części kodu:

<code>                entityProp.SetValue(entity,"NewData",null);
</code>

ale nie zmieniam wartości właściwości atrybutu Nazwa na obiekcie

To nie działa:

<code>attProp.SetValue(attribute,"NewData",null);
</code>

Wartość oferty Nazwa jest nadal oryginalna.

Oto cały kod testowy. Dziękuję za piekło.

<code>    [TestMethod]
    public  void Test()
    {
        var entity = new MyEntity
                         {
                             Data1 = "OldData",
                             Data2 = "OldData"
                         };

        PropertyInfo[] entityProps = entity.GetType().GetProperties();

        foreach (var entityProp in entityProps)
        {
            var attribute = Attribute.GetCustomAttribute(entityProp, typeof (MyAttribute)) as MyAttribute;

            if (attribute != null)
            {
                //get attribute’s property NAME
                PropertyInfo attProp= attribute.GetType().GetProperty("Name");

                //get entity property value
                var propertyValue = entityProp.GetValue(entity, null);

                //get attribute’s property NAME value
                var atributeNameValue = attProp.GetValue(entity, null);

                TestContext.WriteLine(string.Format("property name:{0} property value: {1} : atribute name value: {2}\n", 
                    entityProp.Name, propertyValue, atributeNameValue)); 

                //change values
                entityProp.SetValue(entity,"NewData",null);

                //how can I change value of property Name on object entity ?
                attProp.SetValue(attribute,"NewData",null);

            }

        }

        TestContext.WriteLine(string.Format("After change\n"));

        foreach (var entityProp in entityProps)
        {
            var attribute = Attribute.GetCustomAttribute(entityProp, typeof(MyAttribute)) as MyAttribute;

            if (attribute != null)
            {

                PropertyInfo attProp = attribute.GetType().GetProperty("Name");

                var propertyValue = entityProp.GetValue(entity, null);
                var atributeNameValue = attProp.GetValue(entity, null);

                TestContext.WriteLine(string.Format("property name:{0} property value: {1} : atribute name value: {2}\n",
                    entityProp.Name, propertyValue, atributeNameValue));
            }
        }



    }
</code>

EDYCJA: Usuwam oryginalny post i dodałem bardzo prostą, przejrzystą próbkę. Przepraszam

questionAnswers(2)

yourAnswerToTheQuestion