0
votes

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;
}
1

1 Answers

0
votes

I think Newtonsoft is not registered in the GAC.

The compiler is saying it can't find the assembly, so have you tried giving it the full path to the Newtonsoft DLL?

parameters.ReferencedAssemblies.Add(@"c:\somepath\Newtonsoft.Json.dll");