在运行时更改自定义属性的参数 [英] Change custom attribute's parameter at runtime

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

问题描述

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



属性类:



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



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

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

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



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



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

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



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



这不工作:




  attProp.SetValue(属性, NewData,NULL); 




属性格式名称的值仍然是原来的。



下面是所有测试代码。感谢您的HELLP。

  [TestMethod的] 
公共无效测试()
{
变种实体=新myEntity所
{
数据1 =OLDDATA,
数据2 =OLDDATA
};

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

的foreach(在entityProps VAR entityProp)
{
var属性= Attribute.GetCustomAttribute(entityProp的typeof(MyAttribute))作为MyAttribute;

如果(属性!= NULL)
{
//获得属性的属性NAME
的PropertyInfo attProp = attribute.GetType()的getProperty(姓名);

//获取实体属性值
变种为PropertyValue = entityProp.GetValue(实体,NULL);

//获得属性的属性名值
VAR atributeNameValue = attProp.GetValue(实体,NULL);

TestContext.WriteLine(的String.Format(属性名:{0}属性值:{1}:属性附加伤害名称值:{2} \\\

entityProp.Name,为PropertyValue,atributeNameValue));

//变化值
entityProp.SetValue(实体,NewData,NULL);

//我怎样才能改变对象实体属性名称的价值?
attProp.SetValue(属性NewData,NULL);

}

}

TestContext.WriteLine(的String.Format(后change\\\
));

的foreach(在entityProps VAR entityProp)
{
var属性= Attribute.GetCustomAttribute(entityProp的typeof(MyAttribute))作为MyAttribute;

如果(属性!= NULL)
{

的PropertyInfo attProp = attribute.GetType()的getProperty(姓名);

变种为PropertyValue = entityProp.GetValue(实体,NULL);
VAR atributeNameValue = attProp.GetValue(实体,NULL);

TestContext.WriteLine(的String.Format(属性名:{0}属性值:{1}:属性附加伤害名称值:{2} \\\

entityProp.Name,为PropertyValue,atributeNameValue));
}
}



}

编辑:我删除了原来的职位,并添加非常简单明确的样本。对不起


解决方案

您不能在运行时更改属性。它们被嵌入到程序集的元数据。你的方法是改变一个特定实例的内部状态;但是当你再次加载属性,你得到一个不同的实例。


I need change attribute’s paramater during runtime. I simplified my problem to simple example.

Attribute class:

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

Simple entity wich has decorated properties with attributes:

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

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

I created instace of class MyEntity. I can change value of object’s properties, but I can’t change value of attribute’s property Name on object entity. Is it possible?

Value of property on object entity I can change with this part of code:

                entityProp.SetValue(entity,"NewData",null);

but I don’t how change value of attribute’s property Name on object entity

This not work:

attProp.SetValue(attribute,"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.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆