反射一

Assembly ass;
Type type;
Object obj;
Object any = new Object();
ass = Assembly.LoadFile(@"F:TestDemosAssignmentCommonHelperinDebugCommonHelper.dll");
type = ass.GetType("CommonHelper.MyHelper");

MethodInfo method = type.GetMethod("WriteString");
string test = "test";
int i = 1;
Object[] parametors = new Object[] { test, i };
obj = ass.CreateInstance("Reflection.WriteTest");//必须实例化反射要反射的类,因为要使用的方法并不是静态方法
method.Invoke(obj, parametors);
//method.Invoke(any, parametors);//使用的方法并不是静态方法
method = type.GetMethod("StaticWriteString");
method.Invoke(null, new string[] { "test" });//第一个参数忽略,对于第一个参数是无视的,也就是我们写什么都不会被调用
method.Invoke(obj, new string[] { "test" });
method.Invoke(any, new string[] { "test" });

method = type.GetMethod("NoneParaWriteString");
method.Invoke(null, null);//调用无参数静态方法的例子,这时候两个参数我们都不需要指定
Console.ReadLine();

原文地址:https://www.cnblogs.com/lxf1117/p/4178759.html