Search code examples
c#.net.net-assemblybytecodecil

How to interpret FNPTR from Type blob signature?


How should I interpret FNPTR followed by a MethodDefSig or by a MethodRefSig? I mean that BOOLEAN is bool, OBJECT is object, SZARRAY is a zero-based array, but FNPTR have a method signature and I can't write something like:

public static int*(int) myFunction();

Can anyone explain to me how it works?


Solution

  • FNPTR is a signature element denoting a low-level pointer to a managed function which can be used by the calli opcode, for example. As far as I know, only C++/CLI has a use for them (to support standard C++ function pointers and references), and I suppose low-level languages were the reason they were included in CIL in the first place.

    Despite being a core feature of CLI, there is zero support for them in the reflection API (apart from this thing). They have no actual use apart from creating a distinct signature for method overloads, so you can safely use IntPtr if you are able to.

    Function pointers are basically a low-level equivalent of delegates, only without the target object (and not being objects themselves). If you need a high-level equivalent, Action and Func delegates might be sufficient.