根据用途推断参数类型
我有有线问题,我知道推理需要使用开放通用。
有没有办法调用 AssertDictionary(o2, o1);通过反射。 我想要的是,使用 o1 说 t1 和 o2 说 t2 的类型作为 AssertDictionary(o1, o2);
这有道理吗?
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)
.....
.....
}
}
我可以在运行时创建动态方法并使用泛型类型从那里调用 AssertDictionary 吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您确定必须使用反射,则可以这样做(假设
Program
是包含AssertDictionary()
的类型):另一种选择是使用
dynamic
并在运行时自动推断类型参数:我认为这里不需要创建任何动态方法。
If you're sure you have to use reflection, you can do it like this (assuming
Program
is the type containingAssertDictionary()
):Another option is to use
dynamic
and infer the type parameters at runtime automatically:I think there is no need to create any dynamic methods here.
如果您打算使用反射,那么您假设您已经知道类型,除非您愿意迭代成员。
试试这个:
或者你可以使用dynamic关键字告诉编译器你很确定你在做什么。
为了避免必须将每种类型转换为动态类型,那么您应该修改该方法我不推荐使用这种技术尽管
svik 代码看起来更好。如果我在你那里,我会使用他的代码...
if you are planing on using reflection you are assuming you already know the type unles you are plaing to iterate through the memebrs.
try this:
Or you can tell the compiler that you are shure what you are doing by using the dynamic keyword.
and to avoid having to cast each type to type dynamic then you should modify the method instead I do not recoment using this technique though
svik code looks much better. I will use his code if I where you...