.Net 脚本引擎 (C#) - 是否有某种“头文件”? 或“dll引用文件” 对于.Net 语言?

发布于 2024-07-14 05:20:46 字数 1374 浏览 4 评论 0原文

我正在用 C# 制作一个小型脚本引擎,我想知道是否有某种 #compiler 指令或某种其他类似头文件的文件,我可以使用我的脚本文件进行编译,以定义我的脚本文件引用的托管 Dll?

public static CompilerResults LoadScript(
    string SourceCodeFile, string[] References)
{
    var Parameters = new CompilerParameters()
    {
        GenerateExecutable = false,
        GenerateInMemory = true,
        IncludeDebugInformation = false,
    };

    Parameters.ReferencedAssemblies.AddRange(References);

    return CSharpCodeProvider.CreateProvider(
        CSharpCodeProvider.GetLanguageFromExtension(
            Path.GetExtension(SourceCodeFile)))
                .CompileAssemblyFromFile(Parameters, SourceCodeFile);
}

我希望能够消除这行代码:

Parameters.ReferencedAssemblies.AddRange(References);
and put that information in my scripts instead. The problem is I do not necessarily know ahead of time what .dlls the scripts are going to reference. My current solution right now is to parse a custom "script.cs.refs" file that looks something like this:
"System.dll","System.Data.dll","SomeOther.dll","Ect.dll"
Any help on finding out where visual studio stores your references to dlls and if it has to be defined in a separate file, if there is a standardized format I should be using that I can load into the compiler that tells it this information, would be greatly appreciated!

I'm making a small scripting engine in C# and I was wondering, is there some sort of #compiler directives or some sort of other header-like file I can compile with my script files to define what Managed Dlls my script files are referencing?

public static CompilerResults LoadScript(
    string SourceCodeFile, string[] References)
{
    var Parameters = new CompilerParameters()
    {
        GenerateExecutable = false,
        GenerateInMemory = true,
        IncludeDebugInformation = false,
    };

    Parameters.ReferencedAssemblies.AddRange(References);

    return CSharpCodeProvider.CreateProvider(
        CSharpCodeProvider.GetLanguageFromExtension(
            Path.GetExtension(SourceCodeFile)))
                .CompileAssemblyFromFile(Parameters, SourceCodeFile);
}

I would love to be able to eliminate this line of code:

Parameters.ReferencedAssemblies.AddRange(References);

and put that information in my scripts instead. The problem is I do not necessarily know ahead of time what .dlls the scripts are going to reference. My current solution right now is to parse a custom "script.cs.refs" file that looks something like this:

"System.dll","System.Data.dll","SomeOther.dll","Ect.dll"

Any help on finding out where visual studio stores your references to dlls and if it has to be defined in a separate file, if there is a standardized format I should be using that I can load into the compiler that tells it this information, would be greatly appreciated!

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

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

发布评论

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

评论(2

悲念泪 2024-07-21 05:20:46

您不能在 C# 源代码中指定程序集引用,不是。

Visual Studio 在生成项目时会将项目中的引用传递给编译器。

请注意,使用命令行时,存在响应文件 包含一堆选项。 这可以在命令行上指定,或者 .NET Framework 目录中有一个默认的 csc.resp 文件,该文件具有包括引用在内的默认选项,但我怀疑它是从 CSharpCodeProvider 使用的代码>.

您可能有自己的“特殊注释”格式,其中列出了引用 - 解析为您提供的脚本以获取这些引用,然后将它们传入。

You can't specify assembly references within C# source code, no.

Visual Studio will pass the references in your project to the compiler when it builds the project.

Note that when using the command line, there's the notion of a response file containing a bunch of options. This can be specified on the command line, or there's a default csc.resp file in the .NET framework directory which has default options including references, but I doubt that that's used from CSharpCodeProvider.

You could potentially have your own "special comment" format which lists the references - parse the script that you've been given to grab those references, and then pass those in.

稚然 2024-07-21 05:20:46

我认为对此没有标准的解决方案,除非您可以采用类似 ASP.NET 的方法来引用源文件中定义的程序集。

然后,您需要

  1. 解析源代码文件以查找程序集引用,
  2. 将它们传递给 CompilerParameters
  3. 确保源代码中定义引用的行不会生成编译错误。

要实现 3.,您可以在注释中定义引用。 例如,

//@ Assembly name="SomeOther.dll"

您可以对这些 //@ 行的源代码文件进行正则表达式并解析出程序集名称。

I don't think there is a standard solution to this, except that you could take the ASP.NET like approach of having the references to the assemblies defined in the source file.

You would then need to

  1. Parse the source code file to find the assembly references
  2. Pass them to the CompilerParameters
  3. Make sure the lines defining the references in the source code do not generate compile errors.

To achieve 3., you could have the references defined in comments. e.g.

//@ Assembly name="SomeOther.dll"

You could then regex the sourcode file for these //@ lines and parse out the assembly name.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文