2010-12-13 153 views

回答

3

可能類似於下面

說有一個對象strList

PropertyInfo[] info = strList.GetType().GetProperties(); 

     Dictionary<string, object> propNamesAndValues = new Dictionary<string, object>(); 

     foreach (PropertyInfo pinfo in info) 
     { 
      propNamesAndValues.Add(pinfo.Name, pinfo.GetValue(strList, null)); 
     }    
0

嘗試使用這樣的:

MyClass aClass = new MyClass(); 

Type t = aClass.GetType(); 
PropertyInfo[] pi = t.GetProperties(); 

foreach (PropertyInfo prop in pi) 
    Console.WriteLine("Prop: {0}", prop.Name); 

希望它會幫助你。

1

試試這個

var properties = myObj.GetType() 
        .GetProperties(BindingFlags.Instance | BindingFlags.Public); 
var propertyNames = properties.Select(p => p.Name); 
var propertyValues = properties.Select(p => p.GetValue(myObj, null));