【问题标题】:How to use referenced c# dll code in a VS C# console application如何在 VS C# 控制台应用程序中使用引用的 c# dll 代码
【发布时间】:2018-07-14 10:51:54
【问题描述】:

所以我有两个 dll,Algorithms.dllData_Structures.dll(这些是我在 GitHub 上找到的项目中制作的)。使用浏览功能,我设法将两个 DLL 文件添加为对我的 Visual Studio 2017 控制台项目的引用。问题是我不能对他们做任何其他事情。每当我尝试在任一文件中引用某些内容时,都无法找到它。唯一可以识别的是名称空间,但其中没有任何内容。

我需要做什么才能让 VS 找到这些 DLL 包含的类以便我可以使用它们?我知道我需要使用 Algorithms.Sorting 作为示例,但我不能调用 anything,所以我以这个为例。

附:如果您需要更多信息,请询问。我不确定与这个问题有什么关系。

编辑:好的,有这样的例子是误导性的。已更正,但请阅读问题。

编辑:我在 Monodevelop 上试过这个并得到同样的问题。也许不是 IDE 的问题?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Algorithms.Sorting; // Error, Sorting cannot be found, and neither can the file container Sorting
using Data_Structures; //Perfectly ok, can find the namespace

namespace CS_HW2_Testing_App
{
    class Program
    {
        static void Main(string[] args)
        {
           // I'd like to call MergeSort and so forth here. What am I missing?!
        }
    }
}

如果有帮助,这是包含 MergeSort 的文件的顶部部分

using System;
using System.Collections.Generic;

using Algorithms.Common;

namespace Algorithms.Sorting
{
    public static class MergeSorter
    {
        //
        // Public merge-sort API
        public static List<T> MergeSort<T>(this List<T> collection, Comparer<T> comparer = null)
        {
            comparer = comparer ?? Comparer<T>.Default;

            return InternalMergeSort(collection, 0, collection.Count - 1, comparer);
        }
...

【问题讨论】:

  • 最好的猜测是您引用的是在添加MergeSorter 类之前编译的Algorithms.dll旧版本。尝试删除并添加您知道 使用该类编译的Algorithms.dll 的引用。此外,请确保 Algorithms 项目确实编译并且没有出现任何编译错误。
  • 我无法引用任何内容。我今晚才编译它,只有一个编译。有一些警告,但它们来自我不会使用的项目。感谢您的建议!
  • 查看项目的引用时,对dll的引用是否有效?没有警告标志?
  • 是的,与正常参考的唯一可检测差异是“强名称”为 False。

标签: c# .net visual-studio dll


【解决方案1】:

在第一个代码块中,您导入了错误的命名空间:using Algorithms.MergeSort 应该是 using Algorithms.Sorting。然后你可以在你的代码中使用MergeSorter.MergeSort&lt;T&gt;(...)

【讨论】:

    【解决方案2】:

    你需要引用命名空间而不是类。

    using Algorithms.Sorting; //instead of using Algorithms.MergeSort;
    

    还要确保课程是公开的

    【讨论】:

    • 尝试构建项目,即使您在 VS 中突出显示了错误。如果它编译你需要重新启动 VS 和/或清除 R# 缓存。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多