我正在编写一个小应用程序,它使用函数从源(.cs)代码文件编译文件:
public static bool CompileExecutable(String sourceName)
{
//Source file that you are compliling
FileInfo sourceFile = new FileInfo(sourceName);
//Create a C# code provider
CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp");
//Create a bool variable for to to use after the complie proccess to see if there are any erros
bool compileOk = false;
//Make a name for the exe
String exeName = String.Format(@"{0}\{1}.exe",
System.Environment.CurrentDirectory, sourceFile.Name.Replace(".", "_"));
//Creates a variable, cp, to set the complier parameters
CompilerParameters cp = new CompilerParameters();
//You can generate …Run Code Online (Sandbox Code Playgroud) 我想使用 Microsoft.CSharp.CSharpCodeProvider 类来编译 C# 7.3 代码。编译器版本在创建新 CSharpCodeProvider 时作为输入的 IDictionary 中指定;例如,{“CompilerVersion”、“v4.0”}。“v4.0”是不够的,因为它不能将 v7.3 识别为编译器选项。
在我的程序中,我使用CSharpCodeProvider从源文件编译另一个应用程序,我使用的代码如下:
public static bool CompileExecutable(String sourceName)
{
FileInfo sourceFile = new FileInfo(sourceName);
CodeDomProvider provider = null;
bool compileOk = false;
// Select the code provider based on the input file extension.
if (sourceFile.Extension.ToUpper(CultureInfo.InvariantCulture) == ".CS")
provider = CodeDomProvider.CreateProvider("CSharp");
else if (sourceFile.Extension.ToUpper(CultureInfo.InvariantCulture) == ".VB")
provider = CodeDomProvider.CreateProvider("VisualBasic");
else
{
Console.WriteLine("Source file must have a .cs or .vb extension");
}
if (provider != null)
{
// Format the executable file name.
// Build the output assembly path using the current directory
// …Run Code Online (Sandbox Code Playgroud) 让我们说我们有一个名为Program1.exe的应用程序,所以当我点击该程序必须创建另一个exe的exe时,可以说Program2.exe在屏幕上写"hello world".所以我认为可以在代码中使用csc命令使用visual studio的命令行工具,无论如何,如果可能的话,Program2.exe会替换Program1.exe吗?我的意思是Program1.exe可以在运行时重新编译?
当我将CSharpCodeProvider类设置为使用.NET 3.5时,它会尝试加载某些引用的DLL时出错:
Line number 0, Error Number: CS0006, 'Metadata file 'System.Linq.dll' could not be found;
Line number 0, Error Number: CS0006, 'Metadata file 'System.Collections.dll' could not be found;
Run Code Online (Sandbox Code Playgroud)
问题是,如果我将它设置为2.0它可以工作,但它不能使用LINQ.
现在在其他线程中有解决方案说这是因为DLL不在正确的位置,并提供如下解决方案:
typeof(System.Xml.Linq.Extensions).Assembly.Location
Run Code Online (Sandbox Code Playgroud)
要获取Linq位置的位置,但由于我的程序工作方式,我不知道我可能需要加载哪些DLL.这对于外部库来说不是问题,因为用户需要输入它们的位置,但对于System dlls,我能够使用"System.Linq"或者获取它们的位置"System.Collections"吗?
我正在使用CSharpCodeProvider来编译一个带有可变参数的.exe.编译工作正常(不返回错误)并成功,但在运行时启动并立即退出无错误或输出.当更改"Main"(例如更改为private或通过重命名)时,编译器输出没有有效的Main方法,因此示例代码不应该是原因.
有没有人对此有答案/解决方案?我很遗憾在这一点,并希望任何有用的回应.提前谢谢〜
*编辑:
编译.exe输出:http://imgur.com/a/WBvz3
编译:
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Resources;
using System.Security.Cryptography;
using System.Text;
using Microsoft.CSharp;
using Packer.Properties;
namespace Packer
{
internal class Program
{
private static void Main(string[] args)
{
Console.WriteLine("Sample Compiler");
Console.WriteLine(".ico-path: ");
var icon = "E:\\sample.ico"; //Console.ReadLine();
Console.WriteLine("> " + icon);
Console.WriteLine("Target-exe: ");
var target = "E:\\sample.exe"; //Console.ReadLine();
Console.WriteLine("> " + target);
var source = Resources.samplesource;
// Compile with all params
var success = CompileFromSource(source, target, icon); …Run Code Online (Sandbox Code Playgroud) 我正在构建一个伪代码转换器和编译器.它通过执行几个字符串操作将伪代码转换为代码,然后它使用CSharpCodeProvider类来编译它,最后它尝试运行它.
经过几次测试后,如果翻译/输出代码如下:
using System;
using System.Collections.Generic;
public class TranslatedProgram
{
public static void ReadLine(ref int destiny)
{
destiny = int.Parse(Console.ReadLine());
}
public static void InsertInStack(ref Stack<int> originalStack, int value)
{
Console.WriteLine("Inserting the value " + value + " to the stack.");
originalStack.Push(value);
foreach (int current in originalStack)
{
Console.WriteLine("|" + current);
}
Console.WriteLine("_______");
}
public static void Main()
{
int value = new int();
Stack<int> data = new Stack<int>();
ReadLine(ref value);
InsertInStack(ref data, value);
}
}
Run Code Online (Sandbox Code Playgroud)
当应用程序将此代码发送到CSharpCodeProvider时,它不会编译.在compilerResults中,我收到以下错误:"无法找到类型或命名空间名称'Stack'(您是否缺少using指令或程序集引用?)" (CS0246) …