我有一个C#项目,我必须激活XML序列化程序集生成(csproj中的GenerateSerializationAssemblies)。
该项目包含一个派生自System.ComponentModel.Composition.ExportAttribute的类。
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = false)]
public class MyExportAttribute : ExportAttribute
{ ... }
编译器失败,错误地抱怨ExportAttribute.ContractName上缺少公共属性设置器:
Error 10 Cannot deserialize type 'System.ComponentModel.Composition.ExportAttribute' because it contains property 'ContractName' which has no public setter.
实际上我不想序列化这个类,所以我想将它从序列化程序集中排除。我能这样做吗?或者,指定要包含哪些类?
到目前为止我尝试/想过的事情:
答案 0 :(得分:1)
要解决此错误,我在产生sgen
问题的类上实现了IXmlSerializable
。我通过抛出NotImplementedException
实现了每个必需的成员:
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = false)]
public class MyExportAttribute
: ExportAttribute
// Necessary to prevent sgen.exe from exploding since we are
// a public type with a parameterless constructor.
, System.Xml.Serialization.IXmlSerializable
{
System.Xml.Schema.XmlSchema System.Xml.Serialization.IXmlSerializable.GetSchema() => throw new NotImplementedException("Not serializable");
void System.Xml.Serialization.IXmlSerializable.ReadXml(System.Xml.XmlReader reader) => throw new NotImplementedException("Not serializable");
void System.Xml.Serialization.IXmlSerializable.WriteXml(System.Xml.XmlWriter writer) => throw new NotImplementedException("Not serializable");
}