Globally importing VB.Net namespaces

551 views Asked by At

I've got a C# program this is attempting to dynamically compile some VB.Net code.

var vbc = new Microsoft.VisualBasic.VBCodeProvider(new Dictionary<string, string> 
           { { "CompilerVersion", "v4.0" } });
var parameters = new CompilerParameters(new[] {"mscorlib.dll", "System.Core.dll", 
    "System.Data.dll", "System.dll", "System.Xml.dll", "Microsoft.VisualBasic.dll"},
    @"MyFile.exe", false)
{
    GenerateInMemory = false,
    GenerateExecutable = true
};

CompilerResults results = vbc.CompileAssemblyFromFile(parameters, "file1.vb", "file2.vb", 
            "file3.vb", "file4.vb");

It's working, but with errors. The problem is, in a real Visual Basic project, by default, the following namespaces are automatically imported:

System
System.Collections
System.Collections.Generic
System.Data
System.Diagnostics
System.Linq
System.Xml.Linq
System.Threading.Tasks
Microsoft.VisualBasic

You can verify this yourself by going to create a new Visual Basic project, then looking at the properties of the project and clicking References. You will see that the above listed namespaces are automatically imported.

My question is, how do I globally auto import these namespaces and pass it into VBCodeProvider so it will automatically import these when it tries to compile?

I found a similar question to mine here, however, no answers have been provided except by the OP, who is doing something "hacky", which I prefer not to do.

0

There are 0 answers