Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using dotnet 9's save assembly api for debugging DI IL emit #98234

Merged
merged 2 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>$(NetCoreAppCurrent);$(NetCoreAppPrevious);$(NetCoreAppMinimum);netstandard2.1;netstandard2.0;$(NetFrameworkMinimum)</TargetFrameworks>
Expand All @@ -14,8 +14,8 @@
<PropertyGroup>
<ILEmitBackend Condition="'$(TargetFramework)' != 'netstandard2.0'">true</ILEmitBackend>
<DefineConstants Condition="'$(ILEmitBackend)' == 'true'">$(DefineConstants);IL_EMIT</DefineConstants>
<DefineConstants Condition="$([MSBuild]::GetTargetFrameworkIdentifier('$(TargetFramework)')) == '.NETFramework' and
'$(ILEmitBackendSaveAssemblies)' == 'true'">$(DefineConstants);SAVE_ASSEMBLIES</DefineConstants>
<DefineConstants Condition="($([MSBuild]::GetTargetFrameworkIdentifier('$(TargetFramework)')) == '.NETFramework' or $([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net9.0'))) and
'$(ILEmitBackendSaveAssemblies)' == 'true'">$(DefineConstants);SAVE_ASSEMBLIES</DefineConstants>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,13 @@ private GeneratedMethod BuildTypeNoCache(ServiceCallSite callSite)
var assemblyName = "Test" + DateTime.Now.Ticks;
var fileName = assemblyName + ".dll";

#if NETFRAMEWORK
var assembly = AssemblyBuilder.DefineDynamicAssembly(new AssemblyName(assemblyName), AssemblyBuilderAccess.RunAndSave);
var module = assembly.DefineDynamicModule(assemblyName, fileName);
#else
var assembly = AssemblyBuilder.DefinePersistedAssembly(new AssemblyName(assemblyName), typeof(object).Assembly);
var module = assembly.DefineDynamicModule(assemblyName);
#endif
var type = module.DefineType(callSite.ServiceType.Name + "Resolver");

var method = type.DefineMethod(
Expand All @@ -114,7 +119,6 @@ private GeneratedMethod BuildTypeNoCache(ServiceCallSite callSite)

GenerateMethodBody(callSite, method.GetILGenerator());
type.CreateTypeInfo();
// Assembly.Save is only available in .NET Framework (https://github.com/dotnet/runtime/issues/15704)
assembly.Save(fileName);
#endif
DependencyInjectionEventSource.Log.DynamicMethodBuilt(_rootScope.RootProvider, callSite.ServiceType, ilGenerator.ILOffset);
Expand Down Expand Up @@ -179,7 +183,7 @@ private GeneratedMethod BuildTypeNoCache(ServiceCallSite callSite)
AddConstant(argument, generatedMethod.Lambda);
// ProviderScope
argument.Generator.Emit(OpCodes.Ldarg_1);
argument.Generator.Emit(OpCodes.Call, generatedMethod.Lambda.GetType().GetMethod("Invoke"));
argument.Generator.Emit(OpCodes.Call, generatedMethod.Lambda.GetType().GetMethod("Invoke")!);
#else
AddConstant(argument, generatedMethod.Context);
// ProviderScope
Expand Down
Loading