.NET实时反射计数方法在特定方法中的调用 [英] .NET Real Time Reflection count method calls within a specific method

查看:31
本文介绍了.NET实时反射计数方法在特定方法中的调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用反射或其他技术,我想知道methodB被调用了多少次.最好的方法是计算方法B的调用次数.计算对方法B的调用次数就足够了.

Using reflection or some other technique I want to know how many times methodB is called. Best would be to count number of calls for methodB winthin methodA. It would suffice to count the number of calls to methodB.

class MyClass
{
    void methodA()
    {
        methodB();
        methodB();
    }
    void methodB()
    {
    }
}

推荐答案

Reflection在这里无济于事,因为这都是运行时间.反射只能帮助您提取代码的静态属性.您甚至不知道可以在运行时随时更改呼叫数量吗? 正如进一步的讨论所示,这不是OP的意思.问题是未能解释通话"的含义. [END EDIT]

如果要在整个程序中找到对某个方法的所有引用,那将是完全不同的故事,这是一项艰巨的任务.基本上,如果源代码首先通过解析它,则应该涉及代码的反编译或解决方案的静态分析.我什至不想在这里进一步讨论它.我有能力解决此类问题,在您提出问题的方式中我可以看到.对不起. 正如进一步的讨论所示,这是OP真正要做的.在本段中,以及在对问题的新评论中,我试图解释为什么我认为这种简短的解释对于现在来说应该足够了. [END EDIT]

我要说,只有一种可靠的计数调用的方法:在调用方法本身中对其进行编程,增加一些计数器,这是声明类的一个字段.这个问题是微不足道的. 如进一步的讨论所示,这与OP想要的内容无关. [END EDIT]



比尔在下面的评论中提出了一些好的观点.
预测运行时行为的能力是可计算性理论的一个大问题.

这个问题的复杂性可以从一个众所周知的暂停问题的例子中清楚地看出:某个程序是否会暂停或将永远执行的问题的答案是静态的无法确定:

请参阅:
http://en.wikipedia.org/wiki/Halting_problem [ http://en.wikipedia.org/wiki/Computability_theory_(computer_science) [ http://en.wikipedia.org/wiki/Undecidable_problem [
当然,这是真的,所以比尔就在这里.这里的实际重要问题是:可以找到一类可预测的程序,这将是不平凡的(也就是说,足以解决实际的不平凡的问题)并且可以进行静态分析.这个问题非常困难,但是静态分析已经用于静态验证程序运行时行为的某些方面.我实际上正在解决此类问题之一.

但是,当然,正如我们现在所知道的那样,Nathan提出了一个非常不同的问题:在某些其他方法的主体中,用于编程对某个方法的调用的调用语句的数量可以是多少.这个问题可以解决,因为这是纯静态分析,与运行时间没有直接关系.我懒得解决它.也许Nathan可以根据他已经发布的解决方案找到正确的解决方案. :-)

-SA
Reflection cannot help here, because this is all run time. Reflection can only help you to pull static property of code. Do you even understand that the number of calls can be changed during run time at any moment? As further discussion demonstrated, this is not what OP meant. The problem was failure to explain what is meant by "call". [END EDIT]

It would be completely different story tf you meant to find all references to some method throughout the program, this is much more difficult task. Basically, it should involve decompilation of the code or static analysis of the solution if the source code through parsing it first. I don''t even want to discuss it any further here. I you were capable of addressing such problems, I could see it in the way you formulate questions. Sorry. As further discussion demonstrated, this is what OP really meant to do. In this paragraph and in my new comments to the question, I tried to explain why I think this short explanation should be enough for now. [END EDIT]

I would say, there is only one reliable way to count calls: to program it in the calling method itself, increment some counter which is a field of the declaring class. This problem is just trivial. As further discussion demonstrated, this is irrelevant to what OP wanted. [END EDIT]



Bill make some good points in his comments below.
The ability to predict run-time behavior is a big issue of computability theory.

The complexity of this problem can be clearly seen on the example of well-known halting problem: the answer to the question if some program will ever halt or is going to execute forever is statically undecideable:

Please see:
http://en.wikipedia.org/wiki/Halting_problem[^],
http://en.wikipedia.org/wiki/Computability_theory_(computer_science)[^],
http://en.wikipedia.org/wiki/Undecidable_problem[^].

It seems obvious that if that even the halting is undecideable, the prediction of the number of some calls is not better.

From the other hand, let''s look at the proof of the halting theorem. The proof is non-constructing and involves the following idea: suppose the halting problem is solvable using some algorithm A. Then it can be proved then the algorithm A can be used to create the behavior of the program opposite the the prediction by A. "Non-constructive" means that the proof does not help to construct the program which behavior is undecideable, but we can see that such design can be more exotic. This suggests that, in many special cases, the behavior of program would be quite predictable.

Of course, this is true, so Bill is right here. The practically important problem here is: can a class of predictable programs be found, which would be non-trivial enough (that is, good enough for solving practical non-trivial problem) and could static analysis be practical. The problem is very difficult, but static analysis is already used to validate certain aspects of run-time behavior of a program statically. I''m actually working at one of such problems.

But of course, as now we know that Nathan meant a very different problem: to could the number of call statements which program the call to a certain method, in the body of some other method. This problem is solvable, because this is a pure static analysis, not directly related to the run time. I''m too lazy to get to solving it; perhaps Nathan can find a correct solution based on the one he already posted. :-)

—SA


using System;
using System.Reflection;

namespace ReflectRefCount
{
    static class Program
    {
        static void Main()
        {
            Type t = typeof(A);
            MethodInfo mi = t.GetMethod("E");
            MethodBody mb = mi.GetMethodBody();
            byte[] byEmptyMethod = mb.GetILAsByteArray();
            mi = t.GetMethod("D");
            mb = mi.GetMethodBody();
            byte[] byMethodOneCall = mb.GetILAsByteArray();
            byte[] byCallSig = new byte[byMethodOneCall.Length - byEmptyMethod.Length];
            for (int i = 0; i < byCallSig.Length; i++)
            {
                byCallSig[i] = byMethodOneCall[i];
            }
            mi = t.GetMethod("B");
            mb = mi.GetMethodBody();
            byte[] byMethodTarget = mb.GetILAsByteArray();
            int cnt = match(byCallSig, byMethodTarget);
        }

        private static int match(byte[] needle, byte[] haystack)
        {
            int result = 0;
            int end = needle.Length - 1;
            int hend = haystack.Length - 1;
            for (int i = 0; i < haystack.Length; i++)
            {
                bool match = true;
                int pos = 0;
                while (match)
                {
                    int ipos = i + pos;
                    if (ipos > hend)
                    {
                        break;
                    }
                    match = false;
                    if (haystack[ipos]==needle[pos])
                    {
                        match = true;
                    }
                    if (pos == end)
                    {
                        if (match)
                        {
                            result++;
                        }
                        break;
                    }
                    pos++;
                }
            }
            return result;
        }

        public class A
        {
            public void B()
            {
                C();
                C();
                C();
                C();
                C();
            }
            private void C()
            {
            }
            public void D()
            {
                C();
            }
            public void E()
            {
            }

        }
    }
}


这篇关于.NET实时反射计数方法在特定方法中的调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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