本文介绍了在运行时更改自定义属性的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在运行时改变属性的paramater。我简化了我的问题简单的例子



属性类:



  [AttributeUsage(AttributeTargets.Property)
公共类MyAttribute:属性
{
公共字符串名称{;组; }
}



简单的实体至极拥有装饰性与属性:

 公共类myEntity所
{
[MyAttribute(NAME =OldValue1)]
酒店的公共字符串数据1 {搞定;组; }

[MyAttribute(NAME =OldValue2)]
公共字符串数据2 {搞定;组; }
}



我创建的类myEntity所的instace。我可以改变对象的属性值,但我不能对对象实体改变属性的属性名称的值。 ?是否有可能



值对对象的实体属性我可以用这部分代码更改:

  entityProp.SetValue(实体,NewData,NULL); 



但我也不怎么上的对象实体属性的属性名称的变化值



这不工作:



Value of propery Name is still original.

Here is all test code. Thank you for hellp.

    [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));
            }
        }



    }

EDITED: I delete original post and added very simple clear sample. Sorry

解决方案

You cannot change attributes at runtime. They are embedded into the metadata of the assembly. Your method is changing the internal state of a particular instance; but when you load the attribute again, you are getting a different instance.

这篇关于在运行时更改自定义属性的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 19:18
查看更多