根据用途推断参数类型

发布于 2024-12-20 14:46:25 字数 1216 浏览 1 评论 0原文

我有有线问题,我知道推理需要使用开放通用。

有没有办法调用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

故事灯 2024-12-27 14:46:25

如果您确定必须使用反射,则可以这样做(假设 Program 是包含 AssertDictionary() 的类型):

// get the method
var openMethod = typeof(Program).GetMethod(
    "AssertDictionary", BindingFlags.NonPublic | BindingFlags.Static);

// get type arguments of the dictionary
var dictTypeArgs = o1.GetType().GetGenericArguments();

// set the type parameters of the method
var closedMethod = openMethod.MakeGenericMethod(dictTypeArgs);

// invoke the method
closedMethod.Invoke(null, new [] { o1, o2 });

另一种选择是使用 dynamic 并在运行时自动推断类型参数:

AssertDictionary((dynamic)o1, (dynamic)o2);

我认为这里不需要创建任何动态方法。

If you're sure you have to use reflection, you can do it like this (assuming Program is the type containing AssertDictionary()):

// get the method
var openMethod = typeof(Program).GetMethod(
    "AssertDictionary", BindingFlags.NonPublic | BindingFlags.Static);

// get type arguments of the dictionary
var dictTypeArgs = o1.GetType().GetGenericArguments();

// set the type parameters of the method
var closedMethod = openMethod.MakeGenericMethod(dictTypeArgs);

// invoke the method
closedMethod.Invoke(null, new [] { o1, o2 });

Another option is to use dynamic and infer the type parameters at runtime automatically:

AssertDictionary((dynamic)o1, (dynamic)o2);

I think there is no need to create any dynamic methods here.

无敌元气妹 2024-12-27 14:46:25

如果您打算使用反射,那么您假设您已经知道类型,除非您愿意迭代成员。

试试这个:

        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;

        AssertDictionary((Dictionary<int, Person>)o2, (Dictionary<int, Person>)o1);

或者你可以使用dynamic关键字告诉编译器你很确定你在做什么。

AssertDictionary((dynamic)o1, (dynamic)o2);

为了避免必须将每种类型转换为动态类型,那么您应该修改该方法我不推荐使用这种技术尽管

    private static void AssertDictionary(dynamic expected, dynamic actual)
    {
        // you will be able to call methods as:
        actual.Add(1, new Person()); // intelisence will not help you you have to be carefull with the dynamic type
        // I dont recoment using it if there is a work around
    }

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:

        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;

        AssertDictionary((Dictionary<int, Person>)o2, (Dictionary<int, Person>)o1);

Or you can tell the compiler that you are shure what you are doing by using the dynamic keyword.

AssertDictionary((dynamic)o1, (dynamic)o2);

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

    private static void AssertDictionary(dynamic expected, dynamic actual)
    {
        // you will be able to call methods as:
        actual.Add(1, new Person()); // intelisence will not help you you have to be carefull with the dynamic type
        // I dont recoment using it if there is a work around
    }

svik code looks much better. I will use his code if I where you...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文