using System;
using System.Reflection;
namespace SK.Framework
{
public static class ReflectionExtension
{
///
/// 获取字段值
///
/// 实例
/// 字段名
/// 字段值
public static object GetFieldValue(this object self, string fieldName)
{
Type type = self.GetType();
FieldInfo fieldInfo = type.GetField(fieldName);
return fieldInfo?.GetValue(self);
}
///
/// 获取属性值
///
/// 实例
/// 属性名
///
/// 属性值
public static object GetPropertyValue(this object self, string propertyName, object[] index = null)
{
Type type = self.GetType();
PropertyInfo propertyInfo = type.GetProperty(propertyName);
return propertyInfo?.GetValue(self, index);
}
///
/// 执行方法
///
/// 实例
/// 方法名
/// 参数
///
public static object ExecuteMethod(this object self, string methodName, params object[] args)
{
Type type = self.GetType();
MethodInfo methodInfo = type.GetMethod(methodName);
return methodInfo?.Invoke(self, args);
}
}
}