.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方式的性能,还是要好上很多的。

 

  文章来源微信公众号

  想要更快更方便的了解相关知识,可以关注微信公众号 

posted @   刘靖凯  阅读(0)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 一文读懂知识蒸馏
· 终于写完轮子一部分:tcp代理 了,记录一下
历史上的今天:
2017-02-16 异常处理语句
2017-02-16 五种跳转的语句
2017-02-16 for循环循环时间
点击右上角即可分享
微信分享提示