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

      以上就这么完成。

  • 相关阅读:
    Android UI组件之自定义控件实现IP地址控件
    封装一个类搞定90%安卓客户端与服务器端交互
    深入理解 RecyclerView 系列之:ItemDecoration
    Android开发技巧——设置系统状态栏颜色
    Activity,Fragment的状态保存
    Activity生命周期函数、onSaveInstanceState()和onRestoreInstanceState()的介绍
    Android Fragment生命周期
    恢复云数据库MySQL的备份文件到自建数据库遇到的报错
    如何在宿主机上查看kvm虚拟机的IP
    批量分发公钥脚本
  • 原文地址:https://www.cnblogs.com/kongbailingluangan/p/6243351.html
Copyright © 2011-2022 走看看