1

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.). The dictionary to be edited is passed into the control as an object named innerobject. 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):

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.

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.

1 Answer 1

1

You need a and b to be dynamic also:

dynamic a = keyconverter.ConvertFromString(str[0]);
dynamic b = valueconverter.ConvertFromString(str[1]);
dyndic[a] = b;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.