C#可能的通用类字段
我是C#(Java开发者)的新手,我想要一个类字段,它是一个通用列表,实际上它是一个列表字典:C#可能的通用类字段,c#,generics,C#,Generics,我是C#(Java开发者)的新手,我想要一个类字段,它是一个通用列表,实际上它是一个列表字典: protected IDictionary<String, IList<Object>> filters; 受保护的IDictionary过滤器; 我有一套 public void SetFilters(String key, params Object[] values) { if (key == null || values == null) {
protected IDictionary<String, IList<Object>> filters;
受保护的IDictionary过滤器;
我有一套
public void SetFilters(String key, params Object[] values) {
if (key == null || values == null) {
throw new ArgumentNullException("Must have filter name and values.");
}
if (filters == null) filters = new Dictionary<String, IList<Object>>();
IList<Object> fvalues = values.ToList();
filters.Add(key, fvalues);
}
public void SetFilters(字符串键,参数对象[]值){
如果(键==null | |值==null){
抛出新ArgumentNullException(“必须有筛选器名称和值”);
}
如果(filters==null)filters=newdictionary();
IList fvalues=values.ToList();
过滤器。添加(键、值);
}
但是,当我的代码尝试检索并将IList
强制转换回IList
或IList
时,我得到一个InvalidCastException
我想我会把这个列表列成通用的:
protected IDictionary<String, IList<T>> filters; //does not complile
protected IDictionary<String, IList<T>> filters where T: Object;//does not compile either
受保护的IDictionary过滤器//没有康普利尔
受保护的IDictionary筛选器,其中T:Object//也不编译
我无法使该类成为泛型类,因为字典将包含
字符串列表
或int
。在Java中,整数
和字符串
都是对象
,因此这不是IList的问题有帮助吗?我创建了一个新的泛型类
internal class Program
{
private class Reed<T>
{
private IDictionary<String, IList<T>> filters;
public void SetFilters(String key, params T[] values)
{
if (key == null || values == null)
{
throw new ArgumentNullException("Must have filter name and values.");
}
if (filters == null)
filters = new Dictionary<String, IList<T>>();
IList<T> fvalues = values.ToList();
filters.Add(key, fvalues);
}
}
private static void Main(string[] args)
{
var r1 = new Reed<string>();
r1.SetFilters("test", "one", "two", "three");
var r2 = new Reed<int>();
r2.SetFilters("test", 1, 2, 3);
}
}
内部类程序
{
私家级芦苇
{
专用索引过滤器;
公共void SetFilters(字符串键,参数T[]值)
{
如果(键==null | |值==null)
{
抛出新ArgumentNullException(“必须有筛选器名称和值”);
}
if(过滤器==null)
过滤器=新字典();
IList fvalues=values.ToList();
过滤器。添加(键、值);
}
}
私有静态void Main(字符串[]args)
{
var r1=新簧片();
r1.设置过滤器(“测试”、“一”、“二”、“三”);
var r2=新簧片();
r2.设置过滤器(“测试”,1、2、3);
}
}
您可以使用System.Collection.IList
,类似于此:
public class Foo
{
public IDictionary<String, IList> filters;
public void SetFilters(String key, params object[] values)
{
if (key == null || values == null)
{
throw new ArgumentNullException("Must have filter name and values.");
}
if (filters == null)
{
filters = new Dictionary<String, IList>();
}
IList fvalues = values.ToList();
filters.Add(key, fvalues);
}
}
在访问和使用每个列表类型时,仍然存在将其转换为预期类型的问题
-使用IList
为了避免错误转换,首先需要创建一个类型化列表。要做到这一点,您应该使用泛型
对于字典项类型,可以使用IList或object,但需要将强类型列表存储为项值
如果将SetFilters设置为泛型方法,那么它可以创建一个正确类型的列表以存储在字典中。我包括了一个GetFilters方法,它返回与键匹配的列表。i、 e.values.ToList()将创建一个列表
公共类筛选器管理器
{
受保护的IDictionary筛选器=新字典();
公共void SetFilters(字符串键,参数T[]值)
{
如果(键==null | |值==null)
{
抛出新ArgumentNullException(“必须有筛选器名称和值”);
}
IList fvalues=values.ToList();
过滤器。添加(键、值);
}
公共IList GetFilters(字符串键)
{
返回(IList)过滤器[键];
}
}
这样说吧
var filterManager = new FilterManager();
filterManager.SetFilters("MyIntegerFilters", 3, 4, 5);
filterManager.SetFilters("MyStringFilters", "A", "B", "C");
var intFilters = filterManager.GetFilters<int>("MyIntegerFilters");
var stringFilters = filterManager.GetFilters<string>("MyStringFilters");
var filterManager=new filterManager();
SetFilters(“MyIntegerFilters”,3,4,5);
SetFilters(“MyStringFilters”、“A”、“B”、“C”);
var intFilters=filterManager.GetFilters(“MyIntegerFilters”);
var stringFilters=filterManager.GetFilters(“MyStringFilters”);
如果你打电话,你会得到一个例外
var filters = filterManager.GetFilters<int>("MyStringFilters");
var filters=filterManager.GetFilters(“MyStringFilters”);
因为它将尝试将列表
转换为列表
,所以只需使用非泛型列表,IDictionary filters
。您可能需要强制转换:您可以将列表设置为泛型,但也必须将包含的类设置为泛型类型。因此要使用受保护的IDictionary过滤器
您还需要公共类MyClass
。您的代码对IList
执行强制转换的功能是什么?它如何知道做string
而不是int
?在这里,如果能看到更多的大局图来帮助您,那将是非常有用的。在没有泛型的情况下使用IList是可行的。虽然我仍在想,在将来的情况下如何使用它。这种设计允许字典只保存相同类型的列表。如果我理解正确的话,这个要求似乎是一本字典应该能够保存任何类型的列表。i、 e:filters.add('key1',new List())
以及filters.add('key2',new List())
我正在尝试让字典中的键包含不同类型的列表。例如,SetFilter(“名称”、“名称1”、“名称2”)代码>和SetFilter(“idnumbers”,2,3)代码>使其非泛型有帮助。这看起来很有希望;但是,从GetFilters()返回/转换语句引发异常:[System.InvalidCastException:无法将类型为'System.Collections.Generic.List'的对象强制转换为'System.Collections.Generic.IList
1[System.String].]的类型。是否使用类型为object
的变量作为参数?您应该使用int或string类型的变量。如果仅使用int类型的参数(第一个字符串键参数除外),则应创建int类型的列表,如果仅使用string
类型的参数,则应创建字符串列表。您还可以尝试显式为SetFilters指定该类型filterManager.SetFilters(“MyIntegerFilters”,3,4,5)
I似乎缺少将IList转换为“Key2”的IList(或IList)的方法。我得到一个演员特例。非常酷的dotnetfiddle.net!是我在Grax类似答案的帮助下尝试的代码。
var filterManager = new FilterManager();
filterManager.SetFilters("MyIntegerFilters", 3, 4, 5);
filterManager.SetFilters("MyStringFilters", "A", "B", "C");
var intFilters = filterManager.GetFilters<int>("MyIntegerFilters");
var stringFilters = filterManager.GetFilters<string>("MyStringFilters");
var filters = filterManager.GetFilters<int>("MyStringFilters");