Skip to content

Commit

Permalink
using dotnet 9 save assembly api for debugging DI IL emit
Browse files Browse the repository at this point in the history
  • Loading branch information
allantargino committed Feb 9, 2024
1 parent a79c62d commit 23e5059
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
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 '$(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

0 comments on commit 23e5059

Please sign in to comment.