使用CompilerParameters引用当前程序集 [英] Referencing current assembly with CompilerParameters

查看:144
本文介绍了使用CompilerParameters引用当前程序集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,我正在一个项目上,团队希望找到一种无需重新编译整个项目即可编写代码和对其进行编辑的方法,因此,我决定尝试实现脚本引擎.

Right now I'm working on a project, and the team wants a way to write code and edit it without having to recompile the whole project, so I've decided to try and implement a scripting engine.

之前,我已经在C ++中实现了Lua,但我并不是将脚本功能实现到项目中的新手.但是,我们想尝试使用Microsoft.CSharp命名空间结合已经内置到C#中的System.Reflection来实现直接的C#.

Having implemented Lua into C++ before, I wasn't an entire newbie to implementing scripting capabilities into projects. However, we wanted to try and implement straight C# using the Microsoft.CSharp namespace, combined with System.Reflection that was already built in to C#.

因此,听说此事后,我在文档中打了个招呼,并提出了一个ALMOST可以工作的原型-但效果不尽人意.

So having hearing about this, I poked about in docs and I've come up with a prototype that ALMOST works - but doesn't quite.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.CSharp;
using System.CodeDom.Compiler;
using System.Reflection;

namespace Scripting
{
    class Program
    {
        static void Main(string[] args)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("using System;");
            builder.Append("using Scripting;");
            builder.Append("class MyScript : IScript");
            builder.Append("{");
            builder.Append("    string ScriptName");
            builder.Append("    {");
            builder.Append("        get { return \"My Script\"; }");
            builder.Append("    }");

            builder.Append("    public bool Initialize()");
            builder.Append("    {");
            builder.Append("        Console.WriteLine(\"Hello, World!\");");
            builder.Append("        return true;");
            builder.Append("    }");
            builder.Append("}");

            CSharpCodeProvider provider = new CSharpCodeProvider();
            CompilerParameters param = new CompilerParameters(new string[] { "System.dll", "Scripting.dll" });
            param.GenerateInMemory = true;
            param.GenerateExecutable = true;
            CompilerResults result = provider.CompileAssemblyFromSource(param, builder.ToString());
            if (result.Errors.Count > 0)
            {
                foreach (CompilerError error in result.Errors)
                    Console.WriteLine(error);
                Console.ReadKey();
                return;
            }
        }
    }
}

我目前遇到的问题是,我希望能够引用我的接口-IScript.cs(位于Scripting名称空间内,因此位于当前程序集内),以便在编译器中编写和解析的脚本可以访问它.显然,我添加了Scripting.dll作为参数,但是由于某种原因似乎无法访问它.我 am 在调试中运行它,所以这可能是造成一些主要面部表情的原因.怎么办?

The issue I have at the moment is that I want to be able to reference my interface - IScript.cs (which is inside the Scripting namespace and thus, the current assembly) - so that scripts written and parsed in the compiler can access it. Obviously, I added Scripting.dll as a parameter, but it doesn't seem to be able to be accessed for some reason or another. I am running it in debug so this could be cause for some major facepalmage. What do?

是否可以引用当前程序集并将其传递给CompilerParameters?还是我被搞砸了?我应该依靠为脚本对象等创建程序集吗?

Is there a way to reference the current assembly and pass it to CompilerParameters? Or am I royally screwed / should I rely on creating an assembly for script objects / etc?

推荐答案

它可能在错误的目录中查找.

It's probably looking in the wrong directory.

传递 typeof(Program).Assembly.CodeBase 以传递完整路径.

这篇关于使用CompilerParameters引用当前程序集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆