2017-11-01 12 views
0

実行時にC#コードをコンパイルする必要があります。asp.net mvc 5 でこのコードを使用すると "metadata MIS.dll not found"例外がスローされます。 コンソールのコードを使用すると、エラーなしでコードが実行されます 一般的に、.NET内のすべてのネームスペースはエラーなしで実行され、手動で生成または追加する名前空間は「メタデータxyzが見つかりません」という例外が発生します。asp.net 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"); 
    } 

答えて

0

もう一度清掃してください。このエラーが引き続き発生する場合は、binフォルダーを削除して、きれいにしてから再度ビルドしてください。