【发布时间】:2014-12-19 03:13:44
【问题描述】:
我正在构建一个UITypeEditor(从属性网格启动)来编辑一个Dictionary<T,T>,其中T可以是任何标量类型(int、long、double、string 、DateTime 等)。
将要编辑的字典作为名为 innerobject 的 object 传递到控件中。我得到的类型和键值类型如下:
Type t = innerobject.GetType();
Type[] member_t = innerobject.GetType().GetGenericArguments();
if(member_t.Length !=2)
return null;
var keyconverter = TypeDescriptor.GetConverter(member_t[0]);
var valueconverter = TypeDescriptor.GetConverter(member_t[1]);
if (null == keyconverter || null == valueconverter)
return null;
var dic = Activator.CreateInstance(t);
dynamic dyndic = dic;
稍后当我尝试为其添加值时,我执行以下操作(s 是文本框中的一行):
string[] str = s.Split(new char[] { ',', ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
if (str.Length == 2)
{
object a = keyconverter.ConvertFromString(str[0]);
object b = valueconverter.ConvertFromString(str[1]);
dyndic[a] = b;
}
此时抛出Microsoft.CSharp.RuntimeBinder.RuntimeBinderException。
A first chance exception of type 'Microsoft.CSharp.RuntimeBinder.RuntimeBinderException' occurred in Unknown Module.
Additional information: The best overloaded method match for 'System.Collections.Generic.Dictionary<double,double>.this[double]' has some invalid arguments. If there is a handler for this exception, the program may be safely continued.
【问题讨论】: