C#反射机制问题
dll文件Code如下(保存文件名为ReflectTest.dll)
using System;
using System.Collections.Generic;
using System.Text;
namespace Webtest
{
public class ReflectTest
{
public ReflectTest()
{
}
public string WriteString(string s)
{
return "欢迎您," + s;
}
public static string WriteName(string s)
{
return "欢迎您光临,Crazycoder.cn" + s;
}
public string WriteNoPara()
{
return "您使用的是无参数方法";
}
}
}
页面代码如下:
protected void Page_Load(object sender, EventArgs e)
{
System.Reflection.Assembly ass;
Type type ;
object obj;
try
{
ass = System.Reflection.Assembly.LoadFile(@"f:\Test.dll");
type = ass.GetType("Webtest.ReflectTest");-----------------------------------------1
System.Reflection.MethodInfo method = type.GetMethod("WriteString");
obj = ass.CreateInstance("Webtest.ReflectTest");
string s = (string)method.Invoke(obj,new string[]{"jianglijun"});
Response.Write(s+"<br>");
method = type.GetMethod("WriteName");
s = (string)method.Invoke(null,new string[]{"jianglijun"});
Response.Write(s+"<br>");
method = type.GetMethod("WriteNoPara");
s = (string)method.Invoke(obj,null);
Response.Write(s+"<br>");
method = null;
}
catch(Exception ex)
{
Response.Write(ex+"<br>");
}
finally
{
ass = null;
type = null;
obj = null;
}
}
我在运行跟踪时发现,在1位置时type的值为Null.请问这是什么问题引起的?如何解决?
C#使用反射机制时要哪些条件?请前辈们多多提点~~~
[解决办法]
你看下数组的长度~~这里有个叫程序集的名称~~ 应该和你的 Webtest.ReflectTest有区别