ユーザーは、XMLでEnum型を指定できるアプリケーションを設計しようとしています。そのアプリケーションから、特定のメソッドを実行しますその列挙型(辞書を使用して)。私はXMLのEnum部分にハングアップしています。異なるEnum型のクラスを直列化するEnum型
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 }
}
そして例外でTestClassを結果をシリアル化しようとしている:
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.
私のシリアル化の方法は、次のようになります。
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;
}
}
私はMethodTypeでいくつかのチェック/鋳造を含む試してみましたゲッターが、これは同じエラーになります。
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