惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - 黄永泰

.NET Framework 4与.NET Framework 4 Client Profile 的区别? 《C#与.NET 4高级程序设计:第5版》这本书如何? *.docx文件打开方法 试图运行项目时出错,无法启动调试。没有正确安装调试器,请运行安装程序安装或恢复调试器。 System.ArgumentException: 另一个SqlParameterCollection中已包含SqlParameter。 买了几本书。 给Visual Studio 2010换皮肤。 请教一个程序自动升级的写法. 支持掘金,支持"甜瓜"安东尼. NBA 2009-10赛季赛程表 IList<T>、List<T>、BindingList<T>与dataGridView的绑定问题(请教)。 IList<T>、List<T>、BindingList<T>与dataGridView的绑定问题。 近期及今后一段时间的学习目标. 数据库从Sql server 2000升级到2005后,遇到一点问题。 请教: sql server 中动态创建命令语句 大家继续努力! 近期.net学习计划。 谢谢大家的指教! - 黄永泰 关于“我的藏书阁:.NET/数据库应用开发”的几点看法。 - 黄永泰
extern修饰符的使用(C#)
黄永泰 · 2011-06-28 · via 博客园 - 黄永泰

    很多C#的书都没讲到extern修饰符的使用,查了一下MSDN,整理出来存档。

    extern 修饰符用于声明在外部实现的方法。 extern 修饰符的常见用法是在使用 Interop 服务调入非托管代码时与 DllImport 特性一起使用。 在这种情况下,还必须将方法声明为 static,如下示例所示:

[DllImport("avifil32.dll")]

private static extern void AVIFileInit();

注意:

1extern 关键字还可以定义外部程序集别名,使得可以从单个程序集中引用同一组件的不同版本。

2、将 abstract extern 修饰符一起使用来修改同一成员是错误的。 使用 extern 修饰符意味着方法在 C# 代码的外部实现,而使用 abstract 修饰符意味着在类中未提供方法实现。

3extern 关键字在使用上比在 C++ 中有更多限制。

示例:

在该示例中,程序接收来自用户的字符串并将该字符串显示在消息框中。程序使用从 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);

   }

}

此示例使用 C 程序创建一个 DLL,在下一示例中将从 C# 程序调用该 DLL

// cmdll.c

// compile with: /LD

int __declspec(dllexport) SampleMethod(int i)

{

   return i*10;

}

该示例使用两个文件 CM.cs Cmdll.c 来说明 externC 文件是示例 2 中创建的外部 DLL,它从 C# 程序内调用。

// cm.cs

using System;

using System.Runtime.InteropServices;

public class MainClass

{

   [DllImport("Cmdll.dll")]

   public static extern int SampleMethod(int x);

   static void Main()

   {

      Console.WriteLine("SampleMethod() returns {0}.", SampleMethod(5));

   }

}

输出:

SampleMethod() returns 50.

备注:

生成项目:

使用 Visual C++ 命令行将 Cmdll.c 编译为 DLL

cl /LD Cmdll.c

使用命令行编译 CM.cs

csc CM.cs

这将创建可执行文件 CM.exe。运行此程序时,SampleMethod 将值 5 传递到 DLL 文件,该文件将此值乘以 10 返回。

参考自:http://msdn.microsoft.com/zh-cn/library/e59b22c5(v=vs.80).aspx