4

I'm working with .NET XmlSerializer. In order to properly serialize multiple classes that inherit from another one I must include the name along with the type of the subclass everywhere a use them. In my case I have a group of classes that represent expressions. But is a lot of code replicated everywhere. Is there a way to go from this:

public class ClassThatHasExpression
{
    [XmlElement("equal", typeof(Equals)),
     XmlElement("equals", typeof(Equals))]
    [XmlElement("notequal", typeof(NotEquals)),
     XmlElement("notequals", typeof(NotEquals)),
     XmlElement("diff", typeof(NotEquals))]
    [XmlElement("multiequals", typeof(MultiEquals))]
    [XmlElement("in", typeof(In))]
    [XmlElement("isnull", typeof(IsNull))]
    [XmlElement("notnull", typeof(NotNull))]
    [XmlElement("true", typeof(True))]
    [XmlElement("false", typeof(False))]
    [XmlElement("neg", typeof(Not)),
     XmlElement("not", typeof(Not))]
    [XmlElement("equiv", typeof(Equiv))]
    [XmlElement("inequiv", typeof(InEquiv)),
     XmlElement("xor", typeof(InEquiv))]
    [XmlElement("and", typeof(And))]
    [XmlElement("or", typeof(Or))]
    [XmlElement("imp", typeof(Implication))]
    public object expression { get; set; }
}

To something like this:

public class ClassThatHasExpression
{
    [IsExpression]
    public object expression { get; set; }
}

Or this:

public class ClassThatHasExpression
{
    public Expression expression { get; set; }
}
  • [Take a look here](https://stackoverflow.com/questions/38503146/combining-multiple-attributes-to-a-single-attribute) – Chronicle Jul 17 '18 at 18:51
  • @Chronicle is there a native and simpler way to do it? – Santiago Cabrera Jul 17 '18 at 19:13
  • That is the native way. If you want a simpler way, you can try making an interface with these properties, then add all the XmlElement attributes above the properties in the interface. Then extend the interface in all the classes where you use it. This should be possible because XmlElement is an inherited attribute. This way you only have to write it once. – Chronicle Jul 17 '18 at 19:18
  • That's a perfect and clear solution. Thanks! – Santiago Cabrera Jul 17 '18 at 19:24
  • @Chronicle didn't notice that actually by using an interface the class implementing it is not inheriting the attributes. Tried the example in your answer but is not working. – Santiago Cabrera Jul 17 '18 at 20:54
  • Looks like attributes cannot be inherited from interfaces, unless you try a workaround using methods [described here](https://stackoverflow.com/questions/540749/can-a-c-sharp-class-inherit-attributes-from-its-interface). The original link I posted might still be a better idea – Chronicle Jul 18 '18 at 04:35
  • Another option would be to inherit from a regular class instead of an interface. – Chronicle Jul 18 '18 at 15:07
  • True, but I'd like to have a common class Expression. In that case it would be impossible to have an expression that have another expression inside, because I'd have to inherit from the expression class and "HasExpression" class. – Santiago Cabrera Jul 23 '18 at 19:39

0 Answers0