使用反射时方法不可用 - TargetInitationError

发布于 2024-10-21 15:30:05 字数 1536 浏览 0 评论 0原文

我已经能够使用反射成功执行其他方法,但我现在收到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

醉梦枕江山 2024-10-28 15:30:05

您的错误仅意味着在调用的方法中的某处抛出了异常。这正是你所指出的。您的 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.

别念他 2024-10-28 15:30:05

从使用反射调用的其他方法中调用方法是否存在问题?

不,TargetInitationException 表示该方法已通过反射成功调用,但目标方法抛出了异常。有关目标方法引发的异常的详细信息,请查看 TargetIn VocationExceptionInnerException 属性。

Is there a problem calling methods from within other methods that are being called using reflection?

No, TargetInvocationException means that the method was successfully invoked by reflection, but that the target method threw an exception. Look at the InnerException property of the TargetInvocationException for details on the exception that was thrown by the target method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文