http://msdn.microsoft.com/zh-cn/library/e59b22c5.aspx
小结如下:
extern一般用于system.runtime.interoperservices命名空间
extern必须且仅于static搭配使用,不能与abstract共用
extern表明在c#的代码中调用外部程序的某个方法(此外部程序可能是c,或者c++)
extern与dllimport一并使用,dllimport用于导入要使用的外部程序集的dll
摘选小例:
C#
//using System.Runtime.InteropServices; class ExternTest {
//通过dllimport导入user32.dll外部程序集
[DllImport("User32.dll", CharSet=CharSet.Unicode)] 外部程序集的方法messagebox,注意static与extern须一并出现
public static extern int MessageBox(IntPtr h, string m, string c, int type); static int Main() { string myString; Console.Write("Enter your message: "); myString = Console.ReadLine();
//调用来自于user32.dll中的方法messagebox
return MessageBox((IntPtr)0, myString, "My Message Box", 0); } }
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/9240380/viewspace-707615/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/9240380/viewspace-707615/