All the good time. Studying dynamic compilation I came across a problem - when adding some using to the code text of a compiled program, this code is not compiled, even if the classes from the new using themselves are not used. An example of code that is intended to compile a new program:
using System; using System.CodeDom.Compiler; using System.Collections.Generic; using Microsoft.CSharp; using System.Net.Mail; // intellisense считает ненужной //-----// Dictionary<string, string> providerOptions = new Dictionary<string, string> { {"CompilerVersion", "v3.5"} }; CSharpCodeProvider provider = new CSharpCodeProvider(providerOptions); CompilerParameters compilerParams = new CompilerParameters { OutputAssembly = "D:\\Proga.EXE", GenerateExecutable = true }; compilerParams.ReferencedAssemblies.Add("System.Net.Mail"); CompilerResults results = provider.CompileAssemblyFromSource(compilerParams, source); Here is the line with the code that compiles normally:
string source = @" using System; namespace PR { public class Prog { static void Main(string[] args) { Send.SayHello(); } } public class Send { public static void SayHello() { } } } But if we change the program very slightly, for example, we add
using System.Net.Mail; the compiler will not work with an error: Could not find the metadata file "System.Net.Mail;"
Question: what is missing in the source code to compile a project with using'es other than System and containing classes of them? And the second question: how to connect DLLs from WinApi into the program so that they compile in a new executable file?
[DllImport("mydll.dll")] 