是否可以在C#中组合多个属性?

时间:2018-07-17 18:45:52

标签: c# xml attributes xmlserializer

我正在使用.NET XmlSerializer。为了正确地序列化从另一个继承的多个类,我必须在所有使用它们的地方都包括名称以及子类的类型。就我而言,我有一组表示表达式的类。但是到处都有很多代码被复制。有没有办法解决这个问题:

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; }
}

对于这样的事情:

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

或者这个:

public class ClassThatHasExpression
{
    public Expression expression { get; set; }
}

0 个答案:

没有答案