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

xiaoxiao2021-07-05  238

一、简述

  在做项目的过程中要用到 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; }

  以上就这么完成。

转载请注明原文地址: https://www.6miu.com/read-4821439.html

最新回复(0)