【问题标题】:A reference to " .dll" this could not be added无法添加对“.dll”的引用
【发布时间】:2014-08-15 09:42:32
【问题描述】:

在我的解决方案中的一个项目中,我有以下代码来生成一个 .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 的引用。阅读一些关于 [DLLImport("yourassembly.dll")] 的内容,也许它可能对您的情况有所帮助。

标签: c# .net visual-studio dll


【解决方案1】:

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

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

让visual studio自己添加合适的dll?

【讨论】:

  • .dll 的项目已经在同一个解决方案中。尽管如此,我还是尝试添加对 .dll 项目的引用,我现在可以访问实体模型(如上代码中所示的 FieldIdEntities),但不能访问程序集。
猜你喜欢
  • 2011-03-28
  • 1970-01-01
  • 1970-01-01
  • 2013-08-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-10
相关资源
最近更新 更多