如何从 XML 序列化程序集中排除 class

How to exclude a class from the XML serialization assembly

我有一个 C# 项目,我必须在其中激活 XML 序列化程序集生成(csproj 中的 GenerateSerializationAssemblies)。

该项目包含一个派生自 System.ComponentModel.Composition.ExportAttribute 的 class。

[AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = false)]
public class MyExportAttribute : ExportAttribute
{ ... }

编译器失败并报错 public 属性 setter on ExportAttribute.ContractName:

Error 10 Cannot deserialize type 'System.ComponentModel.Composition.ExportAttribute' because it contains property 'ContractName' which has no public setter. 

实际上我不想序列化这个 class,所以我想将它从序列化程序集中排除。我可以这样做吗?或者,指定要包括哪些 classes?

到目前为止我尝试/想到的:

为了解决此错误,我在 class 上实现了 IXmlSerializable,但出现了 sgen 问题。我通过抛出 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");
}