0

Currently, I'm building a C#-Console that allows me to enter any C# code into a CMD for instant execution. For this approach I'm using the Roslyn compiler, because it allows me to add more code into a session object on every command, so it's allowing me to i.e. initialize anything in the first command and use it in the second command.

In this console I want to be able to access the whole .NET framework, thus I added all references to my console project and tried to iterate those references to add those into the Roslyn session:

Assembly assembly = Assembly.GetExecutingAssembly();
AssemblyName[] references = assembly.GetReferencedAssemblies().Where(x => !x.Name.StartsWith("Roslyn.")).ToArray();
ScriptEngine engine = new ScriptEngine();
Session session = engine.CreateSession();
foreach (AssemblyName reference in references)
{
    session.AddReference(reference.FullName);
}

Unfortunately, I only get a few references my console actually use. I suspect the MSBuild compiler is very neat, detects that almost all of my references won't be used in my console, thus it won't include them at all. How I can get rid of that?

I really don't wish to parse the .proj file and maintain this list separately.

Martin Braun
  • 10,906
  • 9
  • 64
  • 105
  • 2
    Yes, the compiler omits any references that are not actually used in the final assembly. That's not specific to Roslyn, all C# compilers do this. Why would you want to "get rid of that"?? You can't. – Hans Passant Feb 18 '16 at 15:30
  • You need to load the assemblies at run time. Since those assemblies are note necessarily needed by your host, you might as well maintain a list just for that and embed it as resource. – Paulo Morgado Feb 18 '16 at 21:18
  • Or create an internal class with one field of, at least, one type from those assemblies. – Paulo Morgado Feb 18 '16 at 21:18

0 Answers0