调用端:
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 请问何解?谢谢
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 请问何解?谢谢
{
MethodInfo mi = dllType.GetMethod("kkk");
object o=Activator.CreateInstance(dllType);
String nnn = (String)mi.Invoke(o, null);
Console.WriteLine(nnn);
}
改成这样试试.