简体   繁体   English

C# csc 从 memory 编译源代码

[英]C# csc compile source code from memory

I would like to compile c# source code from a string.我想从一个字符串编译 c#源代码 I know this is possible using CodeDom, and I would like to know if it's possible to do this using the command line compiler.我知道使用 CodeDom 可以做到这一点,我想知道是否可以使用命令行编译器来做到这一点。 For example, say I load the following code onto a string:例如,假设我将以下代码加载到字符串中:

static void Main(string[] args)
        {           

        }

Is there some way to compile this code into a c# executable (.exe) file without first writing this code to my hard drive in the form of a C# class (.cs) file?有没有办法将此代码编译成 c# 可执行 (.exe) 文件,而无需先以 C# class (.cs) 文件的形式将此代码写入我的硬盘驱动器?

That was the first part of my question.这是我问题的第一部分。 The second part assumes that the above is in fact impossible.第二部分假设上述实际上是不可能的。 Let's say I had three difference classes (loaded onto three separate strings).假设我有三个不同的类(加载到三个单独的字符串上)。 How would I go about using the Command line compiler to compile each of these three classes into one common executable file?我将如何使用命令行编译器将这三个类中的每一个编译成一个通用的可执行文件? I have found some examples online, but they seem to be very vague, unfortunately.我在网上找到了一些例子,但不幸的是,它们似乎很模糊。

Any help, or pointers in the right direction is much appreciated.非常感谢任何帮助或正确方向的指示。

Thank you, Evan谢谢你,埃文

No, you can't use csc.exe without a source file.不,您不能在没有源文件的情况下使用 csc.exe。 What is your rationale for needing to use csc.exe?您需要使用 csc.exe 的理由是什么? Have you found something that the CodeDOM doesn't handle correctly?您是否发现 CodeDOM 无法正确处理的内容? A command-line option you don't have a CodeDOM equivalent for?您没有 CodeDOM 等效项的命令行选项?

The C# team mentioned that a future version might support compiler-as-a-service , where csc.exe would just become a thin wrapper around a compiler DLL, that you could also call directly. C# 团队提到,未来的版本可能支持编译器即服务,其中 csc.exe 将成为编译器 DLL 的精简包装,您也可以直接调用它。 But you'd still need to bypass csc.exe.但是您仍然需要绕过 csc.exe。

You can use System.CodeDom.Compiler.CompilerParameters to get CodeDOM to give you the same results as csc.exe.您可以使用System.CodeDom.Compiler.CompilerParameters来获取 CodeDOM,从而为您提供与 csc.exe 相同的结果。 Check the build log to find out what options Visual Studio is passing to csc.exe.检查构建日志以了解 Visual Studio 传递给 csc.exe 的选项。 Specifically, the architecture ( AnyCPU vs x86 ) is likely to make a big difference, since it determines whether the resulting assembly will be compatible with 32-bit DLLs when run on Windows x64.具体来说,架构( AnyCPUx86 )可能会产生很大的不同,因为它决定了在 Windows x64 上运行时生成的程序集是否与 32 位 DLL 兼容。 Or you can use corflags to fix the architecture after compilation.或者您可以在编译后使用corflags来修复架构。

I was looking to see if you could direct CSC to compile from STDIN but I can't seem to see that, so I think Part one isn't likely to be the case.我想看看你是否可以指示 CSC 从 STDIN 编译,但我似乎看不到这一点,所以我认为第一部分不太可能是这种情况。

However, part two is easier.但是,第二部分更容易。

a command such as一个命令,例如

csc /out:test.exe *.cs

Compiles all.CS files in the current directory into test.exe.将当前目录下的所有.CS文件编译成test.exe。 You can find other examples here:您可以在此处找到其他示例:

http://msdn.microsoft.com/en-us/library/78f4aasd.aspx http://msdn.microsoft.com/en-us/library/78f4aasd.aspx

Edit: I just thought of a horrible hack that might help.编辑:我只是想到了一个可能有帮助的可怕的黑客攻击。 This is horrible however.然而这很可怕。 What about creating your file as some kind of UNC/Service url;如何将您的文件创建为某种 UNC/Service url; My thinking is that you could associate a NetworkStream to a UNC or an endpoint of some HttpHandler or webservice.我的想法是,您可以将 NetworkStream 与 UNC 或某些 HttpHandler 或 web 服务的端点相关联。 once read from you would simply return the string to the stream.一旦从您那里读取,只需将字符串返回到流中。

This is really hacky though and I have no idea if it'll work!不过,这真的很hacky,我不知道它是否会起作用!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM