创建通用函数的委托

发布于 2024-08-09 13:15:44 字数 1419 浏览 3 评论 0原文

我正在编写一些单元测试,并且我有很多形式的函数

public void SomeTestHelperMethod<TKey, TValue>(TKey key, TValue value)

,我用这样的各种参数重复调用这些函数

SomeTestHelperMethod<int, int>(0, 1);
SomeTestHelperMethod<int, object>(1, new Nullable<double>(16.5));
SomeTestHelperMethod<int, string>(2, "The quick brown fox jumped over the lazy dog.");
SomeTestHelperMethod<object, int>(new NullReferenceException(), 15);
SomeTestHelperMethod<object, object>(StringComparison.Ordinal, new Version());
SomeTestHelperMethod<object, string>((ushort)3, string.Empty);
SomeTestHelperMethod<string, int>(string.Empty, 195);
SomeTestHelperMethod<string, object>("A string", this);
SomeTestHelperMethod<string, string>("Another string", "Another string");

我想做的是编写一个函数,该函数接受一个 Action 委托并可以使用以下命令调用委托所有不同的论点。有什么办法可以做到吗?

答案:

感谢 MichaelCG,这就是我最终所做的:

private void CallWithKeyAndValue(string methodName)
{
    MethodInfo method = typeof(ObservableDictionaryTest).GetMethod(methodName);
    foreach (KeyValuePair<object, object> kvp in ourKeyValueSet)
    {
        MethodInfo genericMethod = method.MakeGenericMethod(kvp.Key.GetType(), kvp.Value.GetType());
        genericMethod.Invoke(this, new[] { kvp.Key, kvp.Value });
    }
}

我仍然对一种更通用的方法感兴趣,但这个方法对于我的目的来说是有用的。

I'm writing some unit tests and I have a lot of functions of the form

public void SomeTestHelperMethod<TKey, TValue>(TKey key, TValue value)

which I'm calling repeatedly with various arguments like this

SomeTestHelperMethod<int, int>(0, 1);
SomeTestHelperMethod<int, object>(1, new Nullable<double>(16.5));
SomeTestHelperMethod<int, string>(2, "The quick brown fox jumped over the lazy dog.");
SomeTestHelperMethod<object, int>(new NullReferenceException(), 15);
SomeTestHelperMethod<object, object>(StringComparison.Ordinal, new Version());
SomeTestHelperMethod<object, string>((ushort)3, string.Empty);
SomeTestHelperMethod<string, int>(string.Empty, 195);
SomeTestHelperMethod<string, object>("A string", this);
SomeTestHelperMethod<string, string>("Another string", "Another string");

What I'd like to do is write a function that takes an Action delegate and can Invoke the delegate with all of the different arguments. Is there any way to do it?

Answer:

Thanks to MichaelCG here's what I ended up doing:

private void CallWithKeyAndValue(string methodName)
{
    MethodInfo method = typeof(ObservableDictionaryTest).GetMethod(methodName);
    foreach (KeyValuePair<object, object> kvp in ourKeyValueSet)
    {
        MethodInfo genericMethod = method.MakeGenericMethod(kvp.Key.GetType(), kvp.Value.GetType());
        genericMethod.Invoke(this, new[] { kvp.Key, kvp.Value });
    }
}

I'd still be interested in a more general method but this one is functional for my purposes.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

围归者 2024-08-16 13:15:44

如果我理解正确的话,这应该表明你正在尝试做什么。神奇之处在于 MakeGenericMethod。

using System;

class Program {
    static void Main(string[] args) {
        var meth = typeof(Program).GetMethod("Meth");
        var items = new[] { 
            new { a = (object)"hi", b = (object)1 },
            new { a = (object)TimeSpan.MaxValue, b = (object)DateTime.UtcNow },
        };
        foreach (var item in items) {
            var gmeth = meth.MakeGenericMethod(item.a.GetType(), item.b.GetType());
            gmeth.Invoke(null, new[] { item.a, item.b });
        }
    }

    public static void Meth<A, B>(A a, B b) {
        Console.WriteLine("<{0}, {1}>", typeof(A).Name, typeof(B).Name);
    }
}

输出:

<String, Int32> 
<TimeSpan, DateTime>

If I understand you correctly, this should demonstrate what you are trying to do. The magic is in MakeGenericMethod.

using System;

class Program {
    static void Main(string[] args) {
        var meth = typeof(Program).GetMethod("Meth");
        var items = new[] { 
            new { a = (object)"hi", b = (object)1 },
            new { a = (object)TimeSpan.MaxValue, b = (object)DateTime.UtcNow },
        };
        foreach (var item in items) {
            var gmeth = meth.MakeGenericMethod(item.a.GetType(), item.b.GetType());
            gmeth.Invoke(null, new[] { item.a, item.b });
        }
    }

    public static void Meth<A, B>(A a, B b) {
        Console.WriteLine("<{0}, {1}>", typeof(A).Name, typeof(B).Name);
    }
}

Outputs:

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