关于字典的问题 [英] A question about dictionaries of Dictionaries
问题描述
这是我想做的事情:
here is what I would like to do:
Dictionary<string,Dictionary<string, XxXxXx>> BoB;
其中 XxXxXx 可以是A类,B类,C类...等
和{A,B,C}:Class Z
这样我就可以声明(大致为伪)
where XxXxXx can be class A, class B, class C...etc
and {A,B,C}:Class Z
so that i can declare (roughly-Pseudo)
class Rusty
{
private Dictionary<String,A> _a;
private Dictionary<String,B> _b;
private Dictionary<String,C> _c;
public Dictionary<String,????> Lookup( identifier id)
{
switch(id)
{
case id.a: return _a;
case id.b: return _b;
case id.c: return _c;}
}
}
}
但我能得到的最接近的结果是:
CS1502,CS0029,CS0039 X |
无法通过参考转换,装箱转换,拆箱转换,换行转换将类型"System.Collections.Generic.Dictionary< string,ClassA>"转换为"System.Collections.Generic.Dictionary< string,ClassZ>".或null类型转换
..基本上说我无法完成以上任务..
找不到这种类型的问题"的体面引用:confused:
有什么想法吗?
根据aspdevdotnet的建议(谢谢)
yet the closest I can get results in:
CS1502, CS0029, CS0039 X|
Cannot convert type ''System.Collections.Generic.Dictionary<string,ClassA>'' to ''System.Collections.Generic.Dictionary<string,ClassZ>'' via a reference conversion, boxing conversion, unboxing conversion, wrapping conversion, or null type conversion
..basically says that what i have above can''t be done..
couldn''t find a decent reference for this type of "issue":confused:
any ideas?
As per aspdevdotnet''s suggestion(thank you)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Jumba
{
class A:Z
{
}
class B:Z
{
}
class C:Z
{
}
class Z
{
}
class Y
{
Dictionary<string, Dictionary<string,Z>> Bob = new Dictionary<string,Dictionary<string,Z>>();
Dictionary<string, A> _a = new Dictionary<string,A>();
Dictionary<string, B> _b = new Dictionary<string,B>();
Dictionary<string, C> _c = new Dictionary<string,C>();
public Y()
{
//Error 4 Argument ''2'': cannot convert from ''System.Collections.Generic.Dictionary<string,Jumba.A>'' to ''System.Collections.Generic.Dictionary<string,Jumba.Z>''
Bob.Add("a",_a);
//Error 6 Argument ''2'': cannot convert from ''System.Collections.Generic.Dictionary<string,Jumba.A>'' to ''System.Collections.Generic.Dictionary<string,Jumba.Z>''
Bob.Add("b",_b);
//Error 8 Argument ''2'': cannot convert from ''System.Collections.Generic.Dictionary<string,Jumba.A>'' to ''System.Collections.Generic.Dictionary<string,Jumba.Z>''
Bob.Add("c",_c);
}
public Dictionary<string, Z> Retrieve(string str)
{
Object alpha = Bob[str];
//Error 15 Cannot implicitly convert type ''Jumba.A'' to ''System.Collections.Generic.Dictionary<string,Jumba.Z>''
if (alpha is A){return (alpha as A)}
}
}
}
推荐答案
关于您的更新样本.您似乎错过了重点.这就是您想要的:
Regarding your updated sample. You seem to have missed the point. This is what you want:
// Your dictionaries will be of value Z (so, A, B, and C instances can be placed in it).
Dictionary<string, Z> _a = new Dictionary<string,Z>();
Dictionary<string, Z> _b = new Dictionary<string,Z>();
Dictionary<string, Z> _c = new Dictionary<string,Z>();
// Your retrieve method will return a dictionary, not a Z derived instance.
public Dictionary<string, Z> Retrieve(string str)
{
return Bob[str];
}
或者,如果您想为此类的用户完成工作,则可以有3个Retrieve方法:
Or, you could have 3 Retrieve methods if you want to do the work for the user of this class:
public A RetrieveA(string str) { return Bob["a"][str] as A; }
public B RetrieveB(string str) { return Bob["b"][str] as B; }
public C RetrieveC(string str) { return Bob["c"][str] as C; }
由于C#是强类型的,因此您的类型存在冲突.您可以做的是让A,B和C类从som公共父类(例如AlphaClass)派生,然后创建该类型的字典.或者,由于所有类都是从Object类派生的,因此您可以只使用Object而不是AlphaClass:
Since C# is strongly typed, your types are clashing. What you can do is have classes A, B, and C derive from som common parent class (say, AlphaClass), then create a dictionary of that type. Or, since all classes derive from the Object class, you could just use Object instead of AlphaClass:
// A, B, and C should derive from AlphaClass.
Dictionary<string, Dictionary<string, AlphaClass>> BoB;
// Or, just use Object, since they already derive from that class.
Dictionary<string, Dictionary<string, Object>> BoB;
当您从字典中获得一项时,就可以检查其类型并将其转换为该类型:
When you get one of the items from the dictionary, you can then just check its type and convert it to that type:
Object alpha = BoB["A"]["someKey"];
if(alpha is A) (alpha as A).SomeAMethod();
这正是.Net中接口的作用
使一个接口代表所有类之间的共同点Ex
This exactly what the interfaces do in .Net
Make an interface represent the common stuff between all the classes Ex
public interface IInterface
{
int number{get; set;}
void doSomething();
}
然后在每个类中实现此接口
Then implement this interface in each class
public classA : IInterface
{
//implementation goes here
}
和调用步骤(如果只想检索对象)
And the calling step (if you want to retrieve the object only)
public IInterface Retrieve(string str)
{
return Bob[str];
}
或者,如果您想取消字典
Or of if you want to retireve the dictionary
public Dictionary<string,IInterface > Retrieve(string str)
{
return new Dictionary<string, IInterface>().Add(f, Bob[str]);
}
或使用更好的解决方案,使用Linq(我最喜欢的一种)
Or for better solution use Linq (my favourite one )
public Dictionary<string,IInterface> Retrieve(string str)
{
return Bob.Where(c => c.Key == str ).ToDictionary(c => c.Key, c => c.Value);
}
希望我能帮忙
hope i could help
这篇关于关于字典的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!