C#语言extern
示例
所述extern关键字被用于声明是外部实现的方法。可以将其与DllImport属性结合使用,以使用Interop服务调用非托管代码。在这种情况下,它将带有static修饰符
例如:
using System.Runtime.InteropServices;
public class MyClass
{
[DllImport("User32.dll")]
private static extern int SetForegroundWindow(IntPtr point);
public void ActivateProcessWindow(Process p)
{
SetForegroundWindow(p.MainWindowHandle);
}
}这使用从User32.dll库中导入的SetForegroundWindow方法
这也可以用于定义外部程序集别名。这使我们可以从单个装配中引用相同组件的不同版本。
要引用具有完全相同的类型名称的两个程序集,必须在命令提示符下指定别名,如下所示:
/r:GridV1=grid.dll /r:GridV2=grid20.dll
这将创建外部别名GridV1和GridV2。要在程序中使用这些别名,请使用extern关键字引用它们。例如:
extern alias GridV1; extern alias GridV2;
热门推荐
10 朋友新年祝福语大全 简短
11 新年祝福语简短大方兔年
12 搬新家礼物祝福语简短
13 同学见面花束祝福语简短
14 五一假期祝福语幽默简短
15 离职欢送敬酒祝福语简短
16 对学弟的祝福语简短
17 考老师辞职祝福语简短
18 祝福语驱散霉运的话简短