gpt4 book ai didi

c# - 动态编译代码中缺少程序集引用

转载 作者:太空狗 更新时间:2023-10-29 17:36:18 25 4
gpt4 key购买 nike

感谢有关我关于此问题的第一篇文章的消息。这次我会重新发布并尝试更清楚。我想这可能是一个微不足道的问题,但我真的被困住了,需要一些帮助。这是我第一次在这里发帖。

using System;
using System.CodeDom.Compiler;
using System.Reflection;
using Microsoft.CSharp;
namespace DynamicCode
{
public class DynaCore
{
string WorkingCode =
"using System;"+
"using System.Collections.Generic;"+
"namespace DynaCore"+
"{"+
" public class DynaCore"+
" {"+
" static public string DynamicResult()"+
" {"+
" return \"I'm compiled\";"+
" }"+
" }"+
"}";


string PredicateTemplCode =
"using System;"+
"using System.Linq;"+
"using System.Collections.Generic;"+
"namespace DynaCore"+
"{"+
" public class DynaCore"+
" {"+
" static public Func<{1}, bool> DynamicResult()"+
" {"+
" return new Func<{1}, bool>({2});"+
" }"+
" }"+
"}";

public DynaCore()
{
string compiledString = WorkingCompilation(WorkingCode);
Func<bool, bool> compiladePredicate = NotWorkingCompilation<bool>(PredicateTemplCode, "(o)=> true");
}

string WorkingCompilation(string code)
{
var cParams = new CompilerParameters();
cParams.GenerateInMemory = true;
cParams.TreatWarningsAsErrors = false;
cParams.GenerateExecutable = false;
cParams.CompilerOptions = "/optimize /target:library";

var curAss = Assembly.GetExecutingAssembly();

cParams.ReferencedAssemblies.Add("System.dll");
cParams.ReferencedAssemblies.Add("mscorlib.dll");
cParams.ReferencedAssemblies.Add("System.dll");
cParams.ReferencedAssemblies.Add("System.Data.dll");
cParams.ReferencedAssemblies.Add(curAss.Location);

var provider = new CSharpCodeProvider();

var compalerResult = provider.CompileAssemblyFromSource(cParams, code);

if (compalerResult.Errors.HasErrors)
{
var complieError = "";
foreach (CompilerError ce in compalerResult.Errors)
complieError += ce + " ";

throw new Exception(complieError.Trim());
}

Module module = compalerResult.CompiledAssembly.GetModules()[0];
Type mt = null;
MethodInfo methInfo = null;

return (string)module.GetType("DynaCore.DynaCore").GetMethod("DynamicResult").Invoke(null, null);
}


Func<T, bool> NotWorkingCompilation<T>(string code, string predicateString)
{
var cParams = new CompilerParameters();
cParams.GenerateInMemory = true;
cParams.TreatWarningsAsErrors = false;
cParams.GenerateExecutable = false;
cParams.CompilerOptions = "/optimize /target:library";

var curAss = Assembly.GetExecutingAssembly();

cParams.ReferencedAssemblies.Add("System.dll");
cParams.ReferencedAssemblies.Add("mscorlib.dll");
cParams.ReferencedAssemblies.Add("System.dll");
cParams.ReferencedAssemblies.Add("System.Data.dll");
cParams.ReferencedAssemblies.Add("System.Core.dll");
cParams.ReferencedAssemblies.Add(curAss.Location);

var provider = new CSharpCodeProvider();

var codeToRun = code.Replace("{1}", typeof(T).Name).Replace("{2}", predicateString);
var compalerResult = provider.CompileAssemblyFromSource(cParams, codeToRun);

if (compalerResult.Errors.HasErrors)
{
var complieError = "";
foreach (CompilerError ce in compalerResult.Errors)
complieError += ce + " ";

throw new Exception(complieError.Trim());
}

Module module = compalerResult.CompiledAssembly.GetModules()[0];
Type mt = null;
MethodInfo methInfo = null;

return (Func<T, bool>)module.GetType("DynaCore.DynaCore").GetMethod("DynamicResult").Invoke(null, null);
}
}
}

问题是,当我在 ReferencedAssemblies.Add("System.Core.dll") 中引用 System.Core.dll 时,它会给我一个编译器错误:

错误 CS0006:找不到元数据文件“System.Core.dll”

我使用的是 v3.5 和 VS 2008。

最佳答案

谢谢大家的回答!

事实证明,CSharpCodeProvider 默认为不支持泛型或 linq 的 2.0 版。以下解决了问题:

var provider = new CSharpCodeProvider(
new Dictionary<String, String>{{ "CompilerVersion","v3.5" }});

关于c# - 动态编译代码中缺少程序集引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4063285/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com