CSharpCodeProvider 符合接口

发布于 2024-12-18 04:19:50 字数 1436 浏览 5 评论 0原文

我有一个 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");但它不起作用。任何帮助将不胜感激。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

何止钟意 2024-12-25 04:19:50

解决方案是引用当前正在执行的程序集。

var location = Assembly.GetExecutingAssembly().Location;
compilerParameters.ReferencedAssemblies.Add(location);

The solution was to reference the currently executing assembly.

var location = Assembly.GetExecutingAssembly().Location;
compilerParameters.ReferencedAssemblies.Add(location);
月寒剑心 2024-12-25 04:19:50

试试这个

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

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");
            }
        }
    }
}
剪不断理还乱 2024-12-25 04:19:50

出色的!
可能是

IDog dog = (IDog)a.CreateInstance("Dog");
dog.Bark(/*additional parameters go here*/);

Excellent!
It could be

IDog dog = (IDog)a.CreateInstance("Dog");
dog.Bark(/*additional parameters go here*/);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文