[DllImport("xyz.dll")] C# Expression/Statement Mode

static extern void func();

works in C# Program Mode only
Can I use it in Expression / Statement Mode somehow?


  • I believe the import is really just a declaration of a method that must exist within a class. I'm not sure if it can really be declared at the top-level, even with latest c# versions. In program mode, everything is place in a class (UserQuery) so it will work there. The same cannot be said for the other modes.

  • The following code works for me in Statements mode in LINQPad 7:

    MessageBox (IntPtr.Zero, "Test", "", 0);
    [DllImport ("user32.dll")]
    static extern int MessageBox (IntPtr hWnd, string text, string caption, int type);

    If you want to call external methods in Expression mode, you can so do by defining the external methods in a class in the "My Extensions" query:

    public static class Native
        [System.Runtime.InteropServices.DllImport ("xyz.dll")]
        public static extern void func();
  • In LINQPad 7 it works for me too, not in 5 I was using.

    Probably because of that new function at Top-Level feature in recent C# / .Net

Sign In or Register to comment.