我有一个Dictionary<string,K>
where K 是通过反射加载的类型,我不能命名 K。
不幸的是,我无法弄清楚我应该如何使用该TryGetValue
方法。我尝试了几种不同的方法,它们都导致异常。我该怎么办?
dynamic dict = GetDictThroughMagic();
dynamic d;
bool hasValue = dict.TryGetValue("name",out d);
bool hasValue = dict.TryGetValue("name",d);
我可以写得更详细if(dict.Contains("name")) d=dict["name"]
但如果我可以编写更简洁的 TryGetValue 方法,我会更喜欢。
更新以包括实际异常:
Unhandled Exception: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: The
best overloaded method match for 'System.Collections.Generic.Dictionary<string,K>
.TryGetValue(string, out K)' has some invalid arguments
at CallSite.Target(Closure , CallSite , Object , String , Object& )