我使用System.Reflection.Emit
以下MSDN 文档创建了一个类型
我使用以下代码创建我的类型和实例:
//following the tutorial I created a method which returns a dynamic type
Type myDynamicType = CreateNewObject("MyDynamicType", fields);
var instance = Activator.CreateInstance(myDynamicType);
现在我想用序列化我的对象XmlSerializer
试过这个:
FileStream fs = new FileStream(@"C:\Test\SerializedDynamic.XML", FileMode.Create);
XmlSerializer xs = new XmlSerializer(typeof(object));
xs.Serialize(fs, instance);
但它抛出一个异常:
"The type MyDynamicType was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically."
有什么帮助吗?