给定属性
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
调用Test.Name
上的get
方法吗?我可以打开我的IDE,做一个"找到所有的参考",但我想知道这是否可以自动化。
可以通过解析每个方法的方法体并搜索相应的元数据令牌来实现这一点。看一下这个例子,它将使用搜索的方法标记打印出所有指令的偏移量。
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
Class1::TestMethod
的ILdasm输出供参考:
.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中找到。反射:MethodBodyReader.cs