Question:
How to get a list of all attributes of a class?Answer: reflection provides a solution to this problem. For a given example, you can use the following code:
To access the attributes associated with the type, please use:
obj.GetType().GetProperties();
typeof(Foo).GetProperties();
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)); }
To retrieve the static attribute value, please pass NULL as the first parameter to getValue.
The above is the detailed content of How Can I Retrieve a Class's Properties Using Reflection?. For more information, please follow other related articles on the PHP Chinese website!