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

Use another Core Assembly with PersistedAssemblyBuilder #101018

Closed
bsdayo opened this issue Apr 14, 2024 · 7 comments · Fixed by dotnet/docs#40735
Closed

Use another Core Assembly with PersistedAssemblyBuilder #101018

bsdayo opened this issue Apr 14, 2024 · 7 comments · Fixed by dotnet/docs#40735
Labels
area-System.Reflection.Emit documentation Documentation bug or enhancement, does not impact product or test code
Milestone

Comments

@bsdayo
Copy link

bsdayo commented Apr 14, 2024

As for the examples in .NET Libraries in .NET 9 Preview 3, we can use typeof(object).Assembly as the Core Assembly to instantiate the PersistedAssemblyBuilder:

PersistedAssemblyBuilder ab = new PersistedAssemblyBuilder(new AssemblyName("MyAssembly"), typeof(object).Assembly);

But how can we use another Core Assembly, for example another version of System.Private.CoreLib? The runtime rejects loading it, according to #89215 and my own tests.

The constructor defined a parameter specifically for it and allow us to provide our own Core Assembly, I'm curious if the scenario above can be achieved, or I left something.

EDIT: I'm writing a toy compiler which needs the ability to dynamically generate IL and save .dll to disk. I'm stucking at how to let users to specify their own System.Private.CoreLib reference, just like what csc do.

@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Apr 14, 2024
Copy link
Contributor

Tagging subscribers to this area: @vitek-karas, @agocke, @VSadov
See info in area-owners.md if you want to be subscribed.

Copy link
Contributor

Tagging subscribers to this area: @vitek-karas, @agocke, @VSadov
See info in area-owners.md if you want to be subscribed.

Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-reflection-emit
See info in area-owners.md if you want to be subscribed.

@teo-tsirpanis
Copy link
Contributor

I think you can use an assembly from MetadataLoadContext.

@jkotas
Copy link
Member

jkotas commented Apr 14, 2024

I think you can use an assembly from MetadataLoadContext.

Pass the value returned by MetadataLoadContext.CoreAssembly to the PersistedAssemblyBuilder constructor.

Also, you should use types returned by the MetadataLoadContext instance throughout your compiler. For example, instead of typeof(object), you should lookup System.Object type in MetadataLoadContext.CoreAssembly by name.

I'm stucking at how to let users to specify their own System.Private.CoreLib

You should direct your uses to use reference assemblies for this purpose: https://www.nuget.org/packages/Microsoft.NETCore.App.Ref/. This package is also included as part of .NET SDK at paths like C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\8.0.4\ref\net8.0.

The core assembly is going to be System.Runtime when using reference assemblies, reference assemblies do not have System.Private.CoreLib.

@buyaa-n Could you please make sure that we have documentation and a sample for this scenario at https://learn.microsoft.com/?

@jkotas jkotas added the documentation Documentation bug or enhancement, does not impact product or test code label Apr 14, 2024
@bsdayo
Copy link
Author

bsdayo commented Apr 14, 2024

Thanks for pointing this out, I didn't even know there is a MetadataLoadContext type before. It fits my scenario perfectly and the problem has been solved. Thanks again.

@buyaa-n
Copy link
Member

buyaa-n commented Apr 15, 2024

@buyaa-n Could you please make sure that we have documentation and a sample for this scenario at https://learn.microsoft.com/

We have it explained in the conceptual doc https://learn.microsoft.com/en-us/dotnet/fundamentals/runtime-libraries/system-reflection-emit-assemblybuilder#persisted-dynamic-assemblies-in-net, need to add a sample

@buyaa-n buyaa-n removed the untriaged New issue has not been triaged by the area owner label Apr 16, 2024
@steveharter steveharter added this to the 9.0.0 milestone Apr 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-System.Reflection.Emit documentation Documentation bug or enhancement, does not impact product or test code
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

6 participants