如何实现通用 IEnumerable 或 IDictionary 以避免 CA1006?

发布于 2024-12-15 19:13:37 字数 1271 浏览 0 评论 0原文

出于好奇,我想知道如何最好地实现一个可用于避免 CA1006 警告的类

CA1006:Microsoft.Design:考虑一种设计,其中“IReader.Query(String, String)”不嵌套泛型类型“IList(Of IDictionary(Of String, Object))”。

这是返回泛型类型的方法

public virtual IList<IDictionary<string, object>> Query(
    string fullFileName, 
    string sheetName)
{
    using (var connection = new OdbcConnection(
        this.GetOdbcConnectionString(fullFileName)))
    {
        connection.Open();
        return connection
            .Query(string.Format(
                CultureInfo.InvariantCulture,
                SystemResources.ExcelReader_Query_select_top_128___from__0_,
                sheetName))
            .Cast<IDictionary<string, object>>()
            .ToList();
    }
}

类似于

SourceData<T, U> Query(string fullFileName, string sheetName)
SourceData Query(string fullFileName, string sheetName)

编辑:

按照 Marc 的建议,我将嵌套泛型封装在此类中

public class QueryRow : List<KeyValuePair<string, object>>
{
    protected internal QueryRow(IEnumerable<KeyValuePair<string, object>> dictionary)
    {
        this.AddRange(dictionary.Select(kvp => kvp));
    }
}

Out of curiosity i would like to know how to best implement a class that could be used to avoid the CA1006 warning

CA1006 : Microsoft.Design : Consider a design where 'IReader.Query(String, String)' doesn't nest generic type 'IList(Of IDictionary(Of String, Object))'.

This is the method that returns the generic type

public virtual IList<IDictionary<string, object>> Query(
    string fullFileName, 
    string sheetName)
{
    using (var connection = new OdbcConnection(
        this.GetOdbcConnectionString(fullFileName)))
    {
        connection.Open();
        return connection
            .Query(string.Format(
                CultureInfo.InvariantCulture,
                SystemResources.ExcelReader_Query_select_top_128___from__0_,
                sheetName))
            .Cast<IDictionary<string, object>>()
            .ToList();
    }
}

Something like

SourceData<T, U> Query(string fullFileName, string sheetName)
SourceData Query(string fullFileName, string sheetName)

EDIT:

Following Marc's suggestions I encapsulated the nested generic in this class

public class QueryRow : List<KeyValuePair<string, object>>
{
    protected internal QueryRow(IEnumerable<KeyValuePair<string, object>> dictionary)
    {
        this.AddRange(dictionary.Select(kvp => kvp));
    }
}

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

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

发布评论

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

评论(1

三生殊途 2024-12-22 19:13:37

首先,请注意,这是一个设计指南,而不是编译器错误。一种有效的方法是:忽略它。

另一种可能是——封装它;即返回一个 List,其中 QueryRow 是带有索引器的 IDictionary 的浅层包装器,即

public class QueryRow {
    private readonly IDictionary<string,object> values;
    internal QueryRow(IDictionary<string,object> values) {
        this.values = values;
    }
    public object this[string key] {
        get { return values[key]; }
        set { values[key] = value; }
    }
}

,由于这是通过 dapper 访问的,因此填写方式为:

var data = connection.Query(....)
        .Select(x => new QueryRow((IDictionary<string,object>)x).ToList()

另一个选项(我不太喜欢)可能是:return DataTable

输入 DataTable 后就去洗手了……啊!现在两次了

Firstly, note that it is a design guideline, not a compiler error. One valid approach here would be: ignore it.

Another might be - encapsulate it; i.e. return a List<QueryRow>, where QueryRow is a shallow wrapper over an IDictionary<string,object> with an indexer, i.e.

public class QueryRow {
    private readonly IDictionary<string,object> values;
    internal QueryRow(IDictionary<string,object> values) {
        this.values = values;
    }
    public object this[string key] {
        get { return values[key]; }
        set { values[key] = value; }
    }
}

then, since this is being accessed via dapper, fill via:

var data = connection.Query(....)
        .Select(x => new QueryRow((IDictionary<string,object>)x).ToList()

Another option (that I'm not hugely fond of), might be: return DataTable.

goes off to wash his hands after typing DataTable... gah! twice now

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文