0

我需要在运行时在 asp.net mvc 5 中编译 c# 代码,当使用此代码时会抛出异常“找不到元数据 MIS.dll”。但是当在控制台应用程序中使用代码时,我的代码运行没有错误通常,.NET 中的任何命名空间本身都运行没有错误,并且会抛出异常“元数据 xyz 未找到”我手动生成或添加到我的 mvc 项目中的任何命名空间。

 private static MethodInfo CreateRule(string rules)
    {


        string code = @"
            using System;
            using System.Collections.Generic;
            using System.Linq;
            using System.Linq.Expressions;
            using MIS.Helper;

            namespace UserFunctions
            { 

                public class BinaryFunction
                {                
                    public static Rule Function()
                    {
                         var t=new Rule();   

                        return func_xy;
                    }
                }
            }
        ";

        string finalCode = code.Replace("func_xy", rules);

        CSharpCodeProvider provider = new CSharpCodeProvider();

        CompilerParameters parameters =
            new CompilerParameters(new[] { "mscorlib.dll", "System.Core.dll","MIS.dll" })
            {
                GenerateInMemory = true,
                GenerateExecutable = false
            };


        CompilerResults results = provider.CompileAssemblyFromSource(parameters, finalCode);
        if (results.Errors.HasErrors)
        {
            var sb = new StringBuilder();

            foreach (CompilerError error in results.Errors)
            {
                sb.AppendLine(String.Format("Error ({0}): {1}", error.ErrorNumber, error.ErrorText));
            }

            throw new InvalidOperationException(sb.ToString());
        }
        var binaryFunction = results.CompiledAssembly.GetType("UserFunctions.BinaryFunction");
        return binaryFunction.GetMethod("Function");
    }
4

1 回答 1

0

清理并重新构建。如果您仍然收到此错误,请删除 bin 文件夹,清理并重新构建。

于 2017-11-01T13:54:25.120 回答