c# - 在运行时更改自定义属性的参数

标签 c# reflection runtime custom-attributes

我需要在运行时更改属性的参数。我将问题简化为简单示例。

属性类:

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

简单的实体,用属性装饰属性:

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

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

我创建了 MyEntity 类的实例。我可以更改对象属性的值,但不能更改对象实体上属性的属性 Name 的值。可能吗?

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

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

但我不知道如何更改对象实体上属性的属性名称的值

这不起作用:

attProp.SetValue(attribute,"NewData",null);

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

这里是所有的测试代码。

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

已编辑:我删除了原始帖子并添加了非常简单清晰的示例。抱歉

最佳答案

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

关于c# - 在运行时更改自定义属性的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10046601/

相关文章:

c# - 如何在代码中设置useUnsafeHeaderParsing

c# - 使用 LINQ 仅基于单个对象属性获取两个对象列表之间的差异

scala - 如何使用 Scala-Macros 获取构造函数参数名称

c# - 如何使用 lambda 表达式将属性列表传递给方法

build - InstallShield 运行时?

c# - 展平 Lambda 表达式以访问集合属性成员名称

c# - 在 C# 中处理丢弃的 TCP 数据包

c# - typeof(ICollection<>).GetTypeInfo().IsAssignableFrom(typeof(IList<>))

java - 如何在运行时加载接口(interface)的实现并调用该类的方法?

android - Alertdialog 不起作用,它在 Android 中产生异常