2

I have a C# project where I have to activate XML serialization assembly generation (GenerateSerializationAssemblies in csproj).

The project contains a class that is derived from System.ComponentModel.Composition.ExportAttribute.

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

The compiler fails with an error complaining about a missing public property setter on ExportAttribute.ContractName:

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

Actually I do not want to serialize this class, so I'd like to exclude it from the serialization assembly. Can I do that? Or alternatively, specify which classes to include?

What I've tried / thought of so far:

  • Hide the ContractName property (non virtual) in MyExportAttribute with an empty setter, call the base implementation in the getter -> same error, serializer still wants to access the properties on the base class
  • Applying XmlIgnore to that MyExportAttribute.ContractName did not help either
  • Moving classes to other projects is an option, but I'd like to avoid that if possible
  • XmlIgnore on the ContractName property would solve my problem, but of course I cannot add it to ExportAttribute. Is there a similar XML serialization control attribute that can be applied to a class, so that it's ignored by the serializer?
dzs
  • 413
  • 4
  • 9
  • 2
    I currently see two options: a) explicitly specify the types you want to be included in the serialization assembly using the `/t`switch of [`sgen.exe`](https://msdn.microsoft.com/en-us/library/bk3w6240(v=vs.110).aspx), or b) move the type(s) that you don't want to include to another assembly. I'm not aware of any attributes that would exclude the type from the generated code. -- Or, another option that you have is to have a look at [XGenPlus](http://www.codeproject.com/Articles/21278/XGenPlus-A-Flexible-Tool-to-Generate-Typed-XML-Ser) – Dirk Vollmar Aug 25 '16 at 10:57
  • Thanks, option a) is something I was looking for. If I'm right I cannot do this in conjunction with the GenerateSerializationAssemblies csproj element, but I can add the sgen command line as a postbuild action. – dzs Aug 26 '16 at 11:31

1 Answers1

1

To work around this error, I implemented IXmlSerializable on the class that gave sgen issues. I implemented each required member by throwing 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");
}
binki
  • 7,754
  • 5
  • 64
  • 110