How can I get fields used in a method (.NET)?

前端 未结 5 1039
名媛妹妹
名媛妹妹 2020-12-16 06:45

In .NET, using reflection how can I get class variables that are used in a method?

Ex:

class A
{
    UltraClass B = new(..);
    SupaClass C = new(..         


        
5条回答
  •  心在旅途
    2020-12-16 07:34

    You need to get the MethodInfo. Call GetMethodBody() to get the method body structure and then call GetILAsByteArray on that. The convert that byte array into a stream of comprehensible IL.

    Roughly speaking

    public static List ReadIL(MethodInfo method)
    {
        MethodBody body = method.GetMethodBody();
        if (body == null)
            return null;
    
        var instructions = new List();
        int offset = 0;
        byte[] il = body.GetILAsByteArray();
        while (offset < il.Length)
        {
            int startOffset = offset;
            byte opCodeByte = il[offset];
            short opCodeValue = opCodeByte;
            // If it's an extended opcode then grab the second byte. The 0xFE
            // prefix codes aren't marked as prefix operators though. 
            if (OpCodeList[opCodeValue].OpCodeType == OpCodeType.Prefix
                || opCodeValue == 0xFE)
            {
                opCodeValue = (short) ((opCodeValue << 8) + il[offset + 1]);
                offset += 1;
            }
            // Move to the first byte of the argument.
            offset += 1;
    
            OpCode code = OpCodeList[opCodeValue];
    
            Int64? argument = null;
            if (code.ArgumentSize() > 0)
            {
                Int64 arg = 0;
                Debug.Assert(code.ArgumentSize() <= 8);
                for (int i = 0; i < code.ArgumentSize(); ++i)
                {
                    Int64 v = il[offset + i];
                    arg += v << (i*8);
                }
                argument = arg;
                offset += code.ArgumentSize();
            }
    
            var instruction = new Instruction(startOffset, code, argument);
            instructions.Add(instruction);
        }
    
        return instructions;
    }
    

    where OpCodeList is constructed via

    OpCodeList = new Dictionary();
    foreach (var opCode in typeof (OpCodes).GetFields()
                           .Where(f => f.FieldType == typeof (OpCode))
                           .Select(f => (OpCode) f.GetValue(null)))
    {
        OpCodeList.Add(opCode.Value, opCode);
    }
    

    You can then work out which instructions are IL property calls or member variable look ups or whatever you require and resolve then via GetType().Module.ResolveField.

    (Caveat code above more or less work but was ripped from a bigger project I did so maybe missing minor details).

    Edit: Argument size is an extension method on OpCode that just uses a look up table to do find the appropriate value

    public static int ArgumentSize(this OpCode opCode)
    {
      Dictionary operandSizes 
               = new Dictionary()
                     {
                        {OperandType.InlineBrTarget, 4},
                        {OperandType.InlineField, 4},
                        {OperandType.InlineI, 4},
                        // etc., etc.
                     };
      return operandSizes[opCode.OperandType];
    }
    

    You'll find sizes in ECMA 335 which you'll also need to look at for the OpCodes to find which OpCodes you to search for to find the calls you are looking for.

提交回复
热议问题