如何使用Reflection来检索属性?

pyo*_*yon 3 c# reflection

如何使用Reflection获取静态只读属性?它的访问修饰符(public,protected,private)不相关.

Mat*_*ias 5

您可以使用Type类的GetProperty()方法:http: //msdn.microsoft.com/en-us/library/kz0a8sxy.aspx

Type t = typeof(MyType);
PropertyInfo pi = t.GetProperty("Foo");
object value = pi.GetValue(null, null);

class MyType
{
 public static string Foo
 {
   get { return "bar"; }
 } 
}
Run Code Online (Sandbox Code Playgroud)