我读到你无法使用CSharpCodeProvider编译C#6.0,因此尝试使用Roslyn.但我找不到一个很好的例子,如何加载文件,然后将其编译为DLL.
我应该如何使用Roslyn编写与此代码类似的内容?或者还有其他方法吗?现在,当我尝试编译包含对带有C#6.0代码的项目的引用的文件时,它只是说"命名空间'y'中不存在类型或命名空间名称'x'(您是否缺少程序集引用?)"
public string CompileCode()
{
var provider = new CSharpCodeProvider();
var outputPath = Path.Combine(Path.GetDirectoryName(_path), $"Code.dll");
var compilerparams = new CompilerParameters(_referencedAssemblies, outputPath);
CompilerResults results = provider.CompileAssemblyFromFile(compilerparams, _path);
var dllPath = results.PathToAssembly;
if (!results.Errors.HasErrors)
return dllPath;
PrintError(results.Errors);
return "";
}
Run Code Online (Sandbox Code Playgroud)
总之,我想:
我正在从代码生成/构建自定义.Net托管资源文件(.resx文件)
在C#或VB.Net中,我如何设计一个能够将我的自定义.resx文件合并,加入或嵌入到.net程序集中的方法(或多或少与VS编译器默认使用的方式相同)?
所以,一方面我有一个Application.exe(已编译),另一方面我有一个resources.resx,我的意图是将resx"合并"到已编译的程序集中.
任何形式的信息都会感激不尽,因为我没有发现任何相关信息.
PS:唯一的必要条件是不使用ILMerge等第三方工具.
我有一些类(简单类),我想在运行时编译这个类,并创建(在运行时)一些将包含此类的DLL.
有办法吗?
谢谢.
以下是我的代码:
using System;
using System.Collections.Generic;
using System.Text;
using System.CodeDom.Compiler;
using System.IO;
using Microsoft.CSharp;
using System.Reflection;
namespace DynaCode
{
class Program
{
static void Main(string[] args)
{
string content = File.ReadAllText(@"D:\hi.cs");
string[] code = new string[content.Length];
char[] seperators = { '\n','\r','\t' };
code = content.Split(seperators);
CompileAndRun(code);
Console.ReadKey();
}
static void CompileAndRun(string[] code)
{
CompilerParameters CompilerParams = new CompilerParameters();
string outputDirectory = Directory.GetCurrentDirectory();
CompilerParams.GenerateInMemory = true;
CompilerParams.TreatWarningsAsErrors = false;
CompilerParams.GenerateExecutable = false;
CompilerParams.CompilerOptions = "/optimize";
string[] references = { "System.dll"};
CompilerParams.ReferencedAssemblies.AddRange(references);
CSharpCodeProvider …Run Code Online (Sandbox Code Playgroud)