I have a class
class a
{
private Dictionary <int , string> m_Dict = new Dictionary<int , string>();
}
from some other component/class need to add values to the m_Dict dictionary using reflection! How do i do it ? i searched and tried but was not successfull.
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 });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With