通过反射得到一个公共静态字段的值 [英] get value of a public static field via reflection

查看:109
本文介绍了通过反射得到一个公共静态字段的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我到目前为止有:

  VAR道具= typeof运算(Settings.Lookup).GetFields();
 Console.WriteLine(道具[0] .GetValue(Settings.Lookup));
         //编译错误,类名是不是在这一点上有效
 

这是我的静态类

 公共静态类设置
{
   公共静态类查询
   {
      公共静态字符串F1 =ABC;
   }
}
 

解决方案

您需要传递的GetValue 中,由于该字段不属于任何实例

 道具[0] .GetValue(空)
 

this is what I have so far:

 var props = typeof (Settings.Lookup).GetFields();
 Console.WriteLine(props[0].GetValue(Settings.Lookup)); 
         // Compile error, Class Name is not valid at this point

and this is my static class

public static class Settings
{
   public static class Lookup
   {
      public static string F1 ="abc";
   }
}

解决方案

You need to pass null to GetValue, since this field doesn't belong to any instance:

props[0].GetValue(null)

这篇关于通过反射得到一个公共静态字段的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆