私はいくつかの辞書を持って指定されたコレクションの参照を取得します。私は、型を与えられた辞書の参照を返すメソッドが必要です。追加またはその辞書に削除するようにその後、私はいくつかの操作を実行します:タイプ
注:私はこのような何かを書くことができDictionary<int, BaseType>
としての私の辞書を設定したくないことはないだろう辞書への参照を返す:
Dictionary<int, BaseType> GetDictionary(BaseType myObject)
{
var dic = new Dictionary<int, BaseType>();
if(myObject is Type1)
{
//ideally I would return my Type1Dictionary here but I can't due type incompatibility
foreach(var x in Type1Dictionary)
{
dic.Add(x.Key, x.Value);
}
return dic;
}
if(myObject is Type2) { /*...*/ }
if(myObject is Type3) { /*...*/ }
if(myObject is Type4) { /*...*/ }
}
EDIT:
私が本当にしたいことは、次の構造を避けるためです:
AddObject(BaseType x)
{
Type1 x1 = x as Type1;
if(x1 != null) { Type1Dictionary.Add(x1.ID, x1); }
Type2 x2 = x as Type2;
if(x2 != null) { Type2Dictionary.Add(x2.ID, x2); }
Type3 x3 = x as Type3;
if(x3 != null) { Type3Dictionary.Add(x3.ID, x3); }
Type4 x4 = x as Type4;
if(x4 != null) { Type4Dictionary.Add(x4.ID, x4); }
}
RemoveObject(BaseType x)
{
Type1 x1 = x as Type1;
if(x1 != null) { Type1Dictionary.Remove(x1.ID); }
Type2 x2 = x as Type2;
if(x2 != null) { Type2Dictionary.Remove(x2.ID); }
Type3 x3 = x as Type3;
if(x3 != null) { Type3Dictionary.Remove(x3.ID); }
Type4 x4 = x as Type4;
if(x4 != null) { Type4Dictionary.Remove(x4.ID); }
}
しかし、その代わりに:
AddObject(BaseType x)
{
var dic = GetDictionary(x);
dic.Add(x.ID, x);
}
RemoveObject(BaseType x)
{
var dic = GetDictionary(x);
dic.Remove(x.ID);
}
'int'から' BaseType'にマップする辞書は必要ありませんが、同じことをする辞書を返しますか? –
私は 'Type2'オブジェクトが' Type1'と同じ辞書に入ることを望ましくありません。しかし、オブジェクトを削除または追加するためには、辞書がどのように扱われているかは気にしません(正しいタイプの場合)。私が書いたコードは私の問題を解決しません、btw。 – Sturm
訪問者パターンの候補のように聞こえますが、「ダイナミック」を使用して問題をDLRに移動してください。 –