I have the below script that uses CSharpCodeProvider and adds necessary system libraries. I've come into a situation where I need to include the Newtonsoft.Json.dll for the compiled program. Unfortunately, even though the dll is in the bin folder, I get
Error (CS0006) Newtonsoft.Json.dll could not be found
Any ideas would be helpful for me and others who may have the same problem.
string code = Encoding.UTF8.GetString(Convert.FromBase64String(Code.code));
CSharpCodeProvider provider = new CSharpCodeProvider();
CompilerParameters parameters = new CompilerParameters();
parameters.GenerateInMemory = true;
parameters.GenerateExecutable = false;
parameters.ReferencedAssemblies.Add("System.Net.Http.dll");
parameters.ReferencedAssemblies.Add("Newtonsoft.Json.dll");
CompilerResults results = provider.CompileAssemblyFromSource(parameters, code);
if (results.Errors.HasErrors)
{
StringBuilder sb = new StringBuilder();
foreach (CompilerError error in results.Errors)
{
sb.AppendLine(String.Format("Error ({0}): {1}", error.ErrorNumber, error.ErrorText));
}
string badResult = sb.ToString();
return badResult;
}