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.