C# 动态为类的属性添加或修改其特性值

一、简述

  在做项目的过程中要用到 WindowsForm PropertyGrid 控件,不过控件显示出来的属性是英文,想要显示出来的是中文,那么在类的属性上面加上一个 DisplayName 特性就行了。但是,因为某种情况要动态的修改控件显示出来的中文,怎么办?

二、内容

  首先先编写一个实验类

复制代码

 public class AppSetings
    {
        private string textID;
        private string textName;
        private Size textSize;

        [DisplayName("文本ID")]
        public string TextID
        {
            get { return textID; }
            set { textID = value; }
        }

        public string TextName
        {
            get { return textName; }
            set { textName = value; }
        }

        public Size TextSize
        {
            get { return textSize; }
            set { textSize = value; }
        }

    }

复制代码

  这里显示为

  接下修改 文本ID 这个属性显示,代码

复制代码

private void Form1_Load(object sender, EventArgs e)
        {
            AppSetings appSeting = new AppSetings();
            PropertyDescriptorCollection appSetingAttributes = TypeDescriptor.GetProperties(appSeting);
            Type displayType = typeof(DisplayNameAttribute);
            FieldInfo fieldInfo = displayType.GetField("_displayName", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.CreateInstance);
            fieldInfo.SetValue(appSetingAttributes["TextID"].Attributes[displayType], "修改后的文本ID");

            propertyGrid1.SelectedObject = appSeting;
        }

复制代码

  上面就是利用反射来修改 DisplayName 特性的值来达到修改属性显示的目的,不过这里有个前提条件,条件就是类的属性上面要提前加上 DisplayName 这个特性

  那么要想在原本没有提前加上 DisplayName 这个特性的类的属性中动态加上 DisplayName 特性,就要换一种写法,可以这么写

复制代码

 private void Form1_Load(object sender, EventArgs e)
        {
            AppSetings appSeting = new AppSetings();
            PropertyDescriptorCollection appSetingAttributes = TypeDescriptor.GetProperties(appSeting);
            Type displayType = typeof(DisplayNameAttribute);
            FieldInfo fieldInfo = displayType.GetField("_displayName", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.CreateInstance);
            fieldInfo.SetValue(appSetingAttributes["TextID"].Attributes[displayType], "修改后的文本ID");


            Type memberDescriptorType = typeof(MemberDescriptor);
            FieldInfo memberDescriptorFieldInfo = memberDescriptorType.GetField("displayName", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.CreateInstance);
            memberDescriptorFieldInfo.SetValue(appSetingAttributes["TextName"], "文本名称");  // TextName属性没加 DisplayName 特性

            propertyGrid1.SelectedObject = appSeting;
        }
    }

复制代码

 

  这时候 TextName 属性便显示为中文 文本名称。最后根据这个思路,就动手改变 TextSize 下面的 Width 与 Height 。要知道这 Width 与 height 可是属于 Size 类下面的属性

复制代码

private void Form1_Load(object sender, EventArgs e)
        {
            AppSetings appSeting = new AppSetings();
            PropertyDescriptorCollection appSetingAttributes = TypeDescriptor.GetProperties(appSeting);
            Type displayType = typeof(DisplayNameAttribute);
            FieldInfo fieldInfo = displayType.GetField("_displayName", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.CreateInstance);
            fieldInfo.SetValue(appSetingAttributes["TextID"].Attributes[displayType], "修改后的文本ID");


            Type memberDescriptorType = typeof(MemberDescriptor);
            FieldInfo memberDescriptorFieldInfo = memberDescriptorType.GetField("displayName", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.CreateInstance);
            memberDescriptorFieldInfo.SetValue(appSetingAttributes["TextName"], "文本名称");  // TextName属性没加 DisplayName 特性

            PropertyDescriptorCollection sizeAttributes = TypeDescriptor.GetProperties(appSeting.TextSize);
            memberDescriptorFieldInfo.SetValue(sizeAttributes["Width"], "宽度");
            memberDescriptorFieldInfo.SetValue(sizeAttributes["Height"], "高度");

            propertyGrid1.SelectedObject = appSeting;
        }

复制代码

  以上就这么完成。

  • 2
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
可以使用反射来动态修改枚举类型中字段的特性。下面是一个示例代码: ```csharp using System; using System.Reflection; public enum MyEnum { [MyAttribute("Value 1")] Value1, [MyAttribute("Value 2")] Value2, [MyAttribute("Value 3")] Value3 } public class MyAttribute : Attribute { public string Description { get; set; } public MyAttribute(string description) { Description = description; } } class Program { static void Main() { Type enumType = typeof(MyEnum); FieldInfo[] fields = enumType.GetFields(BindingFlags.Public | BindingFlags.Static); foreach (FieldInfo field in fields) { MyAttribute attribute = field.GetCustomAttribute<MyAttribute>(); if (attribute != null) { Console.WriteLine($"Original description for {field.Name}: {attribute.Description}"); // 修改特性 attribute.Description = "New description"; Console.WriteLine($"Modified description for {field.Name}: {attribute.Description}"); } } } } ``` 这段代码中,我们定义了一个枚举类型 `MyEnum`,并为每个字段添加了一个自定义特性 `MyAttribute`。然后,我们使用反射获取枚举类型的字段,并使用 `GetCustomAttribute` 方法获取字段的特性。如果特性存在,我们可以通过修改特性属性动态修改特性。 在上述示例中,我们将枚举类型 `MyEnum` 中所有字段的特性 `Description` 修改为 "New description"。你可以根据自己的需要修改特性的其他属性。记得在实际使用中要进行错误处理和适当的验证。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值