사용자가 XML에서 열거 형을 지정할 수 있도록 응용 프로그램을 디자인하려고합니다. 그리고이 응용 프로그램은 연결된 특정 메서드를 실행합니다. 그 enum (사전을 사용). 나는 XML의 열거 형 부분에 매달려있다.다른 수있는 일반 열거 형 클래스 직렬화 열거 형
이string p = "C:\\testclass.xml";
TESTCLASS testclass = new TESTCLASS(Enummies.BigMethods.BIG_ONE);
TestSerializer<TESTCLASS>.Serialize(p, testclass);
System.InvalidOperationException: The type Enummies+BigMethods may not be used in this context.
내 직렬화 방법은 다음과 같습니다 : 예외에 TestClass에 결과를 직렬화하려고
public class TESTCLASS
{
private Enum _MethodType;
[XmlElement(Order = 1, ElementName = "MethodType")]
public Enum MethodType
{
get { return _MethodType; }
set { _MethodType = value; }
}
public TESTCLASS() { }
public TESTCLASS(Enummies.BigMethods bigM)
{
MethodType = bigM;
}
public TESTCLASS(Enummies.SmallMethods smallM)
{
MethodType = smallM;
}
}
public class Enummies
{
public enum BigMethods { BIG_ONE, BIG_TWO, BIG_THREE }
public enum SmallMethods { SMALL_ONE, SMALL_TWO, SMALL_THREE }
}
그리고 나는 MethodType에 캐스팅/몇 가지 검사를 포함하여 시도
public class TestSerializer<T> where T: class
{
public static void Serialize(string path, T type)
{
var serializer = new XmlSerializer(type.GetType());
using (var writer = new FileStream(path, FileMode.Create))
{
serializer.Serialize(writer, type);
}
}
public static T Deserialize(string path)
{
T type;
var serializer = new XmlSerializer(typeof(T));
using (var reader = XmlReader.Create(path))
{
type = serializer.Deserialize(reader) as T;
}
return type;
}
}
Getter,하지만 같은 오류가 발생합니다.
public Enum MethodType
{
get
{
if (_MethodType is Enummies.BigMethods) return (Enummies.BigMethods)_MethodType;
if (_MethodType is Enummies.SmallMethods) return (Enummies.SmallMethods)_MethodType;
throw new Exception("UNKNOWN ENUMMIES TYPE");
}
set { _MethodType = value; }
}
어떤 열거 형이 존재하는지 미리 알고 있습니까? – dbc