Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serialize Object

Tags:

c#

I am new to serialization and I cannot for the life of me figure out how to fix this exception I am getting...

I have an object that has the following structure

[XmlRoot("MaxCut2")]
public class MaxCut2File : IFile
{
    public MaxCut2File()
    {
        MyJob = new Job();
        Job.Reference = "MyRef";
    }

    [XmlElement("JobDetails", typeof(Job))]
    public IJob MyJob
    {
        get;
        set;
    }    
}

An inteferface...

   public interface IJob
    {        
        string Reference { get; set; }
    }

And an class

[Serializable()]
public class Job : IJob
{
    [XmlElement("Reference")]
    public string Reference { get; set; }
}

When I try to serialize an instance of the MaxCut2File object I get an error

{"Cannot serialize member 'MaxCut2File.MaxCut2File.MyJob' of type 'MaxCut2BL.Interfaces.IJob', see inner exception for more details."} "There was an error reflecting type 'MaxCut2File.MaxCut2File'."

However if I change my property MyJob from the IJob type to the Job type it works fine...

Any ideas?

like image 842
Mark Pearl Avatar asked Apr 18 '26 10:04

Mark Pearl


1 Answers

There is a little trick You might give a try :

Serializing an interface

like image 162
Luzik Avatar answered Apr 22 '26 19:04

Luzik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!