CSharpCodeProvider Соответствует интерфейсу - PullRequest
3 голосов
/ 25 ноября 2011

У меня есть CSharpCodeProvider, который принимает код для класса.В проекте, где код компилируется, у меня есть интерфейс.Я хотел бы, чтобы код, который я собираю, соответствовал этому интерфейсу.

Вот самый простой пример, который я могу придумать, чтобы проиллюстрировать мою проблему.У меня есть проект с двумя файлами:

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();
    }
}

Не могу понять, какполучите CSharpCodeProvider для распознавания IDog.Я пытался compilerParameters.ReferencedAssemblies.Add("ConsoleApplication1");, но это не сработало.Любая помощь будет оценена.

Ответы [ 3 ]

1 голос
/ 13 декабря 2011

Решение состояло в том, чтобы сослаться на текущую сборку.

var location = Assembly.GetExecutingAssembly().Location;
compilerParameters.ReferencedAssemblies.Add(location);
0 голосов
/ 04 апреля 2012

Отлично! Это может быть

IDog dog = (IDog)a.CreateInstance("Dog");
dog.Bark(/*additional parameters go here*/);
0 голосов
/ 25 ноября 2011

попробуйте

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