如何实现通用 IEnumerable 或 IDictionary 以避免 CA1006?
出于好奇,我想知道如何最好地实现一个可用于避免 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,请注意,这是一个设计指南,而不是编译器错误。一种有效的方法是:忽略它。
另一种可能是——封装它;即返回一个
List
,其中QueryRow
是带有索引器的IDictionary
的浅层包装器,即,由于这是通过 dapper 访问的,因此填写方式为:
另一个选项(我不太喜欢)可能是: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>
, whereQueryRow
is a shallow wrapper over anIDictionary<string,object>
with an indexer, i.e.then, since this is being accessed via dapper, fill via:
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