.net8中的extern
extern 是C#中的一个关键字,用于声明在C#代码中引用外部程序集中的函数或方法。通常,extern 用于与非托管代码(如C/C++编写的DLL)进行交互,以便在C#中调用非托管代码中的函数。
MessageBox(IntPtr.Zero, "你好extern!", "信息", 0); [DllImport("user32.dll")] static extern int MessageBox(IntPtr hWnd, string text, string caption, uint type);
运行结果:
.net8中,丰富了extern的使用,用UnsafeAccessor可以很简单地调用.net写dll了。
using System.Runtime.CompilerServices; using System.Runtime.InteropServices; var test = new TestClass(); //调用私有方法 Console.WriteLine(GetTime(test)); //读取私有字面 Console.WriteLine(GetNo(test)); //赋值给私有字段 GetNo(test) = 20; Console.WriteLine(GetNo(test)); [UnsafeAccessor(UnsafeAccessorKind.Method, Name = "GetTime")] static extern DateTime GetTime(TestClass test); [UnsafeAccessor(UnsafeAccessorKind.Field, Name = "_no")] static extern ref int GetNo(TestClass test); public class TestClass { int _no = 10; DateTime GetTime() { return DateTime.Now; } }
现在,UnsafeAccessorKind支持的类型如下:
namespace System.Runtime.CompilerServices { public enum UnsafeAccessorKind { Constructor = 0, Method = 1, StaticMethod = 2, Field = 3, StaticField = 4 } }
单看这个功能,有点像反射,但要比反射简单,只需要标志签名即可。
using BenchmarkDotNet.Attributes; using BenchmarkDotNet.Running; using System.Reflection; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using static System.Net.Mime.MediaTypeNames; BenchmarkRunner.Run<Test>(); public class Test { [UnsafeAccessor(UnsafeAccessorKind.Method, Name = "GetTime")] public static extern DateTime GetTime(TestClass test); [UnsafeAccessor(UnsafeAccessorKind.Field, Name = "_no")] public static extern ref int GetNo(TestClass test); [Benchmark] public void UnsafeTest() { var test = new TestClass(); var t = GetTime(test); } [Benchmark] public void RefTest() { var test = new TestClass(); var type = test.GetType(); var method=type.GetMethod("GetTime", BindingFlags.NonPublic | BindingFlags.Instance); var t = method?.Invoke(test, new object[0]); } } public class TestClass { int _no = 10; DateTime GetTime() { return DateTime.Now; } }
下面是用BechamrkDotNet比较反射和UnsafeAccessor方式的性能,还是要好上很多的。
文章来源微信公众号
想要更快更方便的了解相关知识,可以关注微信公众号
****欢迎关注我的asp.net core系统课程****
《asp.net core精要讲解》 https://ke.qq.com/course/265696
《asp.net core 3.0》 https://ke.qq.com/course/437517
《asp.net core项目实战》 https://ke.qq.com/course/291868
《基于.net core微服务》 https://ke.qq.com/course/299524
《asp.net core精要讲解》 https://ke.qq.com/course/265696
《asp.net core 3.0》 https://ke.qq.com/course/437517
《asp.net core项目实战》 https://ke.qq.com/course/291868
《基于.net core微服务》 https://ke.qq.com/course/299524
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?