.NET MAUI 8.0 iOS调试报错:Dynamic code generation is not supported on this platform
解决.NET MAUI 8.0 iOS平台升级至.NET 8.0.7后的动态代码生成异常
升级.NET MAUI 8.0应用到.NET 8.0.7运行环境后,iOS平台抛出Dynamic code generation is not supported on this platform.异常,具体堆栈信息如下:
Exception has occurred: CLR/System.PlatformNotSupportedException An unhandled exception of type 'System.PlatformNotSupportedException' occurred in System.Private.CoreLib.dll: 'Dynamic code generation is not supported on this platform.' at System.Reflection.Emit.AssemblyBuilder.ThrowDynamicCodeNotSupported() at System.Reflection.Emit.AssemblyBuilder.EnsureDynamicCodeSupported() at System.Reflection.Emit.DynamicMethod.Init(String name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] signature, Type owner, Module m, Boolean skipVisibility, Boolean transparentMethod) at System.Reflection.Emit.DynamicMethod..ctor(String name, Type returnType, Type[] parameterTypes, Type owner, Boolean skipVisibility) at DryIoc.FastExpressionCompiler.LightExpression.ExpressionCompiler.TryCompileBoundToFirstClosureParam(Type delegateType, Expression bodyExpr, IParameterProvider paramExprs, Type[] closurePlusParamTypes, Type returnType, CompilerFlags flags) at DryIoc.FactoryDelegateCompiler.CompileToFactoryDelegate(Expression expression, Boolean preferInterpretation) at DryIoc.Container.System.IServiceProvider.GetService(Type serviceType) at Microsoft.Maui.MauiContext.WrappedServiceProvider.GetService(Type serviceType) at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetService[T](IServiceProvider provider) at Microsoft.Maui.Controls.DispatcherExtensions.FindDispatcher(BindableObject bindableObject) at Microsoft.VisualStudio.DesignTools.MauiTap.MauiHelpers.GetDispatcher(BindableObject bindableObject) at Microsoft.VisualStudio.DesignTools.MauiTap.MauiHelpers.GetDispatcher(IWindow window) at Microsoft.VisualStudio.DesignTools.MauiTap.ElementSelectionManager.RegisterWindow(IWindow window) at Microsoft.VisualStudio.DesignTools.MauiTap.ElementSelectionManager.Initialize() at Microsoft.Maui.Dispatching.Dispatcher.<>c__DisplayClass9_0.<DispatchImplementation>b__0() at CoreFoundation.DispatchQueue.static_dispatcher_to_managed(IntPtr context) at UIKit.UIApplication.xamarin_UIApplicationMain(Int32 argc, IntPtr argv, IntPtr principalClassName, IntPtr delegateClassName, IntPtr& gchandle) at UIKit.UIApplication.UIApplicationMain(Int32 argc, String[] argv, IntPtr principalClassName, IntPtr delegateClassName) at UIKit.UIApplication.Main(String[] args, Type principalClass, Type delegateClass)
解决方案
禁用DryIoc的表达式编译
异常根源是DryIoc尝试使用依赖动态代码生成的表达式编译器,而iOS平台限制了该功能。可以在配置DryIoc容器时强制使用解释模式,避免触发动态代码生成:var container = new Container(rules => rules.With(FactoryMethod.Interpret));或者使用“编译失败则回退解释”的模式:
var container = new Container(rules => rules.With(FactoryMethod.CompileIfFails | FactoryMethod.Interpret));排查Visual Studio调试工具冲突
堆栈中出现Microsoft.VisualStudio.DesignTools.MauiTap相关代码,说明可能是VS的MAUI调试工具触发了问题:- 将Visual Studio更新到与.NET 8.0.7兼容的最新版本
- 暂时关闭XAML热重载或设计时预览功能,验证是否能绕过异常
- 尝试以非调试模式运行应用,确认是否仅在调试场景出现问题
检查iOS项目配置
确认iOS项目的「iOS Build」设置中:- 未勾选「Enable dynamic code generation」(iOS默认禁用,误开启会引发冲突)
- 若启用了AOT编译,确保代码中没有依赖动态代码生成的逻辑,AOT环境下该功能本身不被支持
内容的提问来源于stack exchange,提问作者Anand
相关产品推荐
相关产品推荐

