60 lines
1.8 KiB
C#
60 lines
1.8 KiB
C#
using System;
|
|
using System.Reflection;
|
|
|
|
namespace BitStrap
|
|
{
|
|
public static class ReflectionHelper
|
|
{
|
|
public static readonly object[] EmptyArgs = new object[0];
|
|
|
|
public static TField[] GetFieldValuesOfType<TField>(object instance)
|
|
{
|
|
return GetFieldValuesOfType<TField>(instance, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
|
|
}
|
|
|
|
public static TField[] GetFieldValuesOfType<TField>(object instance, BindingFlags bindingFlags)
|
|
{
|
|
FieldInfo[] fields = instance.GetType().GetFields(bindingFlags);
|
|
int num = fields.Count((FieldInfo f) => typeof(TField).IsAssignableFrom(f.FieldType));
|
|
TField[] array = new TField[num];
|
|
int num2 = 0;
|
|
FieldInfo[] array2 = fields;
|
|
foreach (FieldInfo fieldInfo in array2)
|
|
{
|
|
if (typeof(TField).IsAssignableFrom(fieldInfo.FieldType))
|
|
{
|
|
array[num2++] = (TField)fieldInfo.GetValue(instance);
|
|
}
|
|
}
|
|
return array;
|
|
}
|
|
|
|
public static FieldInfo[] GetFieldsOfType<TField>(Type type)
|
|
{
|
|
return GetFieldsOfType<TField>(type, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
|
|
}
|
|
|
|
public static FieldInfo[] GetFieldsOfType<TField>(Type type, BindingFlags bindingFlags)
|
|
{
|
|
FieldInfo[] fields = type.GetFields(bindingFlags);
|
|
int num = fields.Count((FieldInfo f) => typeof(TField).IsAssignableFrom(f.FieldType));
|
|
FieldInfo[] array = new FieldInfo[num];
|
|
int num2 = 0;
|
|
FieldInfo[] array2 = fields;
|
|
foreach (FieldInfo fieldInfo in array2)
|
|
{
|
|
if (typeof(TField).IsAssignableFrom(fieldInfo.FieldType))
|
|
{
|
|
array[num2++] = fieldInfo;
|
|
}
|
|
}
|
|
return array;
|
|
}
|
|
|
|
public static T GetCustomAttribute<T>(this Type type, bool inherit) where T : Attribute
|
|
{
|
|
return type.GetCustomAttributes(typeof(T), inherit).FirstOrDefault() as T;
|
|
}
|
|
}
|
|
}
|