将委派与params关键字匹配任何方法? [英] Will Delegate with params keyword match any method?

查看:107
本文介绍了将委派与params关键字匹配任何方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试完成以下事情:

  public delegate void SomeMethod(params object [] parameters); 

这是我的委托。
我有一些方法可以运行这个SomeMethod委托(无论传递什么),并返回执行的执行时间。

  public TimeSpan BenchmarkMethod(SomeMethod someMethod,params object [] parameters)
{
DateTime benchmarkStart = DateTime.Now;

someMethod(parameters);

DateTime benchmarkFinish = DateTime.Now;
return benchmarkFinish - benchmarkStart;
}

另外我有一些方法:

  public abstract void InsertObjects(Company c); 

所以,我声明:

  SomeMethod dlg = new SomeMethod(InsertObjects); 
TimeSpan executionTime = BenchmarkMethod(dlg,c);

但是它不运行,说'InsertObjects'没有重载匹配委托'TestFactory.MeasuringFactory 一个方法。有什么办法吗?或者我应该改变我的所有方法来接受params对象[]作为参数?

解决方案

严格地说,方法签名必须与委托指定的签名完全匹配(除了协变匹配)。但是,您可以创建一个对象[] 数组,并将其导入到 Delegate.DynamicInvoke(object [] args)。 / p>

编辑:



如果您有关于要调用的方法的信息,可以使用 MethodBase.GetParameters()。Length 来获取参数的数量,所以你可以正确地设置无类型参数数组。



但是,我认为你最好使用一个实现必要的基准操作的抽象基类:

 抽象类Benchmark 
{
TimeSpan Run()
{
秒表swatch = Stopwatch.StartNew();
//可以循环这个循环次数,并循环使用时间:
RunMethod();
swatch.Stop();
return swatch.Elapsed;
}

///< summary>使用要进行基准测试的代码覆盖此方法。< / summary>
protected abstract void RunMethod()
{
}
}

虚拟方法分派与代理具有相似的延迟,比动态调用好得多。


I'm trying to get the following thing done:

public delegate void SomeMethod(params object[] parameters);

That's my delegate. And i have some method that will run this SomeMethod delegate (whatever's passed) and return me the timespan of execution.

   public TimeSpan BenchmarkMethod(SomeMethod someMethod, params object[] parameters)
    {
        DateTime benchmarkStart = DateTime.Now;

        someMethod(parameters);

        DateTime benchmarkFinish = DateTime.Now;
        return benchmarkFinish - benchmarkStart;
    }

Also i have some method:

public abstract void InsertObjects (Company c);

So, i declare it:

SomeMethod dlg = new SomeMethod(InsertObjects);
TimeSpan executionTime = BenchmarkMethod(dlg, c);

But it doesn't run, saying that No overload for 'InsertObjects' matches delegate 'TestFactory.MeasuringFactory.SomeMethod'. Is there any way to do it?.. Or i should change all the methods of mine to accept params object[] as an argument?..

解决方案

Strictly no, the method signature must exactly match the signature specified by the delegate (except for covariant matching). However, you can create an object[] array and feed to Delegate.DynamicInvoke(object[] args).

Edit:

If you have information about the method to be called, you can use MethodBase.GetParameters().Length to obtain the number of arguments, so you can correctly size the untyped argument array.

For benchmarking, though, I think you are better off using an abstract base class that implements the necessary benchmarking ops:

abstract class Benchmark
{
    TimeSpan Run()
    {
        Stopwatch swatch = Stopwatch.StartNew();
        // Optionally loop this several times and divide elapsed time by loops:
        RunMethod();
        swatch.Stop();
        return swatch.Elapsed;
    }

    ///<summary>Override this method with the code to be benchmarked.</summary>
    protected abstract void RunMethod()
    {
    }
}

Virtual method dispatches have comparable latency to delegates, and much better than dynamic invocation.

这篇关于将委派与params关键字匹配任何方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆