0

i created a dll contains a class named PersonVM like what you see below. and its working ...

  public ActionResult Index()

    {


        using (CSharpCodeProvider codeProvider = new CSharpCodeProvider())
        {
            System.CodeDom.Compiler.CompilerParameters parameters = new CompilerParameters();
            parameters.GenerateExecutable = false;
            parameters.OutputAssembly = "Per.dll";
            CompilerResults results = codeProvider.CompileAssemblyFromSource(parameters, "public class PersonVM{ " + "public int id{get;set;}" +
                "public string Name{get;set;}" + "public string LName{get;set;}" + " }");
        }

        Assembly assembly = Assembly.LoadFrom("Per.dll");
        var type = assembly.GetType("PersonVM");
        var d = type.GetProperties();
        object obj = Activator.CreateInstance(type, true);
        return View(obj);


    }

but this code is working just one time in my index controller. for example its not changing my dll class in here:

     public ActionResult Conf()

    {
        using (CSharpCodeProvider codeProvider = new CSharpCodeProvider())
        {
            System.CodeDom.Compiler.CompilerParameters parameters = new CompilerParameters();
            parameters.GenerateExecutable = false;
            parameters.OutputAssembly = "Per.dll";

            CompilerResults results = codeProvider.CompileAssemblyFromSource(parameters, "public class PersonVM{ " + "public int id{get;set;}" +
              "public string Name{get;set;}" + "public string LName{get;set;}" + "public string LNamee2 { get; set; }" + "public string L4 { get; set; }" + " }");
        }


        Assembly assembly = Assembly.LoadFrom("Per.dll");
        var type = assembly.GetType("PersonVM");


        object obj = Activator.CreateInstance(type, true);



        List<ClassInfoVM> model = obj.GetType().GetProperties()
            .Select(T => new ClassInfoVM()
            {
                PropName = T.Name,

                TypeOfProp = T.PropertyType.Name

            }).ToList();


        return View(model);
    }

there is no thing about any error.. it just doesn't changing my dll class...the dll class PersonVM is just contains the properties which i was set it, first time in Index

Iman Salehi
  • 908
  • 2
  • 14
  • 34
  • Out of curosity, why are you building these classes at runtime? – Scott Chamberlain Oct 26 '16 at 15:29
  • thanks for your comment. i actually want to use this class as my view model...it means i want to let user to provide the view models and maybe models, dynamically by himself. in mvc use EntityFramework – Iman Salehi Oct 29 '16 at 05:30

1 Answers1

1

You can't load the same named DLL in a app domain twice using Assembly.LoadFrom.

See the Remarks section of the Assembly.LoadFrom function on the MSDN:

The LoadFrom method has the following disadvantages. Consider using Load instead.

  • If an assembly with the same identity is already loaded, LoadFrom returns the loaded assembly even if a different path was specified.

One possible solution is let CSharpCodeProvider generate a random name for the assembly and load that, however if I where you I would seriously consider if you really need those classes to be built at runtime. Just build them at design time and give them two different names. Perhaps even make the version conf Conf dervive from the version in Index

Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431
  • i tried Assembly.Load but i'v got: "Could not load file or assembly 'Per, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified." – Iman Salehi Oct 29 '16 at 10:24