我希望有人可以帮助我解决我在使用 XmlSerializer 时遇到的这个问题。
我得到的错误是:
System.InvalidOperationException:无法生成临时类(结果 = 1)。错误 CS0012:“System.Data.Objects.DataClasses.EntityObject”类型在未引用的程序集中定义。您必须添加对程序集“System.Data.Entity,Version=3.5.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089”的引用。
我确保我的单元测试引用了 System.Data.Entity,因此它至少能够编译。我还在 app.config 中将程序集绑定到 System.Data.Entity。
这是我粗略的班级结构
[Serializable]
[XmlRoot(Namespace = XmlSupport.MyNamespace, ElementName = XmlSupport.WantToSerialize)]
[XmlInclude(typeof(WantToSerializeBaseClass)]
[XmlInclude(typeof(EntityObject)]
[XmlInclude(typeof(MyEntityObjectSubClass)]
public class WantToSerialize : WantToSerializeBaseClass, IXmlSerializable (I've tried putting this on the baseclass and the current class)
{
// methods and classes
// I've included XmlIncludes for all the classes that this class has a reference too
// even though in the WriteXml it just uses .NET base classes
}
WantToSerializeBaseClass 使用了一些泛型,但我用 XmlIncludes 修饰了它(EntityObject,以及它引用的任何其他类)。
调用代码:
var serializerWrite = new XmlSerializer(typeof (WantToSerialize), XmlSupport.ITNNamespace);
失败
但是,如果我这样做:
var serializerWrite = new XmlSerializer(typeof (WantToSerialize), new Type[] {typeof(EntityObject)});
它是成功的。
任何想法都是最有帮助的。
更新 我已经将问题追踪到 WantToSerializeBaseClass 中的一个方法
public abstract void ConvertFromEntity<TtoCopy>(TtoCopy toCopy) where TtoCopy : MyEntityObjectSubClass;
其中 MyEntityObjectSubClass 是 EntityObject 的子类,它在我的实体对象上添加了一些我想要的方法。MyEntityObjectSubClass 如下所示:
[Serializable]
[XmlInclude(typeof(EntityObject))]
public abstract class MyEntityObjectSubClass : EntityObject, IMyEntityObjectSubClass
再一次,任何想法都会很棒