36 lines
994 B
C#
36 lines
994 B
C#
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
|
|
public static class ReflectionExtensions
|
|
{
|
|
public static IEnumerable<FieldInfo> GetAllFields(this Type type)
|
|
{
|
|
if (type == null)
|
|
{
|
|
return new List<FieldInfo>();
|
|
}
|
|
|
|
BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic |
|
|
BindingFlags.Static | BindingFlags.Instance |
|
|
BindingFlags.DeclaredOnly;
|
|
|
|
return type.GetFields(flags).Concat(GetAllFields(type.BaseType));
|
|
}
|
|
|
|
public static FieldInfo GetAllField(this Type type, string fieldName)
|
|
{
|
|
if (type == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic |
|
|
BindingFlags.Static | BindingFlags.Instance;
|
|
|
|
return type.GetField(fieldName, flags) ?? GetAllField(type.BaseType, fieldName);
|
|
}
|
|
}
|