1

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.

GoldNova
  • 147
  • 1
  • 1
  • 11
  • I see "MyClass is inaccessible due to its protection level. Only public types can be processed." - have you tried making `MyClass` be `public`? tip: never have blind exception handlers like this :) – Marc Gravell Nov 23 '22 at 15:06
  • @MarcGravell Yeah the class is actually public. That was a C&P error :) – GoldNova Nov 23 '22 at 15:08
  • and can we see the library twin to `MyClass`? basically I can't repro what you describe - works fine here (see my answer below) – Marc Gravell Nov 23 '22 at 15:10
  • This is now the third time you basically ask the same question (https://stackoverflow.com/questions/74544509/error-is-not-a-valid-value-for-propertyname-when-trying-to-cast-copy-of-objec?noredirect=1#comment131587653_74544509, https://stackoverflow.com/questions/74548110/instance-validation-error-is-not-a-valid-value-for-when-trying-to-copy-obje). Why? – Klaus Gütter Nov 23 '22 at 16:49

1 Answers1

0

Locally, this works just fine and does not show the error you report. My changes:

  1. made MyClass be public
  2. added a LibraryClass that mimics the source type; note in particular the use of [XmlRoot]
using System;
using System.CodeDom.Compiler;
using System.ComponentModel;
using System.Diagnostics;
using System.Xml.Serialization;

static class P
{
    static void Main()
    {
        var obj = new LibraryClass { auth = LibraryAuth.Offline };
        var foo = CreateCopy<LibraryClass, MyClass>(obj);
        Console.WriteLine(foo.auth);
    }
    public static destT CreateCopy<sourceT, destT>(sourceT sourceObj)
    {
        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);
        }

    }
}

public enum LibraryAuth
{
    Online,
    Offline,
}

[XmlRoot("MyClass")] // the two types must agree on naming
public class LibraryClass
{
    [XmlAttribute]
    public LibraryAuth auth { get; set; }
}

[Serializable, XmlType]
public enum MyAuth
{
    [XmlEnum]
    Online,
    [XmlEnum]
    Offline,
}

[Serializable, DebuggerStepThrough, DesignerCategory("code"), GeneratedCode("WebServiceProxyUpdater", "1.0.0.0"), XmlType]
public class MyClass
{
    [XmlAttribute]
    public MyAuth auth { get; set; }
}
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • The thing is I don't have access to the library class. It is a class from a specific library. I am trying to solve this problem here: https://stackoverflow.com/questions/74548110/instance-validation-error-is-not-a-valid-value-for-when-trying-to-copy-obje – GoldNova Nov 23 '22 at 15:45