using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace ProjectBase.Controls.Others
{
internal sealed class ReflectionUtil
{
private ReflectionUtil()
{
}
///
/// 绑定标识
///
public static BindingFlags bf = BindingFlags.DeclaredOnly | BindingFlags.Public |
BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static;
///
/// 执行方法
///
/// 对象实例
/// 方法名称
/// 参数
///
public static object InvokeMethod(object obj, string methodName, object[] args)
{
object objReturn = null;
Type type = obj.GetType();
objReturn = type.InvokeMember(methodName, bf | BindingFlags.InvokeMethod, null, obj, args);
return objReturn;
}
///
/// 设置对象实例的字段值
///
/// 对象实例
/// 字段名称
/// 字段值
public static void SetField(object obj, string name, object value)
{
FieldInfo fi = obj.GetType().GetField(name, bf);
fi.SetValue(obj, value);
}
///
/// 获取对象实例的字段值
///
/// 对象实例
/// 字段名称
///
public static object GetField(object obj, string name)
{
FieldInfo fi = obj.GetType().GetField(name, bf);
return fi.GetValue(obj);
}
///
/// 设置对象属性的值
///
/// 对象实例
/// 属性名称
/// 属性值
public static void SetProperty(object obj, string name, object value)
{
PropertyInfo propertyInfo = obj.GetType().GetProperty(name, bf);
object objValue = Convert.ChangeType(value, propertyInfo.PropertyType);
propertyInfo.SetValue(obj, objValue, null);
}
///
/// 获取对象属性的值
///
public static object GetProperty(object obj, string name)
{
PropertyInfo propertyInfo = obj.GetType().GetProperty(name, bf);
return propertyInfo.GetValue(obj, null);
}
///
/// 获取对象属性信息(组装成字符串输出)
///
public static List GetPropertyNames(object obj)
{
List nameList = new List();
PropertyInfo[] propertyInfos = obj.GetType().GetProperties(bf);
foreach (PropertyInfo property in propertyInfos)
{
nameList.Add(property.Name);
}
return nameList;
}
///
/// 获取对象属性信息(组装成字符串输出)
///
public static Dictionary GetPropertyNameTypes(object obj)
{
Dictionary nameList = new Dictionary();
PropertyInfo[] propertyInfos = obj.GetType().GetProperties(bf);
foreach (PropertyInfo property in propertyInfos)
{
nameList.Add(property.Name, property.PropertyType.FullName);
}
return nameList;
}
public static DataTable CreateTable(object objSource)
{
DataTable table = null;
IEnumerable objList = objSource as IEnumerable;
if (objList == null)
return null;
foreach (object obj in objList)
{
if (table == null)
{
List nameList = ReflectionUtil.GetPropertyNames(obj);
table = new DataTable("");
DataColumn column;
foreach (string name in nameList)
{
column = new DataColumn();
column.DataType = System.Type.GetType("System.String");
column.ColumnName = name;
column.Caption = name;
table.Columns.Add(column);
}
}
DataRow row = table.NewRow();
PropertyInfo[] propertyInfos = obj.GetType().GetProperties(bf);
foreach (PropertyInfo property in propertyInfos)
{
row[property.Name] = property.GetValue(obj, null);
}
table.Rows.Add(row);
}
return table;
}
///
/// 创建对应实例
///
/// 类型
/// 对应实例
public static object CreateInstance(string type)
{
Type tmp = null;
Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
for (int i = 0; i < assemblies.Length; i++)
{
tmp = assemblies[i].GetType(type);
if (tmp != null)
{
return assemblies[i].CreateInstance(type);
}
}
return null;
//return Assembly.GetExecutingAssembly().CreateInstance(type);
}
///
/// 创建对应实例
///
/// 类型
/// 对应实例
public static object CreateInstance(Type type)
{
return CreateInstance(type.FullName);
}
}
}