如何从DataContract类中的KnownType中删除type属性?

时间:2018-01-31 01:28:41

标签: c# xml wcf datacontractserializer datacontract

是否可以使用KnownType的类名作为标记,而不是父类? e.g。

这是一个较大的DataContract xml文件

的摘录
[DataContract (Namespace = "projectNamespace")]
[KnownType(typeof(Warning)), KnownType(typeof(Error))]
public abstract class Message
{
    protected Message(LogLevel logLevel, int percentUsed, string message)
    {
        this.LogLevel = logLevel;
        this.LogMessage = message;
        this.Percent = percentUsed;
    }

    internal readonly LogLevel LogLevel;
    [DataMember] internal readonly int Percent;    
    [DataMember] internal readonly string LogMessage;
}

[DataContract (Name="Warning", Namespace = "projectNamespace")]
internal sealed class Warning : Message
{
    public Warning(int percentUsed, string message) : base(LogLevel.Warn, percentUsed, message)
    {
    }
}

[DataContract(Name = "Error", Namespace = "projectNamespace")]
internal sealed class Error : Message
{
    public Error(int percentUsed, string message) : base(LogLevel.Error, percentUsed, message)
    {
    }
}

public enum LogLevel
{
    Warn, Error
}

我看到了什么:

<messages>
  <Message i:type="Warning">
    <PercentUsed>75</PercentUsed>
    <LogMessage>75% used</LogMessage>
  </Message>
  <Message i:type="Error">
    <PercentUsed>100</PercentUsed>
    <LogMessage>0% remaining</LogMessage>
  </Message>
</messages>

预期:

<messages>
  <Warning>
    <PercentUsed>75</PercentUsed>
    <LogMessage>75% used</LogMessage>
  </Warning>
  <Error>
    <PercentUsed>100</PercentUsed>
    <LogMessage>0% remaining</LogMessage>
  </Error>
</messages>

请注意消息如何令人烦恼i:type="Error",而不仅仅是<Error>

0 个答案:

没有答案