Yona
Yona

Reputation: 289

TargetInvocationException was unhandled

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.

I've made a dummy version of it to check if the serialization is working, but I get a 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.

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.

How can I fix this problem?

Upvotes: 0

Views: 6766

Answers (3)

Yona
Yona

Reputation: 289

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:

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

Upvotes: 0

Abhinav Sharma
Abhinav Sharma

Reputation: 453

This exception is usually thrown when a remoting call is made across application domain boundaries. 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.

Upvotes: 1

Yuliia Ashomok
Yuliia Ashomok

Reputation: 8598

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);
    }

Upvotes: 0

Related Questions