使用反射时方法不可用 - TargetInitationError
我已经能够使用反射成功执行其他方法,但我现在收到 TargetInitationException
。尽管 TargetInvoationException
将我指向 methodInfo.Invoke
方法,但单步执行代码显示在 SampleXMLToDataTable 时
被调用。 Load
方法中发生异常SampleXMLToDataTable
是一个 public static
方法,与 Load
位于同一类中。当代码尝试输入 SampleXMLToDataTable
时,会引发该错误。
从使用反射调用的其他方法中调用方法是否存在问题?
通过反射调用的代码:
private Object CreateXMLDataLoaderInstance(string xml)
{
object o = null;
Assembly demandAssembly = LoadSampleDemandAssembly();
Type assemblyType = demandAssembly.GetType("SampleDemand.XMLDataLoader");
MethodInfo methodInfo = assemblyType.GetMethod("Load");
o = Activator.CreateInstance(assemblyType, new Object[1] { true });
Object[] oParamArray2 = new Object[1];
methodInfo.Invoke(o, new Object[1] { xml });//TargetInvocationException
return o;
}
以及它尝试调用的方法:
public void Load(string xml)
{
XmlDocument xDoc = new XmlDocument();
xDoc.LoadXml(xml);
XmlNode settingsNode = null;
foreach (XmlNode xNode in xDoc.FirstChild.ChildNodes)
{
string name = xNode.Name;
string wsx = xNode.ChildNodes[0].OuterXml;
XmlDocument doc = new XmlDocument();
doc.LoadXml(wsx);
DataTable dt = SampleXMLToDataTable(doc);//Where the code breaks
XMLSample xmlWS = new XMLSample(dt, wsx, name);
this.sample.Add(name, xmlWS);
}
if (settingsNode != null)
{
settings = GetSettings(settingsNode);
}
}
I have been able to successfully execute other methods using reflection but I am now getting a TargetInvocationException
. Although the TargetInvoationException
points me to the methodInfo.Invoke
method, stepping through the code shows the exception occuring in the Load
method when SampleXMLToDataTable
is called. The SampleXMLToDataTable
is a public static
method in the same class as Load
. The error is thrown as the code is attempting to enter the SampleXMLToDataTable
.
Is there a problem calling methods from within other methods that are being called using reflection?
The code that calls via reflection:
private Object CreateXMLDataLoaderInstance(string xml)
{
object o = null;
Assembly demandAssembly = LoadSampleDemandAssembly();
Type assemblyType = demandAssembly.GetType("SampleDemand.XMLDataLoader");
MethodInfo methodInfo = assemblyType.GetMethod("Load");
o = Activator.CreateInstance(assemblyType, new Object[1] { true });
Object[] oParamArray2 = new Object[1];
methodInfo.Invoke(o, new Object[1] { xml });//TargetInvocationException
return o;
}
And the method it is trying to invoke:
public void Load(string xml)
{
XmlDocument xDoc = new XmlDocument();
xDoc.LoadXml(xml);
XmlNode settingsNode = null;
foreach (XmlNode xNode in xDoc.FirstChild.ChildNodes)
{
string name = xNode.Name;
string wsx = xNode.ChildNodes[0].OuterXml;
XmlDocument doc = new XmlDocument();
doc.LoadXml(wsx);
DataTable dt = SampleXMLToDataTable(doc);//Where the code breaks
XMLSample xmlWS = new XMLSample(dt, wsx, name);
this.sample.Add(name, xmlWS);
}
if (settingsNode != null)
{
settings = GetSettings(settingsNode);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的错误仅意味着在调用的方法中的某处抛出了异常。这正是你所指出的。您的
SampleXMLToDataTable
方法引发异常。这与通过反射调用它无关。我怀疑如果你直接调用它,你会在完全相同的位置得到一个异常。这不是反射的限制,您只是得到一个不同的异常,因为您是通过反射调用它的,尽管
TargetInitationException
的内部异常应该为您提供更多信息。Your error just means that an exception is being thrown somewhere in the invoked method. This is exactly what you have pointed out. Your
SampleXMLToDataTable
method is throwing an exception. This has nothing to do with calling it via reflection. I suspect that if you call it directly, you will get an exception in the exact same spot.This is not a limitation of reflection, you just get a different exception because you are invoking it through reflection, though the inner exception of the
TargetInvocationException
should give you more information.不,
TargetInitationException
表示该方法已通过反射成功调用,但目标方法抛出了异常。有关目标方法引发的异常的详细信息,请查看TargetIn VocationException
的InnerException
属性。No,
TargetInvocationException
means that the method was successfully invoked by reflection, but that the target method threw an exception. Look at theInnerException
property of theTargetInvocationException
for details on the exception that was thrown by the target method.