Dictionary is a pain, since it doesn't implement IList
(something I was moaning about just the other evening). It really comes down to what you have available. Resolving the Add
method for int
/string
is easy enough, but if you are dealing with typical list reflection you'll need the Add
method from ICollection<KeyValuePair<int,string>>
.
For standard usage:
object data = new Dictionary<int, string>();
object key = 123;
object value = "abc";
var add = data.GetType().GetMethod("Add", new[] {
key.GetType(), value.GetType() });
add.Invoke(data, new object[] { key, value });