【发布时间】:2018-04-05 12:22:46
【问题描述】:
我正在尝试对包含枚举属性的类进行 XML 序列化。如果该属性是使用特定枚举声明的,则它可以正常工作。但是我需要属性是枚举类型,所以我可以将它设置为不同的枚举类型。但是,执行此操作时出现异常。
类型 [namespace].Simple 不能在此上下文中使用。
我在枚举定义上尝试了不同的属性,但到目前为止还没有做到正确。有没有办法做到这一点?
public enum Simple : byte
{
one = 0x01,
two = 0x02,
three = 0x03
}
public class Foo
{
public Enum Simple { get; set; }
}
public class Program
{
static void Main(string[] args)
{
using (var writer = XmlWriter.Create(Console.OpenStandardOutput()))
{
try
{
var foo = new Foo
{
Simple = Simple.three
};
var serializer = new XmlSerializer(foo.GetType());
serializer.Serialize(writer, foo);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
Console.ReadLine();
}
}
【问题讨论】:
标签: c# xml serialization enums