question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

.NET Core generated assembly references mscorlib

See original GitHub issue

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: my app.dll is generated and if I place alongside it the correct app.runtimeconfig.json I can dotnet it and “Hellow, World” is displayed. However, by using ILSpy, I can see my app.dll assembly has references to mscorlib, System.Console and System.Private.CoreLib, and btw, ILSpy is unable to locate the latter. I would have rather expected System.Runtime instead of mscorlib and System.Private.CoreLib

I’m using Mono.Cecil package Version 0.11.1

Here is the code I’m using:

using System;
using Mono.Cecil;
using Mono.Cecil.Cil;

namespace repro
{
    // Build as a.netcoreapp3.0 project
    internal sealed class Program
    {
        [STAThread]
        private static void Main()
        {
            var outputFile = @"c:\temp\app.dll";
            new Program().Run(outputFile);
        }

        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;
                mainModule.RuntimeVersion = "v4.0.30319";

                var type = new TypeDefinition("app", "Program", TypeAttributes.NotPublic | TypeAttributes.Sealed, mainModule.TypeSystem.Object)
                {
                    IsBeforeFieldInit = true
                };

                var main = new MethodDefinition("Main", MethodAttributes.Private | MethodAttributes.Static, mainModule.TypeSystem.Void);

                var writeLine = mainModule.ImportReference(typeof(Console).GetMethod("WriteLine", new[] { typeof(string) }));

                var il = main.Body.GetILProcessor();
                il.Emit(OpCodes.Nop);
                il.Emit(OpCodes.Ldstr, "Hello, World!");
                il.Emit(OpCodes.Call, writeLine);
                il.Emit(OpCodes.Nop);
                il.Emit(OpCodes.Ret);

                type.Methods.Add(main);
                mainModule.Types.Add(type);

                mainModule.EntryPoint = main;

                assemblyDefinition.EntryPoint = main;
                assemblyDefinition.Write(filename);
            }
        }
    }
}

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
jbevaincommented, Feb 4, 2020

The best way to more finely control the creation of reference is to not use the System.Type/System.Reflection overloads for ImportReference, and use the Cecil based one. If you couple that with your own assembly resolver you should have a lot more control.

0reactions
odaletcommented, Feb 4, 2020

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

You must add a reference to assembly mscorlib, version= ...
I created a new project Class Library (.NET Framework) and moved the code there instead and it worked without any issues. Share.
Read more >
You must add a reference to assembly 'mscorlib, Version= ...
I have an ASP.NET Core project that I created with Visual Studio 2015. I added a single NuGet package that is allegedly compatible...
Read more >
Reference assemblies
Learn about reference assemblies, a special type of assemblies in .NET that contain only the library's public API surface.
Read more >
You must add a reference to assembly mscorlib, version=4.0.0 ...
For MVC 5, you must add 2 references Right click the project, and add reference. Assemblies > Framework. Add this 2 references. System.IdentityModel...
Read more >
NET Core: Missing mscorlib after installing a Portable Class ...
NET Core projects can reference Portable Class Libraries if one of the .NET Standard compliant profiles is used (e.g. the profile 259).
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found