如果有:<代码>的基本类型 IDictionary不执行IDictionary<string,active>
, 您不能投标,期限。 如果是的话,则通过<代码>(IDictionary<string, Dynamic>) 在当地投放。 Var 就足够了。
如果没有,你可以做两件事:
- Copy the
IDictionary
to your generic type.
- Build an Adapter class that accepts the
IDictionary
as a dependency and implements the generic IDictionary
you want, mapping calls from one to the other.
<><>Edit>: 刚贴出的样本代码将复印字典,无论何时打到!。 我将在某个时候再次提出某些建议守则。
<><>>
您的样本代码法作为复制数据的手段是坚实的,但复制件应当贴在册,或重新复制时间。 d 我建议你将实际翻译法作为单独的方法,并要求从你的财产中第一次使用。 例如:
private IDictionary dataLayerProperties;
private IDictionary<string, dynamic> translatedProperties = null;
protected virtual IDictionary<string, dynamic> Properties
{
if(translatedProperties == null)
{
translatedProperties = TranslateDictionary(dataLayerProperties);
}
return translatedProperties;
}
public IDictionary<string, dynamic> TranslateDictionary(IDictionary values)
{
return values.Keys.Cast<string>().ToDictionary(key=>key, key => values[key] as dynamic);
}
现在,这种做法显然有合意......如果需要更新数据的话? 你们必须把翻译作品重新开销,等等。
<><>>>
这是我的首选做法。
public class TranslatedDictionary : IDictionary<string, dynamic>
{
private IDictionary Original = null;
public TranslatedDictionary(IDictionary original)
{
Original = original;
}
public ICollection<string> Keys
{
get
{
return Original.Keys.Cast<string>().ToList();
}
}
public dynamic this[string key]
{
get
{
return Original[key] as dynamic;
}
set
{
Original[key] = value;
}
}
// and so forth, for each method of IDictionary<string, dynamic>
}
//elsewhere, using your original property and field names:
Properties = new TranslatedDictionary(properties);
现在,这种做法显然也有同感,最明显的是<代码>。 钥匙 (and Value and any others that Return ICollection
on IDictionary
has to re a new range for each calls. 但是,这仍然允许采取最灵活的做法,因为它确保了数据始终如一。