在我的解决方案中的一个项目中,我具有以下代码来生成.dll程序集。主要是用msdn documentation和此处关于stackoverflow的另一个问题的答案开发的。该代码可以正常构建和运行,并且在项目内的bin \ debug文件夹中生成了“ .dll”程序集。但是,我在解决方案中的其他项目中引用程序集时遇到问题。我右键单击引用->添加引用,然后选择“ .dll”文件。当我单击确定时,我得到此问题标题中所述的错误消息。谢谢。

        // 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");

最佳答案

您是否尝试过将包含DLL的项目添加到同一解决方案中,然后在引用管理器中选择

解决方案->项目-> [您的dll项目]

以便Visual Studio本身添加适当的dll?

关于c# - 不能添加对“.dll”的引用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25324338/

10-17 00:58