0

i need compile c# code at run time in asp.net mvc 5 when use this code throw exception "metadata MIS.dll not found." but when use code in console app my code run without error Generally, any namespace that is in .NET itself run without error, and will throw exception "metadata xyz not found" any namespace I manually generate or add in my mvc project.

 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");
    }
D_ramezani
  • 11
  • 2

1 Answers1

0

Clean and build again. If you still get this error, delete the bin folder, clean and build again.

WilsonPena
  • 1,451
  • 2
  • 18
  • 37