如何从 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?
到目前为止我尝试/想到的:
- 在 MyExportAttribute 中用空 setter 隐藏 ContractName 属性(非虚拟),在 getter 中调用基本实现 -> 同样的错误,序列化器仍然想要访问基础上的属性 class
- 对那个 MyExportAttribute.ContractName 应用 XmlIgnore 也没有帮助
- 将 classes 转移到其他项目是一种选择,但我想尽可能避免这种情况
- ContractName 属性 上的 XmlIgnore 可以解决我的问题,但我当然不能将它添加到 ExportAttribute。是否有类似的 XML 序列化控制属性可以应用于 class,以便它被序列化程序忽略?
为了解决此错误,我在 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");
}
我有一个 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?
到目前为止我尝试/想到的:
- 在 MyExportAttribute 中用空 setter 隐藏 ContractName 属性(非虚拟),在 getter 中调用基本实现 -> 同样的错误,序列化器仍然想要访问基础上的属性 class
- 对那个 MyExportAttribute.ContractName 应用 XmlIgnore 也没有帮助
- 将 classes 转移到其他项目是一种选择,但我想尽可能避免这种情况
- ContractName 属性 上的 XmlIgnore 可以解决我的问题,但我当然不能将它添加到 ExportAttribute。是否有类似的 XML 序列化控制属性可以应用于 class,以便它被序列化程序忽略?
为了解决此错误,我在 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");
}