wpf TargetInvocationException 未处理

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24080327/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 11:51:18  来源:igfitidea点击:

TargetInvocationException was unhandled

c#wpfserializationxml-serializationsystem.reflection

提问by Yona

I was searching a lot and found some solutions, but they don't work for me. I have some GUI creation tool written in WPF, and I want to be able to serialize instances of objects.

我搜索了很多并找到了一些解决方案,但它们对我不起作用。我有一些用 WPF 编写的 GUI 创建工具,我希望能够序列化对象的实例。

I've made a dummy version of it to check if the serialization is working, but I get a TargetInvocationException.

我制作了一个虚拟版本来检查序列化是否有效,但我得到了一个TargetInvocationException.

The project has two classes of label and image that extend CanvasItem, a class of layout which holds a collection CanvasItems, and a class of project which holds a collection of layouts.

该项目有两个扩展 CanvasItem 的标签和图像类,一个包含 CanvasItems 集合的布局类,以及一个包含布局集合的项目类。

The class I wrote to serialize:

我写的序列化类:

public class XMLWrite
{
    public static void WriteXML(LCTProject project)
    {
        System.Xml.Serialization.XmlSerializer writer =
        new System.Xml.Serialization.XmlSerializer(typeof(LCTProject));
        string path = Directory.GetParent(Directory.GetParent(Directory.GetParent(
            System.AppDomain.CurrentDomain.BaseDirectory.ToString()).ToString()).ToString()).ToString()
            + project.name + ".xml";
        System.IO.StreamWriter file = new System.IO.StreamWriter(path);
        writer.Serialize(file, project);
        file.Close();
    }

    public static LCTProject ReadXML(string name)
    {
        System.Xml.Serialization.XmlSerializer reader =
            new System.Xml.Serialization.XmlSerializer(typeof(LCTProject));
        string path = Directory.GetParent(Directory.GetParent(Directory.GetParent(
            System.AppDomain.CurrentDomain.BaseDirectory.ToString()).ToString()).ToString()).ToString()
            + name + ".xml";
        System.IO.StreamReader file = new System.IO.StreamReader(path);
        LCTProject project = new LCTProject();
        project = (LCTProject)reader.Deserialize(file);
        return project;
    }
}

And how I try to make it run:

以及我如何尝试让它运行:

public MainWindow()
    {
        InitializeComponent();

        LCTLabel label1 = new LCTLabel();
        label1.locationX = 6;
        label1.locationY = 8;
        label1.alignment = CanvasItem.Alignment.Bottom;
        label1.text = "hi hi hi";
        label1.textSize = 12;
        Color clr = new Color();
        label1.color = clr;

        LCTImage img = new LCTImage();
        img.locationX = 1;
        img.locationY = 2;
        img.alignment = CanvasItem.Alignment.Right;
        img.path = @"C:\";

        LCTImage img2 = new LCTImage();
        img2.locationX = 500;
        img2.locationY = 100;
        img2.alignment = CanvasItem.Alignment.Up;
        img2.path = @"C:\";

        LCTLayout layout1 = new LCTLayout();
        LCTLayout layout2 = new LCTLayout();

        layout1.items.Add(label1);
        layout1.items.Add(img);
        layout2.items.Add(img);
        layout2.items.Add(img2);

        LCTProject project = new LCTProject();
        project.layouts.Add(layout1);
        project.layouts.Add(layout2);

        XMLWrite.WriteXML(project);
    }

And I get the following exception:

我得到以下异常:

TargetInvocationException was unhandled An unhandled exception of type 'System.Reflection.TargetInvocationException' occurred in PresentationFramework.dll Additional information: Exception has been thrown by the target of an invocation.

TargetInvocationException 未处理 在 PresentationFramework.dll 中发生类型为“System.Reflection.TargetInvocationException”的未处理异常 附加信息:调用目标已抛出异常。

How can I fix this problem?

我该如何解决这个问题?

回答by Abhinav Sharma

This exception is usually thrown when a remoting call is made across application domainboundaries. It is not that meaningless if you know where to look for details. The key property is InnerException - you should examine this one to obtain the real exception occurred. Chances are this property will contain another TargetInvocationException instance - so you should continue digging into the InnerException chain until you've found something meaningful.

当跨应用程序域边界进行远程调用时,通常会抛出此异常 。如果您知道在哪里查找详细信息,这并不是毫无意义。关键属性是 InnerException - 您应该检查这个属性以获得真正发生的异常。此属性可能包含另一个 TargetInvocationException 实例 - 因此您应该继续深入研究 InnerException 链,直到找到有意义的内容。

回答by Yuliia Ashomok

Try to get trace and InnerException.

尝试获取跟踪和内部异常。

    try
    {
    //somecode
    }
    catch (Exception e)
    {
     Console.WriteLine("Error trace {0}", e.Trace);
     Console.WriteLine ("Inner Exception is {0}",e.InnerException);
    }

回答by Yona

It turns out trying and catching does not help. There are two important things to remember:

事实证明,尝试和捕捉无济于事。有两件重要的事情要记住:

First, using XmlSerializer you can't serialize objects that do not have a constructor without arguments. You should add a constructor without arguments, or if you don't really need a constructor, don't add one.

Second, there is a problem serializing a collection of different objects (of different classes) that only inherit the same class. Let's say the base class is A; B and C inherit A. So you should add to the definition of A:

首先,使用 XmlSerializer 不能序列化没有没有参数的构造函数的对象。你应该添加一个没有参数的构造函数,或者如果你真的不需要一个构造函数,就不要添加一个。

其次,序列化仅继承同一类的不同对象(不同类)的集合存在问题。假设基类是 A;B 和 C 继承了 A。所以你应该添加到 A 的定义中:

[XmlInclude(typeof(B))]
[XmlInclude(typeof(C))]
public class A
{...}