3

I have a CSharpCodeProvider which takes in the code for a class. In the project where the code is being compiled, I have an interface. I would like the code I'm compiling to conform to this interface.

Here's the simplest example I could think of to illustrate my problem. I have a project with two files:

Program.cs:

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            //set up the compiler
            CSharpCodeProvider csCompiler = new CSharpCodeProvider();

            CompilerParameters compilerParameters = new CompilerParameters();
            compilerParameters.GenerateInMemory = true;
            compilerParameters.GenerateExecutable = false;

            var definition = 
@"class Dog : IDog
{
    public void Bark()
    {
        //woof 
    }
}";    
            CompilerResults results = csCompiler.CompileAssemblyFromSource(compilerParameters, new string[1] { definition });

            IDog dog = null;
            if (results.Errors.Count == 0)
            {
                Assembly assembly = results.CompiledAssembly;
                dog = assembly.CreateInstance("TwoDimensionalCellularAutomatonDelegate") as IDog;
            }

            if (dog == null)
                dog.Bark();
        }
    }
}

IDog.cs:

namespace ConsoleApplication1
{
    interface IDog
    {
        void Bark();
    }
}

I can't seem to figure out how to get the CSharpCodeProvider to recognize IDog. I tried compilerParameters.ReferencedAssemblies.Add("ConsoleApplication1"); but it didn't work. Any help would be appreciated.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
LandonSchropp
  • 10,084
  • 22
  • 86
  • 149
  • I think you need to have the same assembly at compile time. in other words, `csc /r:IDoc.dll dog.cs` is NG. `csc dog.cs IDoc.cs` is OK. – BLUEPIXY Nov 25 '11 at 14:56
  • I'm not quite following what you're saying. Could you go into a little more detail please? – LandonSchropp Nov 25 '11 at 15:22

3 Answers3

1

The solution was to reference the currently executing assembly.

var location = Assembly.GetExecutingAssembly().Location;
compilerParameters.ReferencedAssemblies.Add(location);
LandonSchropp
  • 10,084
  • 22
  • 86
  • 149
0

Excellent! It could be

IDog dog = (IDog)a.CreateInstance("Dog");
dog.Bark(/*additional parameters go here*/);
Himanshu
  • 31,810
  • 31
  • 111
  • 133
edguan
  • 1
0

try this

using System;
using System.IO;
using System.CodeDom.Compiler;
using System.Reflection;
using Microsoft.CSharp;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            //set up the compiler
            CSharpCodeProvider csCompiler = new CSharpCodeProvider();

            CompilerParameters compilerParameters = new CompilerParameters();
            compilerParameters.GenerateInMemory = true;
            compilerParameters.GenerateExecutable = false;
            compilerParameters.ReferencedAssemblies.Add("System.dll");

            var definition = File.ReadAllText("./IDog.cs") +
@"public class Dog : ConsoleApplication1.IDog
{
    public void Bark()
    {
        System.Console.WriteLine(""BowWoW"");
    }
}";
            CompilerResults results = csCompiler.CompileAssemblyFromSource(compilerParameters,
                new string[1] { definition });

            dynamic dog = null;
            if (results.Errors.Count == 0)
            {
                Assembly assembly = results.CompiledAssembly;
//              Type idog = assembly.GetType("ConsoleApplication1.IDog");
                dog = assembly.CreateInstance("Dog");
            } else {
                Console.WriteLine("Has Errors");
                foreach(CompilerError err in results.Errors){
                    Console.WriteLine(err.ErrorText);
                }
            }
            if (dog != null){
                dog.Bark();
            } else {
                System.Console.WriteLine("null");
            }
        }
    }
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70