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");


    }
user3845413
  • 53
  • 1
  • 1
  • 5
  • try googling the exact error have you at least tried that first..? – MethodMan Jul 03 '16 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 – user3845413 Jul 03 '16 at 23:59
  • How about adding "using...; using...;" to the codeToCompile ? –  Jul 04 '16 at 00: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 http://stackoverflow.com/questions/793610/compilerparameters-referencedassemblies-add-reference-to-system-web-ui-webcon – MethodMan Jul 04 '16 at 00:00

3 Answers3

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);
    }
}
  • That works fine as i noted. Why is this: parameters.ReferencedAssemblies.Add("System.dll") not working? – user3845413 Jul 04 '16 at 00:15
  • 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 –  Jul 04 '16 at 00:16
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
{
.....
MoustafaS
  • 1,991
  • 11
  • 20
  • Like i said, doing that works fine.. I'm just wondering why doing this: "parameters.ReferencedAssemblies.Add("System.dll")" doesn't work – user3845413 Jul 04 '16 at 00:04
  • 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. –  Jul 04 '16 at 00:06
  • You are correct. But i believe using parameters.ReferencedAssemblies.Add() is an alternative to doing it directly in code. – user3845413 Jul 04 '16 at 00:13
  • @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. – MoustafaS Jul 04 '16 at 00:14
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).

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188