-
-
Notifications
You must be signed in to change notification settings - Fork 638
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
.NET Core generated assembly references mscorlib #646
Comments
Oh, I just noticed my issue is probably related to #524... private void Run(string filename)
{
const string assemblyAndMainModuleName = "app";
var assemblyNameDefinition = new AssemblyNameDefinition(assemblyAndMainModuleName, new Version(1, 0, 0, 0));
using (var assemblyDefinition = AssemblyDefinition.CreateAssembly(assemblyNameDefinition, assemblyAndMainModuleName, ModuleKind.Console))
{
var mainModule = assemblyDefinition.MainModule;
var type = new TypeDefinition("app", "Program", TypeAttributes.NotPublic | TypeAttributes.Sealed, mainModule.TypeSystem.Object)
{
IsBeforeFieldInit = true
};
mainModule.Types.Add(type);
assemblyDefinition.Write(filename);
}
} The resulting assembly still references |
That's just the default behavior of the https://github.com/jbevain/cecil/blob/master/Mono.Cecil/TypeSystem.cs#L93 Cecil simply doesn't know which runtime you're targeting, and where Before creating any type, create an AssemblyNameReference for |
Thanks, that's what I ended up doing: var corlibReference = new AssemblyNameReference("System.Runtime", new Version(4, 2, 1, 0))
{
PublicKeyToken = new byte[] { 0xb0, 0x3f, 0x5f, 0x7f, 0x11, 0xd5, 0x0a, 0x3a }
};
...
mainModule.AssemblyReferences.Add(corlibReference); So, now my references make more sense, but still I wonder: my assembly now has explicit references to:
whereas an assembly built with the C# compiler and doing roughly the same thing has no explicit reference to I suppose there is some |
I think it's because of the line:
That's a perfectly valid way of creating a reference scoped for the module, however it's using reflection to get the member and assembly data. At that point, it seems that the assembly of the type Console is I'm not smart enough to understand the difference System.Runtime, System.Private.CoreLib and mscorlib accross multiple platforms, so I'd rather not implement something in Cecil that tries to be smart and smoothes references to core assemblies. However, by now I think that System.Runtime is only typeforwarders on .NET Framework and on .NET Core. On .NET Framework, Object lives in |
Thanks for sharing your thoughts! This confirms what I suspected by reading the code in https://github.com/jbevain/cecil/blob/master/Mono.Cecil/Import.cs If I really wanted to fine tune my assembly references, I suppose I'd have to somehow preprocess a set of "reference" assemblies into a huge list of type references so that every type I need would be resolved against this list instead of relying upon reflection (that by design will inspect the generator application's assemblies). This approach could open some interesting scenarios, such as being able to target netfx/netcore/netstandard from any framework... This would still be a huge amount of work! Btw, I'm pretty sure the ratio benefit/investment would be a better reason for not carrying up such a task and not you being not smart enough; this great lib of yours proves it! Thanks again. I'm closing this as you graciously answered all I was wondering. |
The best way to more finely control the creation of reference is to not use the System.Type/System.Reflection overloads for |
I'm trying to generate an assembly using Cecil completely from scratch. For now, the generated application is simply a Hello World resembling many examples I found on the web.
I'm building my code by targetting
netcoreapp3.0
. Everything seems: myapp.dll
is generated and if I place alongside it the correctapp.runtimeconfig.json
I can dotnet it and "Hellow, World" is displayed.However, by using ILSpy, I can see my
app.dll
assembly has references tomscorlib
,System.Console
andSystem.Private.CoreLib
, and btw, ILSpy is unable to locate the latter.I would have rather expected
System.Runtime
instead ofmscorlib
andSystem.Private.CoreLib
...I'm using Mono.Cecil package Version 0.11.1
Here is the code I'm using:
The text was updated successfully, but these errors were encountered: