简体   繁体   English

C#如何获取方法调用的数量?

[英]C# How to get the number of the method calls?

In Java - basing on the Aspects - we can get the number of the function calls, eg: 在Java中-基于Aspects-我们可以获得函数调用的数量,例如:

SomeClass sc = new SomeClass();
sc.m1();
sc.m1();
int no = sc.getNumberOfCalls("m1");
System.out.println(no); //2

How to do it in C# ? 如何在C#中做到这一点?

this is another option by using external product..In .NET 这是使用外部产品的另一种选择。

The canonical, easiest way would probably be to simply use a profiler application. 规范,最简单的方法可能是简单地使用探查器应用程序。 Personally I have good experiences with jetBrains dotTrace , but there are more out there. 我个人对jetBrains dotTrace拥有很好的经验,但还有更多经验。

Basically what you do is you let the profiler fire up your app, and it will keep track of all method calls in your code. 基本上,您要做的是让探查器启动您的应用程序,它将跟踪代码中的所有方法调用。 It will then show you how much time was spent executing those methods, and how many times they are called. 然后它将向您显示执行这些方法花费了多少时间,以及它们被调用了多少次。

Assuming your reason for wanting to know this is actually performance, I think it's a good idea to look at a profiler. 假设您想知道这实际上是性能的原因,我认为查看分析器是个好主意。 You can try to optimize your code by doing an educated guess to where the bottlenecks are, but if you use a profiler you can actually measure that. 您可以通过对瓶颈在哪里进行有根据的猜测来尝试优化代码,但是如果您使用探查器,则可以实际进行测量。 And we all know, measure twice, cut once ;-) 而且我们都知道,测量两次,切割一次;-)

or 要么

this is also good option 这也是不错的选择

AQtime does that with no difficulty. AQtime毫不费力地做到了。

I believe theres not suach a built in feature for that, so you may wanna code something this way: 我相信并没有为此提供内置功能,因此您可能希望通过以下方式编写代码:

    public class SomeClass
    {
        public Int32 MethodCallCount { get; set; }

        public void Method()
        {
            this.MethodCallCount++;
            //Your custom code goes here!
        }
    }

If you wanna go deeper you may want look for AOP (aspect oriented programming) interceptors! 如果您想更深入,则可能需要寻找AOP(面向方面​​的编程)拦截器! - if so you may start looking for Spring.NET framework! -如果是这样,您可能会开始寻找Spring.NET框架!

If you are within a test scenario the most apropriate solution would be using a mock framework for that. 如果您处于测试场景中,则最合适的解决方案是为此使用模拟框架。

How about simply do this in your class ? 在您的班级中简单地这样做呢?

public class SomeClass
{
    private int counter = 0;

    public void m1()
    {
        counter++;
    }

    public int getMethodCalls()
    {
        return counter;
    }
}

This capability is not built into .NET. .NET未内置此功能。 However, you can use any one of the mock object frameworks to do this. 但是,您可以使用任何一个模拟对象框架来执行此操作。 For example, with RhinoMocks you can set the number of expected number of calls to a method and check it. 例如,使用RhinoMocks,您可以设置方法的预期调用次数并进行检查。

You can also accomplish this if you create a dynamic, runtime proxy for your objects and have your proxy keep track. 如果您为对象创建动态的运行时代理并跟踪代理 ,则也可以完成此操作。 That might make the cure worse than the disease though! 但这可能会使治疗胜于疾病!

-- Michael -迈克尔

You can also do Aspects in C#. 您也可以在C#中执行方面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM