Search code examples
c#.netmethodimplattributeexternal-methods

Can anyone show me an example of MethodImplOptions.ForwardRef


It looks cool on MSDN:

Specifies that the method is declared, but its implementation is provided elsewhere.

So I tried it in a console application:

public class Program
{
    [MethodImplAttribute(MethodImplOptions.ForwardRef)]
    public static extern void Invoke();

    static void Main(string[] args)
    {
        Invoke();
        Console.Read();
    }
}

Then what should I do now? Where can I provide the implementation of Program.Invoke?


Solution

  • My understanding is that ForwardRef acts in the same way as extern, and is intended for guiding the runtime when the language you are using lacks direct support (via extern in C#). As such, the usage should be very similar to the extern modifier, most notably using [DllImport(...)].