读书人

反射出TargetException 异常请教何解

发布时间: 2012-03-06 20:47:55 作者: rapoo

反射出TargetException 错误,请问何解?急,在线等,谢谢!!
调用端:
string filepath = @ "E:\VS2005\DataCollector\DataCollector\Components\DataAnalyzer.dll ";
String test = "分析中 ";
Assembly myDllAssembly = Assembly.LoadFrom(filepath);
Type dllType = myDllAssembly.GetType( "DataAnalyzer.Goodoon ");
if (dllType!=null)
{
MethodInfo mi = dllType.GetMethod( "kkk ");
String nnn = (String)mi.Invoke(dllType, null); <----出错,TargetException
Console.WriteLine(nnn);
}


在DLL那边:
public String kkk()
{
String teststr = "abc ";
return teststr;
}


在出错行那里提示:对象与目标类型不匹配,TargetException

请问何解?谢谢

[解决办法]
if (dllType!=null)
{
MethodInfo mi = dllType.GetMethod( "kkk ");
object o=Activator.CreateInstance(dllType);
String nnn = (String)mi.Invoke(o, null);
Console.WriteLine(nnn);
}


改成这样试试.

读书人网 >C#

热点推荐