C#2.0-extern

- 中国WEB开发者网络 (http://www.webasp.net)
-- 技术教程 (http://www.webasp.net/article/)
--- C#2.0-extern (http://www.webasp.net/article/28/27521.htm)
-- 作者:未知
-- 发布日期: 2006-04-11
extern 修饰符用于声明在外部实现的方法。extern 关键字常用于定义外部程序集别名,使得可以从单个程序集中引用同一组件的不同版本。

  extern 修饰符的常见用法是在使用 Interop 服务调入非托管代码时与 DllImport 属性一起使用;在这种情况下,该方法还必须声明为 static。如:

[DllImport("avifil32.dll")]
private static extern void AVIFileInit();

在该示例中,程序接收来自用户的字符串并将该字符串显示在消息框中。程序使用从 User32.dll 库导入的 MessageBox 方法。

using System;
using System.Runtime.InteropServices;
class MainClass 
{
    [DllImport("User32.dll")]
    public static extern int MessageBox(int h, string m, string c, int type);

    static int Main() 
    {
        string myString; 
        Console.Write("Enter your message: ");
        myString = Console.ReadLine();
        return MessageBox(0, myString, "My Message Box", 0);
    }
}


webasp.net