通过反射得到一个公共静态字段的值字段、静态、反射

2023-09-02 01:57:07 作者:痴货、

这是我到目前为止有:

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

这是我的静态类

 公共静态类设置
{
   公共静态类查询
   {
      公共静态字符串F1 =ABC;
   }
}
 
sqlite 如何获取某一字段的所有值

解决方案

您需要传递的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)