gpt4 book ai didi

c# - 使用反射查找所有属性引用

转载 作者:行者123 更新时间:2023-11-30 21:54:31 26 4
gpt4 key购买 nike

给定一个属性

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.Reflection 中找到方法主体解析器的完整实现:​​MethodBodyReader.cs

关于c# - 使用反射查找所有属性引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32997245/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com