How do I get a list of all the properties of a class?
From stackoverflow
-
Reflection; for an instance:
obj.GetType().GetPropeties();
for a type:
typeof(Foo).GetProperties();
for example:
class Foo { public int A {get;set;} public string B {get;set;} } ... Foo foo = new Foo {A = 1, B = "abc"}; foreach(var prop in foo.GetType().GetProperties()) { Console.WriteLine("{0}={1}", prop.Name, prop.GetValue(foo, null)); }
Following feedback...
- To get the value of static properties, pass
null
as the first argument toGetValue
- To look at non-public properties, use (for example)
GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
(which returns all public/private instance properties ).
Marc Gravell : For completeness, there is also the ComponentModel, exposed by TypeDescriptor.GetProperties(...) - which allows dynamic runtime properties (reflection is fixed at compile-time).Richard : Suggestion: Expand answer to cover protected/private/static/inherited properties. - To get the value of static properties, pass
-
You can use reflection.
Type typeOfMyObject = myObject.GetType(); PropertyInfo[] properties =typeOfMyObject.GetProperties();
-
You can use Reflection to do this: (from my library - this gets the names and values)
public static Dictionary<string, object> DictionaryFromType(object atype) { if (atype == null) return new Dictionary<string, object>(); Type t = atype.GetType(); PropertyInfo[] props = t.GetProperties(); Dictionary<string, object> dict = new Dictionary<string, object>(); foreach (PropertyInfo prp in props) { object value = prp.GetValue(atype, new object[]{}); dict.Add(prp.Name, value); } return dict; }
This thing will not work for properties with an index - for that (it's getting unwieldy):
public static Dictionary<string, object> DictionaryFromType(object atype, Dictionary<string, object[]> indexers) { /* replace GetValue() call above with: */ object value = prp.GetValue(atype, ((indexers.ContainsKey(prp.Name)?indexers[prp.Name]:new string[]{}); }
Also, to get only public properties: (see MSDN on BindingFlags enum)
/* replace */ PropertyInfo[] props = t.GetProperties(); /* with */ PropertyInfo[] props = t.GetProperties(BindingFlags.Public)
This works on anonymous types, too!
To just get the names:public static string[] PropertiesFromType(object atype) { if (atype == null) return new string[] {}; Type t = atype.GetType(); PropertyInfo[] props = t.GetProperties(); List<string> propNames = new List<string>(); foreach (PropertyInfo prp in props) { propNames.Add(prp.Name); } return propNames.ToArray(); }
And it's just about the same for just the values, or you can use:
GetDictionaryFromType().Keys // or GetDictionaryFromType().Values
But that's a bit slower, I would imagine.
Marc Gravell : ... but atype.GetProperty(prp.Name) is going to return prp?Lucas Jones : Oh - you're right! D'oh.Lucas Jones : I've fixed that now. -
You could use the
System.Reflection
namespace with theType.GetProperties()
mehod:PropertyInfo[] propertyInfos; propertyInfos = typeof(MyClass).GetProperties(BindingFlags.Public|BindingFlags.Static);
0 comments:
Post a Comment