mercredi 26 octobre 2016

Generating DLL assembly at run time and change it?

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





Aucun commentaire:

Enregistrer un commentaire