System.Reflection.Assembly ass;
Type type ;
object obj;
try
{
ass = System.Reflection.Assembly.LoadFile(@"D:/huangkai/programe/TestReflect/TestReflect/bin/Debug/TestReflect.dll");
type = ass.GetType("Webtest.ReflectTest");//必须使用名称空间+类名称
System.Reflection.MethodInfo method = type.GetMethod("WriteString");//方法的名称
obj = ass.CreateInstance("Webtest.ReflectTest");//必须使用名称空间+类名称
string s = (string)method.Invoke(obj,new string[]{"hk"}); //实例方法的调用
MessageBox.Show("WriteString:" + s);
method = type.GetMethod("WriteName");//方法的名称
s = (string)method.Invoke(null,new string[]{"hk"}); //静态方法的调用
MessageBox.Show("WriteName:" + s);
method = type.GetMethod("WriteNoPara");//无参数的实例方法
s = (string)method.Invoke(obj,null);
MessageBox.Show("WriteNoPara:" + s);
method = null;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
ass = null;
type = null;
obj = null;
}