使用反射查找所有属性引用

时间:2015-10-07 16:08:12

标签: c# reflection

给定属性

class Test
{
    private string name;
    public string Name
    {
        get { return name; }
        set { name = value;}
    }
}

有没有办法使用反射来查找程序集中的所有get / set引用?例如,如果某些测试代码使用此属性,则如下所示

class Client 
{
    private Test test = new Test();

    public string Name = test.Name;
}

反思可以发现Client调用get上的Test.Name方法吗?我可以打开我的IDE并执行“查找所有引用”,但我想知道这是否可以自动化。

1 个答案:

答案 0 :(得分:3)

您可以通过解析每个方法的methodbody并搜索相应的元数据标记来实现此目的。看看这个例子,它将使用搜索的方法标记打印出所有指令的偏移量。

namespace TokenSearch
{
    internal static class Program
    {
        private static void Main()
        {
            var token = typeof (Class1).GetProperty("TargetProp").GetGetMethod().MetadataToken;

            const BindingFlags findAll = BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic |
                                         BindingFlags.Instance | BindingFlags.Static;
            var references =
                typeof (Program).Assembly.ManifestModule.GetTypes()
                    .SelectMany(x => x.GetMethods(findAll).Cast<MethodBase>().Union(x.GetConstructors(findAll)))
                    .ToDictionary(y => y, y => y.GetMethodUsageOffsets(token).ToArray())
                    .Where(z => z.Value.Length > 0).ToList();

            foreach (var kv in references)
            {
                Console.WriteLine(
                    $"{kv.Key.DeclaringType}::{kv.Key.Name}: {string.Join(" ", kv.Value.Select(x => $"0x{x:x}"))}");
            }
        }
    }

    //some tests
    public class Class1
    {
        public string TargetProp { get; set; }

        private void TestMethod()
        {
            TargetProp = "123";
            var x = TargetProp;
            var y = TargetProp;
        }
    }

    public class Class2
    {
        private string c1 = new Class1().TargetProp;

        public void MoreMethods()
        {
            var c = new Class1();
            var x = c.TargetProp;
        }

        public void CantFindThis()
        {
            var c = new Class1();
            var x = c.ToString();
        }
    }

    public static class Extensions
    {
        private static readonly Dictionary<short, OpCode> OpcodeDict =
            typeof (OpCodes).GetFields(BindingFlags.Public | BindingFlags.Static)
                .Select(x => (OpCode) x.GetValue(null))
                .ToDictionary(x => x.Value, x => x);

        public static IEnumerable<short> GetMethodUsageOffsets(this MethodBase mi, int token)
        {
            var il = mi.GetMethodBody()?.GetILAsByteArray();
            if (il == null) yield break;
            using (var br = new BinaryReader(new MemoryStream(il)))
            {
                while (br.BaseStream.Position < br.BaseStream.Length)
                {
                    var firstByte = br.ReadByte();
                    var opcode =
                        OpcodeDict[
                            firstByte != 0xFE
                                ? firstByte
                                : BitConverter.ToInt16(new[] {br.ReadByte(), firstByte}, 0)];
                    switch (opcode.OperandType)
                    {
                        case OperandType.ShortInlineBrTarget:
                        case OperandType.ShortInlineVar:
                        case OperandType.ShortInlineI:
                            br.ReadByte();
                            break;
                        case OperandType.InlineVar:
                            br.ReadInt16();
                            break;
                        case OperandType.InlineField:
                        case OperandType.InlineType:
                        case OperandType.ShortInlineR:
                        case OperandType.InlineString:
                        case OperandType.InlineSig:
                        case OperandType.InlineI:
                        case OperandType.InlineBrTarget:
                            br.ReadInt32();
                            break;
                        case OperandType.InlineI8:
                        case OperandType.InlineR:
                            br.ReadInt64();
                            break;
                        case OperandType.InlineSwitch:
                            var size = (int) br.ReadUInt32();
                            br.ReadBytes(size*4);
                            break;
                        case OperandType.InlineMethod:
                        case OperandType.InlineTok:
                            if (br.ReadInt32() == token)
                            {
                                yield return (short) (br.BaseStream.Position - 4 - opcode.Size);
                            }
                            break;
                    }
                }
            }
        }
    }
}

控制台输出:

TokenSearch.Class1::TestMethod: 0xe 0x15
TokenSearch.Class2::MoreMethods: 0x8
TokenSearch.Class2::.ctor: 0x6

ILdasm输出Class1::TestMethod以供参考:

.method private hidebysig instance void  TestMethod() cil managed
// SIG: 20 00 01
{
  // Method begins at RVA 0x21d0
  // Code size       28 (0x1c)
  .maxstack  2
  .locals init ([0] string x,
           [1] string y)
  IL_0000:  /* 00   |                  */ nop
  IL_0001:  /* 02   |                  */ ldarg.0
  IL_0002:  /* 72   | (70)000037       */ ldstr      "123"
  IL_0007:  /* 28   | (06)000003       */ call       instance void TokenSearch.Class1::set_TargetProp(string)
  IL_000c:  /* 00   |                  */ nop
  IL_000d:  /* 02   |                  */ ldarg.0
  IL_000e:  /* 28   | (06)000002       */ call       instance string TokenSearch.Class1::get_TargetProp()
  IL_0013:  /* 0A   |                  */ stloc.0
  IL_0014:  /* 02   |                  */ ldarg.0
  IL_0015:  /* 28   | (06)000002       */ call       instance string TokenSearch.Class1::get_TargetProp()
  IL_001a:  /* 0B   |                  */ stloc.1
  IL_001b:  /* 2A   |                  */ ret
} // end of method Class1::TestMethod

可以在Mono.Reflection中找到方法体解析器的完整实现:​​MethodBodyReader.cs