FxCop:将矩阵作为属性返回(C#) [英] FxCop: Returning Matrix as Property (C#)

查看:98
本文介绍了FxCop:将矩阵作为属性返回(C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个静态类'Defaults',该类应保存默认矩阵,这些默认矩阵转发到最后要求double[][]的接口.到目前为止,我只是将静态属性放在返回double[][] s的此类中.

现在要使其符合我们公司的编码标准,该代码必须符合FxCop的规则此处).

我想

足够好",所以我实现了该属性以返回IList<IList<double>>(尽管嵌套类型也很酷).但是,正如我所说,我需要使用的接口最终要求输入double[][]矩阵.我不知道如何在不显式转换回每个列表的情况下,将列表列表转换成数组 .当然可以,但是那样会产生大量的开销,特别是因为我什至不访问那些矩阵-我只会将它们传递给接口.

(PS:我知道,这是接口的问题,但目前我们无法更改.)

我发现使用ILists<IList<double>>确实无济于事,因为它违反了CA1819, which won't allow me to return a jagged array from a property like I did. And instead of arrays, I shall return IList or IEnumerable (as discussed here).

"Fair enough" I thought, so I implemented the property to return IList<IList<double>> (although nested types are uncool too). However, as I said, the interface which I need to work with asks for double[][] matrices in the end.. I have no idea how to get this list of lists into an array of arrays without explicitly converting back each list. Of course, I could, but that would create an insane amount of overhead, especially since I don't even access those matrices - I only pass them through to the interface.

(PS: I know, it's the Interface's fault, but at the moment we can't change that.)

Edit: I found out that using ILists<IList<double>> donesn't help anyway, since it violates CA1006. The easy solution that I took to make FxCop shut up was to make the properties internal. Anyway, the nicer solution is stated below. Alternatively, one may consider to use an indexed property, which is a bit messy in C# though.

解决方案

I suggest, you create a class Matrix<T> that accepts a T[][] into its constructor and can be converted to T[][] and make your properties in Defaults return a Matrix<double> instance. The conversion can either be implemented implicitly, explicitly or using a method.

这篇关于FxCop:将矩阵作为属性返回(C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆