C# XML序列化枚举类型属性
我正在尝试用XML序列化包含枚举属性的类。如果属性是使用特定的枚举声明的,那么它就可以正常工作。但是我需要属性的类型为Enum,这样我就可以将它设置为不同的Enum类型。然而,当我这样做时,我得到了一个例外 类型[namespace].Simple不能在此上下文中使用 我在枚举定义上尝试了不同的属性,但到目前为止还没有得到正确的结果。有办法做到这一点吗C# XML序列化枚举类型属性,c#,xml,serialization,enums,C#,Xml,Serialization,Enums,我正在尝试用XML序列化包含枚举属性的类。如果属性是使用特定的枚举声明的,那么它就可以正常工作。但是我需要属性的类型为Enum,这样我就可以将它设置为不同的Enum类型。然而,当我这样做时,我得到了一个例外 类型[namespace].Simple不能在此上下文中使用 我在枚举定义上尝试了不同的属性,但到目前为止还没有得到正确的结果。有办法做到这一点吗 public enum Simple : byte { one = 0x01, two = 0x02, three =
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();
}
}
Enum
是抽象的,无法序列化。中介绍了一种可能的解决方法
enum
的公共基元类型是int
(默认情况下,例如,也可以是byte或long)。因此,您也可以简单地使用这个整数基类型,比如在
Foo
类中使用byte-Simple
如果需要在xml中显示字符串表示形式(与枚举字段名相同),请将其公开为
string Simple
enum
是抽象的,无法序列化。中介绍了一种可能的解决方法
enum
的公共基元类型是int
(默认情况下,例如,也可以是byte或long)。因此,您也可以简单地使用这个整数基类型,比如在
Foo
类中使用byte-Simple
如果您需要字符串表示形式以xml形式出现(与枚举字段名相同),请将其公开为
字符串Simple
您可以尝试在要序列化的DataContract上设置EnumMember属性,以获取更具体的信息
您可以尝试在要序列化的DataContract上设置EnumMember attrubute,有关更具体的信息,请访问 基于作者关于将枚举拆分为两个字符串作为枚举类型和成员名称的想法,我提出了以下解决方案。该示例将对象从
Foo
转换为XML字符串,然后再次转换回新的Foo
对象
public enum SimpleEnum : byte
{
one = 0x01,
two = 0x02,
three = 0x03
}
public class Foo
{
private Enum _simple;
[XmlIgnore]
public Enum Simple
{
get { return _simple; }
set {
_simple = value;
var type = Simple.GetType();
var underlyingType = Enum.GetUnderlyingType(type);
EnumType = Simple.GetType().FullName;
EnumMember = Simple.ToString();
}
}
private string _enumType;
public string EnumType
{
get { return _enumType; }
set { _enumType = value; }
}
private string _enumMember;
public string EnumMember
{
get { return _enumMember; }
set {
_enumMember = value;
_simple = (Enum)Enum.Parse(Type.GetType(EnumType), EnumMember);
}
}
}
public class Program
{
static void Main(string[] args)
{
var str = new StringBuilder();
using (var writer = XmlWriter.Create(str))
{
try
{
var foo = new Foo
{
Simple = SimpleEnum.three
};
var serializer = new XmlSerializer(typeof(Foo));
serializer.Serialize(writer, foo);
Console.WriteLine(str.ToString());
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
using (TextReader reader = new StringReader(str.ToString()))
{
try
{
var serializer = new XmlSerializer(typeof(Foo));
var foo = (Foo)serializer.Deserialize(reader);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
Console.ReadLine();
}
}
根据作者关于将枚举拆分为两个字符串(枚举类型和成员名称)的想法,我提出了以下解决方案。该示例将对象从Foo
转换为XML字符串,然后再次转换回新的Foo
对象
public enum SimpleEnum : byte
{
one = 0x01,
two = 0x02,
three = 0x03
}
public class Foo
{
private Enum _simple;
[XmlIgnore]
public Enum Simple
{
get { return _simple; }
set {
_simple = value;
var type = Simple.GetType();
var underlyingType = Enum.GetUnderlyingType(type);
EnumType = Simple.GetType().FullName;
EnumMember = Simple.ToString();
}
}
private string _enumType;
public string EnumType
{
get { return _enumType; }
set { _enumType = value; }
}
private string _enumMember;
public string EnumMember
{
get { return _enumMember; }
set {
_enumMember = value;
_simple = (Enum)Enum.Parse(Type.GetType(EnumType), EnumMember);
}
}
}
public class Program
{
static void Main(string[] args)
{
var str = new StringBuilder();
using (var writer = XmlWriter.Create(str))
{
try
{
var foo = new Foo
{
Simple = SimpleEnum.three
};
var serializer = new XmlSerializer(typeof(Foo));
serializer.Serialize(writer, foo);
Console.WriteLine(str.ToString());
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
using (TextReader reader = new StringReader(str.ToString()))
{
try
{
var serializer = new XmlSerializer(typeof(Foo));
var foo = (Foo)serializer.Deserialize(reader);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
Console.ReadLine();
}
}
问题是不同枚举中的成员可以具有相同的值,即,我需要知道已序列化的枚举类型。在使用字符串时,是否可以在XAML中获取“[Enum].[Member]”,并在反序列化时使用它?我不这么认为。也许其他人有更好的想法,在本例中,我将在序列化中包含两个字段:一个是枚举类型名称,另一个是相应的字节/整数值。然后在实际的xml反序列化之后,通过like
Enum.Parse(value)
执行部分反序列化。应用@Asw的解决方案并将类型名称作为属性值的一部分也可以起作用。但这也不能使您免于一些定制的反序列化工作。这是一个有趣的想法。将调查…:-)问题是不同枚举中的成员可以具有相同的值,即,我需要知道已序列化的枚举类型。在使用字符串时,是否可以在XAML中获取“[Enum].[Member]”,并在反序列化时使用它?我不这么认为。也许其他人有更好的想法,在本例中,我将在序列化中包含两个字段:一个是枚举类型名称,另一个是相应的字节/整数值。然后在实际的xml反序列化之后,通过likeEnum.Parse(value)
执行部分反序列化。应用@Asw的解决方案并将类型名称作为属性值的一部分也可以起作用。但这也不能使您免于一些定制的反序列化工作。这是一个有趣的想法。将调查…:-)