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 生日广州男生祝福语简短