1

I have added "System.dll" to the compiler parameter referenced assemblies.I also noticed that adding to "Using System" to the codeToCompile OR using "System.Math" or "System.Double" works fine.Not sure what's wrong.

    using Microsoft.CSharp;
    using System;
    using System.CodeDom.Compiler;
    using System.Text;
    using System.Windows.Forms;

     private void onLoadPlugin(object sender, EventArgs e)
    {
        string codeToCompile =
     @"

class TestPlugin
{
    public string ArithmeticOperator
    {
        get { return ""X^2""; }
    }
    public double PerformCalculation(string value)
    {
        Double var = Double.Parse(value);
        if (var == 0)
            return 0;
        return Math.Pow(var, 2);
    }
}
        ";

        CSharpCodeProvider provider = new CSharpCodeProvider();//new Dictionary<String, String> { { "CompilerVersion", "v4.0" } });
        CompilerParameters parameters = new CompilerParameters();
        parameters.ReferencedAssemblies.Add("System.dll");//This doesn't seem to be working

            parameters.GenerateInMemory = false;
            parameters.GenerateExecutable = false;
            parameters.OutputAssembly = "TestPlugin.dll";

        CompilerResults results = provider.CompileAssemblyFromSource(parameters, codeToCompile);
        if (results.Errors.Count != 0)
            throw new Exception("Mission Failed");


    }
4
  • try googling the exact error have you at least tried that first..? Commented Jul 3, 2016 at 23:56
  • Tried that. Found a couple "Type 'Double' and 'Math' could not be found/does not exist in current context" errors but none in relation to Runtime Compilation Commented Jul 3, 2016 at 23:59
  • How about adding "using...; using...;" to the codeToCompile ? Commented Jul 4, 2016 at 0:00
  • what does your using section in the class header look like can you edit your code and show us..? look at this post as well which uses .AddRange vs .Add function stackoverflow.com/questions/793610/… Commented Jul 4, 2016 at 0:00

3 Answers 3

3

Using "using...":

using System;
class TestPlugin
{
    public string ArithmeticOperator
    {
        get { return ""X^2""; }
    }
    public double PerformCalculation(string value)
    {
        Double var = Double.Parse(value);
        if (var == 0)
            return 0;
        return Math.Pow(var, 2);
    }
}

or not using "using...":

class TestPlugin
{
    public string ArithmeticOperator
    {
        get { return ""X^2""; }
    }
    public double PerformCalculation(string value)
    {
        System.Double var = System.Double.Parse(value);
        if (var == 0)
            return 0;
        return System.Math.Pow(var, 2);
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

That works fine as i noted. Why is this: parameters.ReferencedAssemblies.Add("System.dll") not working?
It's like only adding an assembly System.dll but forget to write "using System;" -> VS2015 will show it Red... compiler error... Try to create a new project, add System.dll in it, and dont use "using System;" -> cannot be compiled. In System.dll there are System.Double and System.Math but not Double and Math
1

For each class you use in .net you must do 2 things: 1) Reference its assembly 2) add using statement for it, or type the fully qualified name eg: System.Math

There is no class in the global in .net framework, each class is within some assembly (Namespace)

Add "using System;" at the top of your code to be like this:

string codeToCompile =
     @"
using System;
class TestPlugin
{
.....

4 Comments

Like i said, doing that works fine.. I'm just wondering why doing this: "parameters.ReferencedAssemblies.Add("System.dll")" doesn't work
You just adding "System.dll" but you must call also System.Double instead of Double. Because of the namespace. If you are not using "using System;" and calling only Double, where is Double ? There is no Double, there is only System.Double.
You are correct. But i believe using parameters.ReferencedAssemblies.Add() is an alternative to doing it directly in code.
@user3845413 No, its not, try yourself, try yourself, create new project, add reference to some assembly using the VS wizard, and then try calling a class of it without typing "using", you will see it complain even though you already added reference.
1

The base types like System.Double are in mscorlib.dll.

You can see that if you add the option "View Containers" in the Object Browser.

Note that System.dll and System.Core.dll add additional types to namespaces already existing in mscorlib.dll. So you don't have a 1-to-1 relationship between namespaces and DLLs.

But probably mscorlib.dll is added by default. If you are working with the C# aliases string, double, you are okay without explicitly mentioning the System namespace, but otherwise you need either a using System; or qualify the types with it (System.Double, System.Math.Pow).

1 Comment

Tried it parameters.ReferencedAssemblies.Add("mscorlib.dll"); Did not work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.