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

  以上就這么完成。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM