Let's say I have a library with the following enum attributes:
public enum LibraryAuth
{
Online,
Offline,
}
This enum is being used in an Object of the library.
Now I have my own enum which goes like this:
[Serializable, XmlType]
public enum MyAuth
{
[XmlEnum]
Online,
[XmlEnum]
Offline,
}
This one is supposed to be used within a class of my own programm:
[Serializable, DebuggerStepThrough, DesignerCategory("code"), GeneratedCode("WebServiceProxyUpdater", "1.0.0.0"), XmlType]
public class MyClass
{
[XmlAttribute]
public MyAuth auth { get; set; }
}
The object of the library and my object have the same attributes.
Now I am trying to cast the object of the library into the object of my program with this function:
public static destT CreateCopy<sourceT, destT>(sourceT sourceObj)
{
try
{
XmlSerializer sourceXS = new XmlSerializer(typeof(sourceT));
XmlSerializer destXS = new XmlSerializer(typeof(destT));
using (MemoryStream ms = new MemoryStream())
{
sourceXS.Serialize(ms, sourceObj);
ms.Position = 0;
return (destT)destXS.Deserialize(ms);
}
}
catch (Exception ex)
{
return default(destT);
}
}
Unfortunately this throws the following error: Instance validation error: '' is not a valid value for MyAuth
If I replace my enum with the enum of the library the serialisation works just fine. But somehow serializing it with my enum does not work.
What am I missing in my Enum Class?
EDIT: I've read about a similar problem in this thread but the OP in that thread wanted to ignore the error. I on the other hand want to cast one object into another.