[英]Getting inherited public static field with Reflection in Portable Class Libraries
Within a Portable Class Library, I have 2 classes: 在可移植类库中,我有2个类:
The parent 父母
public class Parent
{
public string inherited;
public static string inheritedStatic;
}
And the child the derives from it 孩子从中得到
public class Child : Parent
{
public static string mine;
}
The problem is that I cannot get the inherited static field named "inheritedState", I just get the non-static ("inherited"). 问题是我无法获取名为“ inheritedState”的继承静态字段,而只是获取非静态字段(“ inherited”)。
This is the code that I'm running: 这是我正在运行的代码:
class Program
{
static void Main(string[] args)
{
var childFields = typeof(Child).GetTypeInfo().GetRuntimeFields();
foreach (var fieldInfo in childFields)
{
Console.WriteLine(fieldInfo);
}
}
}
What should I do to get the inherited static field? 我应该怎么做才能获得继承的静态字段? Thanks!
谢谢!
You could use: 您可以使用:
public static FieldInfo[] DeclaredFields(TypeInfo type)
{
var fields = new List<FieldInfo>();
while (type != null)
{
fields.AddRange(type.DeclaredFields);
Type type2 = type.BaseType;
type = type2 != null ? type2.GetTypeInfo() : null;
}
return fields.ToArray();
}
For PCL library, and tested for my: 对于PCL库,并为我进行了测试:
public static IEnumerable<FieldInfo> DeclaredFields(Type type)
{
var fields = new List<FieldInfo>();
while (type != null)
{
fields.AddRange(type.GetRuntimeFields());
type = type.GetTypeInfo().BaseType;
}
return fields;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.