验证中...
Languages: C#
Categories: 编程语言基础
Latest update 2019-03-27 15:24
KuroLib.Helper.ReflexHelper
Raw Copy
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace KuroLib.Helper
{
public static class ReflexHelper
{
public static T GetField<T>(this object instance, string fieldname)
{
var flag = BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public;
var type = instance.GetType();
var field = type.GetField(fieldname, flag);
return (T)field?.GetValue(instance);
}
public static T GetPrivateField<T>(this object instance, string fieldname)
{
var flag = BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic;
var type = instance.GetType();
var field = type.GetField(fieldname, flag);
return (T)field?.GetValue(instance);
}
public static T GetPublicField<T>(this object instance, string fieldname)
{
var flag = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public;
var type = instance.GetType();
var field = type.GetField(fieldname, flag);
return (T)field?.GetValue(instance);
}
public static List<FieldInfo> GetFieldList<T>(this object instance)
{
var flag = BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public;
var type = instance.GetType();
return type.GetFields(flag).ToList();
}
public static List<FieldInfo> GetPrivateFieldList<T>(this object instance)
{
var flag = BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic;
var type = instance.GetType();
return type.GetFields(flag).ToList();
}
public static List<FieldInfo> GetPublicFieldList<T>(this object instance)
{
var flag = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public;
var type = instance.GetType();
return type.GetFields(flag).ToList();
}
public static T GetFieldInBase<T>(this object instance, string fieldname)
{
var flag = BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public;
var type = instance.GetType().BaseType;
var field = type?.GetField(fieldname, flag);
return (T)field?.GetValue(instance);
}
public static T GetPrivateFieldInBase<T>(this object instance, string fieldname)
{
var flag = BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic;
var type = instance.GetType().BaseType;
var field = type?.GetField(fieldname, flag);
return (T)field?.GetValue(instance);
}
public static T GetPublicFieldInBase<T>(this object instance, string fieldname)
{
var flag = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public;
var type = instance.GetType().BaseType;
var field = type?.GetField(fieldname, flag);
return (T)field?.GetValue(instance);
}
public static List<FieldInfo> GetFieldListInBase<T>(this object instance)
{
var flag = BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public;
var type = instance.GetType().BaseType;
return type?.GetFields(flag).ToList();
}
public static List<FieldInfo> GetPrivateFieldListInBase<T>(this object instance)
{
var flag = BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic;
var type = instance.GetType().BaseType;
return type?.GetFields(flag).ToList();
}
public static List<FieldInfo> GetPublicFieldListInBase<T>(this object instance)
{
var flag = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public;
var type = instance.GetType().BaseType;
return type?.GetFields(flag).ToList();
}
public static void SetField(this object instance, string fieldname, object value)
{
var flag = BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public;
var type = instance.GetType();
var field = type.GetField(fieldname, flag);
field?.SetValue(instance, value);
}
public static void SetPrivateField(this object instance, string fieldname, object value)
{
var flag = BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic;
var type = instance.GetType();
var field = type.GetField(fieldname, flag);
field?.SetValue(instance, value);
}
public static void SetPublicField(this object instance, string fieldname, object value)
{
var flag = BindingFlags.Instance | BindingFlags.Public;
var type = instance.GetType();
var field = type.GetField(fieldname, flag);
field?.SetValue(instance, value);
}
public static void SetFieldInBase(this object instance, string fieldname, object value)
{
var flag = BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public;
var type = instance.GetType().BaseType;
var field = type?.GetField(fieldname, flag);
field?.SetValue(instance, value);
}
public static void SetPrivateFieldInBase(this object instance, string fieldname, object value)
{
var flag = BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic;
var type = instance.GetType().BaseType;
var field = type?.GetField(fieldname, flag);
field?.SetValue(instance, value);
}
public static void SetPublicFieldInBase(this object instance, string fieldname, object value)
{
var flag = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public;
var type = instance.GetType().BaseType;
var field = type?.GetField(fieldname, flag);
field?.SetValue(instance, value);
}
public static T GetPrivateProperty<T>(this object instance, string propertyname)
{
var flag = BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic;
var type = instance.GetType();
var field = type.GetProperty(propertyname, flag);
return (T)field.GetValue(instance, null);
}
public static T GetPublicProperty<T>(this object instance, string propertyname)
{
var flag = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public;
var type = instance.GetType();
var field = type.GetProperty(propertyname, flag);
return (T)field.GetValue(instance, null);
}
public static void SetPrivateProperty(this object instance, string propertyname, object value)
{
var flag = BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic;
var type = instance.GetType();
var field = type.GetProperty(propertyname, flag);
field.SetValue(instance, value, null);
}
public static void SetPublicProperty(this object instance, string propertyname, object value)
{
var flag = BindingFlags.Instance | BindingFlags.Public;
var type = instance.GetType();
var field = type.GetProperty(propertyname, flag);
field.SetValue(instance, value, null);
}
public static T CallPrivateMethod<T>(this object instance, string name, params object[] param)
{
var flag = BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic;
var type = instance.GetType();
var method = type.GetMethod(name, flag);
return (T)method.Invoke(instance, param);
}
public static T CallPublicMethod<T>(this object instance, string name, params object[] param)
{
var flag = BindingFlags.Instance | BindingFlags.Public;
var type = instance.GetType();
var method = type.GetMethod(name, flag);
return (T)method.Invoke(instance, param);
}
}
}

Comment list( 0 )

Sign in for post a comment

Help Search