What is the (fnptr)* type and how to create it?

前端 未结 3 1108
一个人的身影
一个人的身影 2020-12-29 11:02

The following IL code creates a Type instance named (fnptr)* (token 0x2000000 - invalid, module mscorlib.dll).

ldtoken method void* ()*
call cla         


        
3条回答
  •  鱼传尺愫
    2020-12-29 11:57

    It is possible to load the signature of a pointer to a function pointer:

    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);
        }
    }
    
    var fnptrPtr = GetTypeFromFieldSignature(new byte[]{6, 15, 27, 0, 0, 1});
    

    6 is field, 15 is pointer, 27 is function pointer and 0, 0, 1 is a method signature without return or parameters.

提交回复
热议问题