FXCop / 对带有数组的 WCF DataContract 类进行代码分析 - 创建 CA1819 错误

发布于 2024-10-31 03:07:19 字数 627 浏览 5 评论 0原文

我有以下 WCF 数据协定类:

[DataContract]
public class BinaryResponse : ResponseBase
{
    [DataMember]
    public byte[] Payload { get; set; }
}

很好且简单,完全按照我的需要工作。不过,我现在正在通过完整的代码分析规则集来运行它。这会生成以下警告:

CA1819 : Microsoft.Performance : Change 'BinaryResponse.Payload' to return a collection or make it a method.

查看了 此错误的帮助页面 解决方案很简单。然而,该解决方案并不真正适合 WCF 数据成员。

所以问题是,如何重构此类,使其仍可用作 WCF 数据契约并通过代码分析?

干杯

I have the following WCF data contract class:

[DataContract]
public class BinaryResponse : ResponseBase
{
    [DataMember]
    public byte[] Payload { get; set; }
}

Nice and simple, works exactly as I need it to. However I am now running this through the full code analysis ruleset. This generates the following warning:

CA1819 : Microsoft.Performance : Change 'BinaryResponse.Payload' to return a collection or make it a method.

Having looked at the help page for this error the solution is a simple one. However the solution doesn't really fit with WCF datamembers.

So the question is, how can I refactor this class to still be useable as a WCF datacontract and also pass the code analysis?

Cheers

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

追星践月 2024-11-07 03:07:19

您可以将字节数组更改为可枚举或集合,如下所示:

[DataContract]
public class BinaryResponse : ResponseBase
{
    [DataMember] public ICollection<byte> Payload { get; set; }
}

或者您可以将 byte[] 属性保留在私有数组中并将其包装在 ICollection 属性中(如果您需要内部数组):

[DataContract]
public class BinaryResponse : ResponseBase
{
    // this is NOT a member of the DataContract
    private byte[] payload;

    [DataMember] public ICollection<byte> Payload {
        get { return this.payload; }
        set { this.payload = value.toArray(); }
    }
}

You can do change the byte array to an enumerable or collection, like tehe following:

[DataContract]
public class BinaryResponse : ResponseBase
{
    [DataMember] public ICollection<byte> Payload { get; set; }
}

or you can keep the byte[] attribute in a private array and wrap it in an ICollection property (if you need the internal array):

[DataContract]
public class BinaryResponse : ResponseBase
{
    // this is NOT a member of the DataContract
    private byte[] payload;

    [DataMember] public ICollection<byte> Payload {
        get { return this.payload; }
        set { this.payload = value.toArray(); }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文