I have wired problem, I know Inference requires the use of an open generic.
Is ther a way to call AssertDictionary(o2, o1); by Reflection.
What I want is, use Type of o1 say t1 and o2 say t2 as AssertDictionary(o1, o2);
does this make sense?
public static void Main(String[] args)
{
Dictionary<int, Person> actual = new Dictionary<int, Person>();
actual.Add(10, new Person{Address="Abc", Name = "Dipak"});
Dictionary<int, Person> expected = new Dictionary<int, Person>();
expected.Add(10, new Person { Address = "Abc", Name = "Dipak" });
object o1 = actual;
object o2 = expected;
CallAssert(o1, o2);
}
private static CallAssert(object exp, object act)
{
AssertDictionary(exp, act);// I'm doing it at runtime by reflection and actual & expected are of type System.Object
}
private static void AssertDictionary<TKey, TVal>(Dictionary<TKey, TVal> expected, Dictionary<TKey, TVal> actual)
{
if (expected != null && actual != null)
{
foreach (KeyValuePair<TKey, TVal> valuePair in expected)
.....
.....
}
}
Can I create dynamic method at runtime and Call AssertDictionary from there with Generic types?
If you’re sure you have to use reflection, you can do it like this (assuming
Programis the type containingAssertDictionary()):Another option is to use
dynamicand infer the type parameters at runtime automatically:I think there is no need to create any dynamic methods here.