C#基础_反射
Excell导入调用泛型方法实例总结:
学习:
public class ReflectMethodTest
{
public ReflectMethodTest()
{
Console.WriteLine("this is ReflectMethod");
}
public string Show()
{
return "show 无参方法";
}
public string Show(string name)
{
return $"show 有参方法,参数为{name}";
}
public string Show(int count, string name)
{
return $"show 有参方法,
参数为count:{count},name:{name}";
}
public string Show(string name, int count)
{
return $"show 有参方法,参数为count:{count},name:{name}";
}
public string SHHHH<T>(Student student) where T : Student
{
return $"调用泛型方法 {student.Name}";
}
public string SHHHH<T>(Student student,int count) where T : Student
{
return $"调用泛型方法 {student.Name},count {count}";
}
///调用带泛型参数的重载的泛型方法
public string Show<T>(T t)
{
return $"this is Show<T> method params[name:{t} ]";
}
}
调用
static void Main(string[] args)
{
System.Console.WriteLine("反射的学习--------------------------------");
var object1 = new ReflectMethodTest();
var type = typeof(ReflectMethodTest);
var methods = type.GetMethods();
foreach (var item in methods)
{
Console.WriteLine(item.Name);
}
System.Console.WriteLine("---调用无参方法--------------------------------");
var show = type.GetMethod("Show", new Type[] { });
var res = show.Invoke(object1, new object[] { });
Console.WriteLine(res);
System.Console.WriteLine("---调用一个参数的有参方法(string name)");
var show1 = type.GetMethod("Show", new Type[] { typeof(string) });
var res1 = show1.Invoke(object1, new object[] { "张三" });
Console.WriteLine(res1);
System.Console.WriteLine("---调用二个参数的有参方法(int count,string name)");
var show2 = type.GetMethod("Show", new Type[] { typeof(int), typeof(string) });
var res2 = show2.Invoke(object1, new object[] { 100, "张三" });
Console.WriteLine(res2);
System.Console.WriteLine("---调用二个参数的有参方法(string name,int count)");
var show3 = type.GetMethod("Show", new Type[] { typeof(string), typeof(int) });
var res3 = show3.Invoke(object1, new object[] { "李四", 10890 });
Console.WriteLine(res3);
System.Console.WriteLine("---调用泛型方法");
MethodInfo mi = type.GetMethod("SHHHH", new Type[] { typeof(Student), typeof(int) });
///获取可使用的泛型方法,必须通过该方法才能传递泛型参数
MethodInfo miConstructed = mi.MakeGenericMethod(new Type[] { typeof(int) });
var res4 = miConstructed.Invoke(object1, new object[] { new Student() { Name = "ma6", Age = 28, ClassName = "4年纪" }, 1 });
Console.WriteLine(res4);
System.Console.WriteLine("---调用带泛型参数的重载的泛型方法");
MethodInfo showMethodT = null;
var methods = type.GetMethods();
foreach (var method in methods)
{
if (method.ToString() == "System.String Show[T](T)")
{
showMethodT = method;
var showMethodTCtor = showMethodT.MakeGenericMethod(typeof(string));
var res1 = showMethodTCtor.Invoke(new ReflectMethodTest2(), new object[] { "李四" });
Console.WriteLine(res1);
}
}
Console.Read();
}
输出结果
--- 调用带泛型参数的重载的泛型方法
工作中的例子
/// <summary>
/// 得到程序集
/// </summary>
static Assembly GetAssembly()
{
AssemblyName name = new AssemblyName("Henkel.DataModels");//我的程序集的名称为"CommonEntity"
var result = Assembly.Load(name);
//Console.WriteLine(result.FullName);
return result;
//TestGetIntance(result);
//GetIntanceType(result);
}
/// <summary>
/// 获取实体类型
/// </summary>
/// <param name="assembly"></param>
public Type GetIntanceType(string str, Assembly assembly = null)
{
if (assembly == null)
{
assembly=GetAssembly();
}
var clstype = assembly.GetType($"Henkel.DataModels.Entity.{str}");
return clstype;
}
调用例子1
//var xxx= IDbMaintenance
//获取到接口类型
var sqlSugarClient = ExcelHelper.GetDb().DbMaintenance;
//ExcelHelper.GetDb().DbMaintenance.TruncateTable<T>();
var temp = typeof(IDbMaintenance).GetMethod("TruncateTable", new Type[] { });
var methodInfo = temp.MakeGenericMethod(new ReflectHellp().GetIntanceType(tableName));
methodInfo.Invoke(sqlSugarClient, new object[] { });
CommonHelp.writeLine(string.Format("{0} 清除完毕 DateTime:{1}", tableName, DateTime.Now.ToLongTimeString()));
调用例子2(通过字符串调用泛型方法)
///通过字符串调用泛型方法
///ExcelHelper.WorkSheetImport<T>(package)
var methodInfo=typeof(ExcelHelper).GetMethod();
///如果对应的方法名有重载的,要把参数传递进来.(WorkSheetImport:方法名,)
var methodInfo = typeof(ExcelHelper).GetMethod("WorkSheetImport",
new Type[] { package.GetType() }).MakeGenericMethod(new ReflectHellp().GetIntanceType(tableName));
var import = (int)methodInfo.Invoke(null, new object[] { package });