3

In a project within my solution I have the following code to generate a .dll assembly. Developed mainly with the msdn documentation and the answer to another question here on stackoverflow. The code builds and runs fine and the ".dll" assembly in generated in the bin\debug folder within the project. However I have problems referencing the assembly in other projects within the solution. I right click references-> add reference and select the ".dll" file. When I click ok I get the error message stated in the title of this question. Thanks.

        // Retrieve application domain ie environment where applications execute
        // This is done because if an assembly is loaded into the default 
        // application domain it cannot be unloaded from memory while the process
        // is running. However if another application domain is opened to load
        // and execute the assembly, the assembly is unloaded when the application
        // domain is unloaded. 
        AppDomain currentDomain = AppDomain.CurrentDomain;

        // Now create a dynamic assembly in the current application domain
        // and allow it to be executed and saved to disk
        AssemblyName name = new AssemblyName("FieldIdEnum");
        AssemblyBuilder assemblyBuilder = currentDomain.DefineDynamicAssembly(name,
            AssemblyBuilderAccess.RunAndSave);

        // Now we define a dynamic module in the assembly that was just created
        // For this single single module assembly the module has the same name as 
        // the assembly
        ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule(name.Name,
            name.Name + ".dll");

        // Now we define the public enumeration with the name "DbFieldId" and an
        // underlying type of Integer
        EnumBuilder myEnumBuilder = moduleBuilder.DefineEnum("EnumeratedTypes.DbFieldId",
            TypeAttributes.Public, typeof(int));

        // Now retrieve the FieldId table from the database, bear in mind that the
        // string description will have white spaces, these need to be trimmed 
        // otherwise they are invalid for enum
        // For this to work a reference was added to ConsoleApplication4 and
        // Entity Framework
        using (FieldIdEntities dbEntities = new FieldIdEntities())
        {
            var lines = dbEntities.CSV_FIELD_DETAILS;
            foreach (var field in lines)
            {
                myEnumBuilder.DefineLiteral(field.DESCRIPTION.Replace(" ", String.Empty),               (int)field.FIELD_ID);
            }
        }

        // Now create the enum
        myEnumBuilder.CreateType();

        // If good so far save the assembly:
        assemblyBuilder.Save(name.Name + "dll");
Community
  • 1
  • 1
Nic
  • 61
  • 8
  • Sounds like you're trying to add reference to an unmanaged dll. Read a bit about [DLLImport("yourassembly.dll")] maybe it might help in your situation. – Bayeni Aug 15 '14 at 10:14

1 Answers1

0

Did you try adding the project containing the DLL into the same solution and then in the reference manager select

Solution -> Projects -> [your dll project]

so that visual studio adds the appropriate dll itself?

SCBuergel
  • 1,613
  • 16
  • 26
  • The project for the .dll is already in the same solution. I tried adding a reference to the .dll project nonetheless, I can now access the Entities model (FieldIdEntities as shown above in the code) but not the assembly. – Nic Aug 15 '14 at 09:58