Dynamic assembly missing RRS feed

  • Question

  • Hello!

    I'm using CSharpCodeProvider to create a class with several fields dynamically. That's the code:

    //определение пространства имен
    CodeCompileUnit compileUnit = new CodeCompileUnit();CodeNamespace dynNS = new CodeNamespace("Dyn");compileUnit.Namespaces.Add( dynNS );
    //определение класса
    CodeTypeDeclaration dynClass = new CodeTypeDeclaration("DynClass");
    int i = 0;foreach(string gr in Groupings){	dynClass.Members.Add(new CodeMemberField((Type)GroupTypes[i], (string)Groupings[i]));
    //компиляция полученной сборки
    CSharpCodeProvider provider = new CSharpCodeProvider();CompilerParameters parameters = new CompilerParameters();parameters.GenerateInMemory = true;CompilerResults cr = provider.CompileAssemblyFromDom(parameters, compileUnit);
    //получение типа из сборки
    Assembly dynAssembly = cr.CompiledAssembly;
    Type dClass = dynAssembly.GetType("DynClass");
    But I receive an error message like "Unable to load file or assembly". What's wrong?

    • Moved by Fred BaoModerator Wednesday, October 29, 2014 3:03 AM Being moved from LINQ2SQL forum
    Tuesday, October 28, 2014 10:01 AM


  • Hello Summit2,

    >>But I receive an error message like "Unable to load file or assembly". What's wrong?

    Being confused why you would get the error, because with your provided code, I made a test, however, after changing parameters.GenerateInMemory to false, it generated the dll successfully which contains the class as:

    using System;
    namespace Dyn
                    public class DynClass
                                    private int ID;
                                    private string Name;

    And after changing “dynAssembly.GetType("DynClass")” to “dynAssembly.GetType("Dyn.DynClass")”(this is because we need to provide the whole type name which includes the namespace), the variable “dClass “ shows the correct information about the dynamic generated dll.

    Here is the whole test code below which is a bit different from yours, I assign the filed type and its name explicitly since I do not know how you write the Groupings and GroupTypes:

    CodeCompileUnit compileUnit = new CodeCompileUnit();
                CodeNamespace dynNS = new CodeNamespace("Dyn");
                CodeTypeDeclaration dynClass = new CodeTypeDeclaration("DynClass");
                int i = 0;
                //foreach (string gr in Groupings)
                //    dynClass.Members.Add(new CodeMemberField((Type)GroupTypes[i], (string)Groupings[i]));
                dynClass.Members.Add(new CodeMemberField(typeof(int), "ID"));
                dynClass.Members.Add(new CodeMemberField(typeof(string), "Name"));
                CSharpCodeProvider provider = new CSharpCodeProvider();
                CompilerParameters parameters = new CompilerParameters();
                parameters.GenerateInMemory = true; 
                CompilerResults cr = provider.CompileAssemblyFromDom(parameters, compileUnit);
                Assembly dynAssembly = cr.CompiledAssembly;
                Type dClass = dynAssembly.GetType("Dyn.DynClass");

    The environment is .NET Framework 4.5 and VS 2013. Please try with the example above.

    If it still throws the error message, please tell us which line causes it and this would help narrow down this issue.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, October 29, 2014 3:31 AM