zoukankan      html  css  js  c++  java
  • 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;
            }

      以上就这么完成。

  • 相关阅读:
    SSM项目搭建(提供源码)
    U盘启动安装linux时卡在“starting dracut initqueue hook”
    nginx 中只能访问根目录,无法访问路由(404)
    在多GPU情况下TensorFlow如何指定在哪些GPU上运行程序
    TensorFlow只训练部分参数
    python中的随机数函数
    Python中读取、显示和保存图片的方法
    神经网络中参数数量的计算
    排序算法
    window Linux 双系统安装
  • 原文地址:https://www.cnblogs.com/kongbailingluangan/p/6243351.html
Copyright © 2011-2022 走看看