首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >dotnet 测试 AOT 的 API 行为

dotnet 测试 AOT 的 API 行为

作者头像
林德熙
发布2025-07-10 15:45:49
发布2025-07-10 15:45:49
9500
代码可运行
举报
文章被收录于专栏:林德熙的博客林德熙的博客
运行总次数:0
代码可运行

本文记录我测试 AOT 的 API 行为

判断当前代码正在 AOT 模式下运行:

代码语言:javascript
代码运行次数:0
运行
复制
RuntimeFeature.IsDynamicCodeSupported

获取入口程序集 Assembly.GetEntryAssembly() 返回非空,返回真的入口程序集。此行为和单元测试下不同,单元测试下返回空

获取调用当前方法的程序集 Assembly.GetCallingAssembly 可能抛出 PlatformNotSupportedException 异常

获取正在执行的程序集 Assembly.GetExecutingAssembly 返回非空

测试代码如下

代码语言:javascript
代码运行次数:0
运行
复制
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;

Console.WriteLine($"RuntimeFeature.IsDynamicCodeSupported={RuntimeFeature.IsDynamicCodeSupported}");

Console.WriteLine($"Assembly.GetEntryAssembly() is not null={Assembly.GetEntryAssembly() is not null}");
try
{
    Console.WriteLine($"Assembly.GetCallingAssembly() is not null={Assembly.GetCallingAssembly() is not null}");
}
catch (Exception e)
{
    Console.WriteLine($"Assembly.GetCallingAssembly() Exception. {e.GetType().FullName}: {e.Message}");
}

Console.WriteLine($"Assembly.GetExecutingAssembly() is not null={Assembly.GetExecutingAssembly() is not null}");

Console.WriteLine($"Assembly.GetEntryAssembly()?.GetCustomAttribute<DebuggableAttribute>() is not null={Assembly.GetEntryAssembly()?.GetCustomAttribute<DebuggableAttribute>() is not null}");

以上代码的 AOT 输出内容如下

代码语言:javascript
代码运行次数:0
运行
复制
RuntimeFeature.IsDynamicCodeSupported=False
Assembly.GetEntryAssembly() is not null=True
Assembly.GetCallingAssembly() Exception. System.PlatformNotSupportedException: Operation is not supported on this platform.
Assembly.GetExecutingAssembly() is not null=True
Assembly.GetEntryAssembly()?.GetCustomAttribute<DebuggableAttribute>() is not null=False

本文以上代码放在 github 和 gitee 上,可以使用如下命令行拉取代码。我整个代码仓库比较庞大,使用以下命令行可以进行部分拉取,拉取速度比较快

先创建一个空文件夹,接着使用命令行 cd 命令进入此空文件夹,在命令行里面输入以下代码,即可获取到本文的代码

代码语言:javascript
代码运行次数:0
运行
复制
git init
git remote add origin https://gitee.com/lindexi/lindexi_gd.git
git pull origin 0295a1d5ac9c60903fe0674859b83d421bbf1a08

以上使用的是国内的 gitee 的源,如果 gitee 不能访问,请替换为 github 的源。请在命令行继续输入以下代码,将 gitee 源换成 github 源进行拉取代码。如果依然拉取不到代码,可以发邮件向我要代码

代码语言:javascript
代码运行次数:0
运行
复制
git remote remove origin
git remote add origin https://github.com/lindexi/lindexi_gd.git
git pull origin 0295a1d5ac9c60903fe0674859b83d421bbf1a08

获取代码之后,进入 Workbench/NufubijemlerWabearnagaqem 文件夹,即可获取到源代码

更多技术博客,请参阅 博客导航

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档