简体   繁体   English

向动态类型添加值会导致RuntimeBinderException

[英]Adding values to a dynamic type causes a RuntimeBinderException

I am working on building a UITypeEditor (launched from the property grid) to edit a Dictionary<T,T> where T could be any scalar type ( int , long , double , string , DateTime , etc.). 我正在构建一个UITypeEditor (从属性网格启动)来编辑Dictionary<T,T> ,其中T可以是任何标量类型( intlongdoublestringDateTime等)。 The dictionary to be edited is passed into the control as an object named innerobject . 要编辑的字典作为名为innerobjectobject传递到控件中。 I get the type and key-value types as following: 我得到的类型和键值类型如下:

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;

Later when I try to add values to it, I do the following (s is one line in the textbox): 稍后,当我尝试向其中添加值时,我将执行以下操作(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;
}

A Microsoft.CSharp.RuntimeBinder.RuntimeBinderException is thrown at this point. 此时将引发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.

You need a and b to be dynamic also: 您还需要ab来保持动态:

dynamic a = keyconverter.ConvertFromString(str[0]);
dynamic b = valueconverter.ConvertFromString(str[1]);
dyndic[a] = b;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在视图中访问动态匿名类型时的RuntimeBinderException - RuntimeBinderException when accessing dynamic anonymous type in view 尝试使用动态类型时RuntimeBinderException - RuntimeBinderException when trying to use dynamic type RuntimeBinderException 将泛型类型上的方法作为动态调用 - RuntimeBinderException invoking a method on a generic type as dynamic 尝试在动态创建的程序集上绑定动态方法会导致RuntimeBinderException - Attempting to bind a dynamic method on a dynamically-created assembly causes a RuntimeBinderException 将动态方法调用移至类库会导致C#中的RuntimeBinderException - Moving dynamic method call to class library causes a RuntimeBinderException in C# 将int []传递给参数类型为动态的方法时发生RunTimeBinderException - RunTimeBinderException when passing int[] to method having parameter type dynamic 尝试实例化动态类型的数组时发生RuntimeBinderException(后期绑定) - RuntimeBinderException when trying to instantiate an array of a dynamic type (late-binding) 将JSON对象反序列化为动态类型,但获取RuntimeBinderException访问属性? - Deserialized JSON object to dynamic type but getting RuntimeBinderException accessing properties? RuntimeBinderException通过动态将非0的Enum值传递给.Net Com程序集 - RuntimeBinderException passing Enum values other then 0 to a .Net Com Assembly via dynamic C#动态-“ RuntimeBinderException” - C# Dynamic - “RuntimeBinderException”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM