다음 코드가 주어질 때개체 필드에 Enum이 설정된 C# DataContractSerializer SerializationException
[DataContract]
public class TestClass
{
[DataMember]
public object _TestVariable;
public TestClass(object value)
{
_TestVariable = value;
}
public void Save()
{
using (XmlDictionaryWriter writer = XmlDictionaryWriter.CreateTextWriter(new FileStream("test.tmp", FileMode.Create)))
{
DataContractSerializer ser = new DataContractSerializer(typeof(TestClass));
ser.WriteObject(writer, this);
}
}
}
public enum MyEnum
{
One,
Two,
Three
}
_TestVariable
이 Enum 값으로 설정된 경우 직렬화에 실패하는 이유는 무엇입니까?
new TestClass(1).Save(); // Works
new TestClass("asdfqwer").Save(); // Works
new TestClass(DateTime.UtcNow).Save(); // Works
new TestClass(MyEnum.One).Save(); // Fails
throw되는 예외는 다음과 같습니다.
System.Runtime.Serialization.SerializationException : Type 'xxx.MyEnum' with data contract name 'xxx.MyEnum:http://schemas.datacontract.org/2004/07/Tests' is not expected. Consider using a DataContractResolver or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.
_TestVariable에'object type'을 사용하여 해결하려는 문제는 무엇입니까? 아마도 실제 문제를 설명 할 수 있습니다. –
비슷한 질문에 대한 답변보기 http://stackoverflow.com/a/8795410/52277 –