代码编译方法中的函数执行次数 [英] Count of function execution in method on code compilation

查看:51
本文介绍了代码编译方法中的函数执行次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想计算函数 'ExecuteAction' 在类方法中出现的次数.

I would like to count how many times function 'ExecuteAction' appear in class method.

public class A : B
{
  public void X()
  {
    ExecuteAction(....);
    ExecuteAction(....);
  }
}

分数是 2,因为 ExecuteAction 出现了 2x.我需要它是因为我构建了测试框架并希望允许外部测试操作员知道当前步骤执行的位置以及它将在哪里结束.是否可以做或我应该改变我的方法?

And the score is 2 becouse ExecuteAction appear 2x. I need it becouse I build testing framework and would like allow for external test operator to know where current step execution is and where it will end. Is it possible to do or should I change my approach?

谢谢.

推荐答案

下面的方法演示了如何通过反射读取方法体并统计特定方法的所有调用:

Below is the approach that demonstrates how to read method body via reflection and count all the calls of the specific method:

class Foo {
    public void SomeMethod() {
        ExecuteAction();
        ExecuteAction();
    }
    public void ExecuteAction() {
        //
    }
}
// --- Read the IL ---
var mInfo = typeof(Foo).GetMethod("SomeMethod");
var token = typeof(Foo).GetMethod("ExecuteAction").MetadataToken;

var methodBody = mInfo.GetMethodBody();
var rawIL = methodBody.GetILAsByteArray();

int counter = 0;
var reader = new ILReader(rawIL);
while(reader.Read(mInfo)) {
    if(reader.OpCode == OpCodes.Call && object.Equals(reader.MetadataToken, token)) {
        System.Console.WriteLine("Method \"{0}\" call detected", reader.Operand);
        counter++;
    }
}
System.Console.WriteLine("Total: {0}", counter);

ILReader 类实现如下(此特定任务的最小实现):

The ILReader class is implemented as follows(minimal implementation for this specific task) :

class ILReader {
    readonly byte[] msil;
    int ptr;
    public ILReader(byte[] msil) {
        this.msil = msil;
    }
    public OpCode OpCode { get; private set; }
    public int MetadataToken { get; private set; }
    public object Operand { get; private set; }
    public bool Read(MethodInfo methodInfo) {
        if(ptr < msil.Length) {
            OpCode = ReadOpCode();
            Operand = ReadOperand(OpCode, methodInfo);
            return true;
        }
        return false;
    }
    OpCode ReadOpCode() {
        byte instruction = ReadByte();
        if(instruction != 254)
            return singleByteOpCode[instruction];
        else
            return doubleByteOpCode[ReadByte()];
    }
    object ReadOperand(OpCode code, MethodInfo methodInfo) {
        MetadataToken = 0;
        switch(code.OperandType) {
            case OperandType.InlineMethod:
                MetadataToken = ReadInt();
                System.Type[] methodArgs = null;
                if(methodInfo.GetType() != typeof(ConstructorInfo))
                    methodArgs = methodInfo.GetGenericArguments();
                System.Type[] typeArgs = null;
                if(methodInfo.DeclaringType != null)
                    typeArgs = methodInfo.DeclaringType.GetGenericArguments();
                return methodInfo.Module.ResolveMember(MetadataToken, typeArgs, methodArgs);
        }
        return null;
    }
    byte ReadByte() {
        return msil[ptr++];
    }
    int ReadInt() {
        byte b1 = ReadByte();
        byte b2 = ReadByte();
        byte b3 = ReadByte();
        byte b4 = ReadByte();
        return (int)b1 | (((int)b2) << 8) | (((int)b3) << 16) | (((int)b4) << 24);
    }
    #region static
    static ILReader() {
            CreateOpCodes();
        }
    static OpCode[] singleByteOpCode;
    static OpCode[] doubleByteOpCode;
    static void CreateOpCodes() {
        singleByteOpCode = new OpCode[225];
        doubleByteOpCode = new OpCode[31];

        FieldInfo[] fields = GetOpCodeFields();

        for(int i = 0; i < fields.Length; i++) {
            OpCode code = (OpCode)fields[i].GetValue(null);
            if(code.OpCodeType == OpCodeType.Nternal)
                continue;

            if(code.Size == 1)
                singleByteOpCode[code.Value] = code;
            else
                doubleByteOpCode[code.Value & 0xff] = code;
        }
    }
    static FieldInfo[] GetOpCodeFields() {
        return typeof(OpCodes).GetFields(BindingFlags.Public | BindingFlags.Static);
    }
    #endregion static
}

这篇关于代码编译方法中的函数执行次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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