2013-02-07 22 views
0

我有一個巨大的用戶定義的類有很多的屬性,其中一些必須設置一定的值。如何在運行時處理用戶定義的類的屬性?

更具體地,所有的公共屬性與在此用戶定義類的類型有在執行結束時「清空」

所以我到達了所有的公共屬性(比如300個屬性),併爲它們分配了我的300行代碼。我做了什麼來解決這個問題,做了我所需要的,但當然不滿足我。

所以決定寫一個輔助方法(如擴展方法)在C#,通過對象實例和訪問的特性迭代/動態操作它們。我已經看到一些類似的問題,關於遍歷這裏的屬性,但目前爲止還沒有看到更改屬性值的任何內容。這裏是我的嘗試:

public static void SetDefaultStringValues(this Object myObject) 
{ 
    PropertyInfo[] aryProperties = entityObject.GetType().GetProperties(); 

    foreach (object property in aryProperties) 
    { 
     if (property is String) 
     { 
      //property = String.Empty; 
     } 
    } 
} 

註釋的部分必須是我設置的變量但是這是不是一個真正的成功故事:)我感謝所有幫助行。

回答

1

您可以使用System.ComponentModelPropertyDescriptor懶惰的編碼。假設你要遍歷公共實例方法(不是靜態),你可以試試下面的示例:

測試

public class TestClass 
{ 
    private int mIntMemeber = 0;     // # to test int type 
    private string mStringMember = "abc";   // # to test string type (initialized) 
    private string mNullStringMember = null;  // # to test string type (null) 

    private static string mStaticNullStringMember; // # to test string type (static) 

    // # Defining properties for each member 
    public int IntMember        
    { 
     get { return mIntMemeber; } 
     set { mIntMemeber = value; }     
    } 

    public string StringMember      
    { 
     get { return mStringMember; } 
     set { mStringMember = value; } 
    } 

    public string NullStringMember 
    { 
     get { return mNullStringMember; } 
     set { mNullStringMember = value; } 
    } 

    public static string StaticNullStringMember 
    { 
     get { return mStaticNullStringMember; } 
     set { mStaticNullStringMember = value; } 
    } 
} 

SetDefaultStringValues()擴展方法

public static string SetDefaultStringValues(this TestClass testClass) 
{ 
    StringBuilder returnLogBuilder = new StringBuilder(); 
    // # Get all properties of testClass instance 
    PropertyDescriptorCollection propDescCollection = TypeDescriptor.GetProperties(testClass); 
    // # Iterate over the property collection 
    foreach (PropertyDescriptor property in propDescCollection) 
    { 
     string name = property.Name; 
     Type t = property.PropertyType; // # Get property type 
     object value = property.GetValue(testClass); // # Get value of the property (value that member variable holds) 

     if (t == typeof(string)) // # If type of propery is string and value is null 
     { 
      property.SetValue(testClass, String.Empty); // # Set value of the property (set member variable) 
      value = String.Empty; // # <-- To prevent NullReferenceException when printing out 
     } 

     returnLogBuilder.AppendLine("*****\nName:\t{0}\nType:\t{1}\nValue:\t{2}", name, t.ToString(), value.ToString()); 
    } 

    returnLogBuilder.AppendLine("*****"); 
    return returnLogBuilder.toString(); 
} 

您也可以在循環內檢查的值是否爲nullSetDefaultStringValues方法參數可以是任何對象實例。您可以將其更改爲SetDefaultStringValues(this object o),並將其作爲擴展方法用於您定義的類實例。

2

使用PropertyInfo.SetValue方法,詳見第here

0

您需要以檢查屬性類型不同的語法;此外,你應該檢查房產是否有公共設置者。

if (property.PropertyType == typeof(string) && property.GetSetMethod() != null) 
    property.SetValue(entityObject, ""); 
0
foreach (PropertyInfo property in aryProperties) 
    { 
     if (property.PropertyType == typeof(string) && property.CanWrite) 
     { 
      property.SetValue(myObject, "", null); 
     } 
    } 
相關問題