【问题标题】:Get type stored in binary field signature获取存储在二进制字段签名中的类型
【发布时间】:2015-01-28 03:21:54
【问题描述】:
假设您在 .NET 模块中有字段签名的二进制表示,例如 0604。 6 (FIELD) 代表字段调用约定,4 (ELEMENT_TYPE_I1) 代表 I1 原始类型(有关 CIL 的更多信息,请参见 ECMA-335)。签名可以来自调试器或程序集检查器,这并不重要。更重要的是,是否有可能(使用 .NET 提供的方法)“解析”这个签名并获得该签名所代表的对应 .NET 类型?
例子:
0601 ⇒ System.Void
0604 ⇒ System.SByte
060E ⇒ System.String
061408020000 ⇒ System.Int32[,]
【问题讨论】:
标签:
c#
.net
reflection
clr
cil
【解决方案2】:
有一些内部 .NET 方法可以做到这一点:
public static unsafe Type GetTypeFromFieldSignature(byte[] signature, Type declaringType = null)
{
declaringType = declaringType ?? typeof(object);
Type sigtype = typeof(Type).Module.GetType("System.Signature");
Type rtype = typeof(Type).Module.GetType("System.RuntimeType");
var ctor = sigtype.GetConstructor(BindingFlags.Public | BindingFlags.Instance, null, new[]{typeof(void*), typeof(int), rtype}, null);
fixed(byte* ptr = signature)
{
object sigobj = ctor.Invoke(new object[]{(IntPtr)ptr, signature.Length, declaringType});
return (Type)sigtype.InvokeMember("FieldType", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetProperty, null, sigobj, null);
}
}
这会加载任何有效的字段签名并返回适当的类型。