C# .NET 中的延迟程序集依赖

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9173/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-07-31 17:24:27  来源:igfitidea点击:

Lingering assembly dependency in C# .NET

提问by Dogmang

My C# project - we'll call it the SuperUI - used to make use of a class from an external assembly. Now it doesn't, but the compiler won't let me build the project without the assembly reference in place. Let me elaborate.

我的 C# 项目 - 我们将其称为 SuperUI - 用于使用来自外部程序集的类。现在没有,但编译器不会让我在没有程序集引用的情况下构建项目。让我详细说明一下。

This project used to throw and catch a custom exception class - the SuperException- which was derived from the standard System.Exception and lived in a separate, precompiled assembly, SuperAssembly.DLL, which I referenced.

这个项目曾经抛出和捕获一个自定义异常类 - the SuperException- 它派生自标准 System.Exception 并存在于一个单独的预编译程序集中SuperAssembly.DLL,我引用了它。

Eventually, I decided this was a pointless exercise and replaced all SuperExceptionswith a System.SuitableStandardException in each case. I removed the reference to SuperException.DLL, but am now met with the following on trying to compile the project:

最终,我认为这是一个毫无意义的练习,并SuperExceptions在每种情况下用 System.SuitableStandardException替换了所有内容。我删除了对 的引用SuperException.DLL,但现在在尝试编译项目时遇到以下情况:

The type 'SuperException' is defined in an assembly that is not referenced. You must add a reference to assembly 'SuperException, Version=1.1.0.0 (...)'

类型“SuperException”是在未引用的程序集中定义的。您必须添加对程序集“SuperException, Version=1.1.0.0 (...)”的引用

The source file referenced by the error doesn't seem relevant; it's the project namespace that gets highlighted in the IDE.

错误引用的源文件似乎不相关;它是在 IDE 中突出显示的项目命名空间。

Now, here's the thing:

现在,事情是这样的:

  1. All uses of SuperExceptionhave been eliminated from the project's code.
  2. Compared to another project that compiles fine without a reference to SuperException.DLL, I only reference one more assembly - and thatreferences nothing that my project doesn't reference itself. While it's possible that any of these dependencies could throw SuperExceptions, I'm only catching the base Exception class and in any case... the other project builds fine!
  3. I've done Visual Studio's "Clean Solution" and cleared everything out by hand, many times.
  1. SuperException已从项目代码中删除了所有使用。
  2. 与另一个在没有引用的情况下编译良好的项目相比SuperException.DLL,我只引用了一个程序集 - 并且that没有引用我的项目没有引用的任何内容。虽然这些依赖项中的任何一个都可能抛出SuperExceptions异常,但我只捕获了基本的 Exception 类,无论如何……另一个项目构建得很好!
  3. 我已经完成了 Visual Studio 的“清洁解决方案”,并多次手动清除了所有内容。

It's not the end of the world to include this reference, I just don't see why it's necessary any more. Nrrrgg. Any pointers welcome!

包含这个参考并不是世界末日,我只是不明白为什么有必要了。Nrrrgg。欢迎任何指点!

采纳答案by Henrik

It's likely a transitive reference, where some type method call returns an instance of SuperException boxed ("downcast") as e.g. Exception, but from inspecting the code in the transitively included code, i.e. code from your external method calls, the compiler knows that you need to be able to have information about that type at some point.

它可能是一个传递引用,其中某些类型的方法调用将 SuperException 装箱(“向下转换”)的实例返回为例如 Exception,但是通过检查传递包含的代码中的代码,即来自外部方法调用的代码,编译器知道您需要能够在某个时候获得有关该类型的信息。

Resharper would tell you where it's the case that you need to add a reference, and you could use Lütz Roeder's aka RedGate's Reflector to scan compiled IL for a reference to this type in two ways: 1) use the search-facility, 2) open each public type you're using and for that one which requires the "ghost" assembly, it will ask you to specify its location.

Resharper 会告诉您需要添加引用的情况,您可以使用 Lütz Roeder 的 aka RedGate 的 Reflector 以两种方式扫描已编译的 IL 以获取对此类型的引用:1) 使用搜索工具,2) 打开您正在使用的每种公共类型以及需要“ghost”程序集的公共类型,它都会要求您指定其位置。

This most often happends to me when I reference Castle.Windsor but not Castle.MicroKernel. :p

当我引用 Castle.Windsor 而不是 Castle.MicroKernel 时,这种情况最常发生在我身上。:p

回答by Iain Holder

This sounds pretty strange. Here's what I would check next:

这听起来很奇怪。这是我接下来要检查的内容:

  1. Check that there's nothing lingering in your Properties/AssemblyInfo.cs file.
  2. Check that there's nothing lingering in your SuperUI.csproj file.
  3. Delete all references and re-add them.
  1. 检查您的 Properties/AssemblyInfo.cs 文件中是否没有任何内容。
  2. 检查您的 SuperUI.csproj 文件中是否没有任何内容。
  3. 删除所有引用并重新添加它们。

回答by Joseph Daigle

Since it's a compiler error, there must be a reference or use of SuperException somewhere in the project.

既然是编译器错误,项目中一定有引用或使用 SuperException 的地方。

  1. Do a find/replace in the entire project or solution for that type and remove every reference (it's possible you already did this).
  2. If you reference any types that inherits from SuperException (even if the type defined in another assembly), you need a reference to the assembly that SuperException is defined in.
  1. 在整个项目或该类型的解决方案中执行查找/替换并删除每个引用(您可能已经这样做了)。
  2. 如果引用从 SuperException 继承的任何类型(即使是在另一个程序集中定义的类型),则需要对在其中定义 SuperException 的程序集的引用。

Take the line that the compiler is showing the error on and start tracing the inheritance tree of the objects used on that line, you might find the source of it that way.

选择编译器显示错误的那一行,并开始跟踪该行上使用的对象的继承树,您可能会以这种方式找到它的来源。

回答by Lance Fisher

Try creating a new project, and adding all your classes to it.

尝试创建一个新项目,并将所有类添加到其中。

回答by Michael Stum

  1. Exit Visual Studio
  2. Delete the bin and obj Folders in your solution directory
  3. Restart and see what happens
  1. 退出 Visual Studio
  2. 删除解决方案目录中的 bin 和 obj 文件夹
  3. 重启看看会发生什么

回答by Stu

grep your project folder. It could be a hidden reference in your project, or a project that your project references. Cleanse with Notepad if needed.

grep 您的项目文件夹。它可能是您项目中的隐藏引用,也可能是您的项目引用的项目。如果需要,用记事本清洁。

回答by Dogmang

Thanks for your answers so far. I've tried every suggestion (except one) to no avail.

到目前为止,感谢您的回答。我已经尝试了所有建议(除了一个)都无济于事。

The suggestion I haven't tried is to create a new project and add all my stuff to it, the thought of which really tests my will to live. ;) I may try this tomorrow if I can be bothered. Thanks again.

我没有尝试过的建议是创建一个新项目并将我所有的东西添加到其中,这想法确实考验了我的生活意愿。;) 如果我不介意的话,我明天可以试试这个。再次感谢。

回答by Will Dean

There is really nothing very mysterious about VS projects nowadays - it's all text files, etc. SOMETHING must reference that class/dll, and that something must be part of your project.

现在的 VS 项目真的没有什么神秘之处——它都是文本文件等。某些东西必须引用该类/dll,并且某些东西必须是您项目的一部分。

Have you really grep'd or findstr'd the whole solution tree, every single file, for a reference to that exception?

您是否真的对整个解决方案树(每个文件)进行了grep 或 findstr 处理,以供参考该异常?

回答by Rob Cooper

I agree with the other comments here.. There is a reference, in plain text somewhere! I have had similar problems in the past where searching through the project files returned nothing, turns out it was in some other file that wasn't automatically picked up in the search.

我同意这里的其他评论..某处有一个纯文本参考!过去我遇到过类似的问题,搜索项目文件没有返回任何内容,结果是在搜索中没有自动选择的其他文件中。

I don't think that creating a new project is the solution here.. You need to be positive that NONEof the references in your dependency tree use SuperException.. NONE

我不认为创建一个新的项目是这里的解决方案..你需要积极的,NONE在你的依赖树使用SuperException引用的.. NONE

I have never experienced this to the point where I have needed to literally wipe the project, I have always found the reference somewhere. Ensure you are searching everyfile.

我从来没有经历过这种情况,我需要从字面上擦除项目,我总是在某处找到参考。确保您正在搜索每个文件。

EDIT:

编辑:

Just a point to add, if the location pointed to by the error seems random, that can often mean there is a mismatch between the compiled source and the source code file.. Is this a ASP.NET application? I have had it before where the compiled DLL's haven't been replaced on a rebuild in the ASP.NET temp folder causing things to get.. Interesting when debugging :)

补充一点,如果错误指向的位置看起来是随机的,那通常意味着编译的源代码和源代码文件之间存在不匹配。这是一个 ASP.NET 应用程序吗?我之前在 ASP.NET 临时文件夹中的重建中没有替换已编译的 DLL 导致事情变得......在调试时很有趣:)

回答by Orion Edwards

If you reference any types that inherits from SuperException (even if the type defined in another assembly), you need a reference to the assembly that SuperException is defined in.

如果引用从 SuperException 继承的任何类型(即使是在另一个程序集中定义的类型),则需要对在其中定义 SuperException 的程序集的引用。

Seconded on that.

对此表示赞同。

You might not be referencing SuperException, but you might be referencing SpecializedSuperException, which is derived from, or somehow otherwise uses SuperException- your grep of the project for SuperExceptionwon't be catching it though.

您可能没有引用SuperException,但您可能引用了SpecializedSuperException,它派生自或以其他方式使用SuperException- 您的项目的 grep forSuperException不会捕获它。

Try have a hack with the trial of NDepend

尝试破解NDepend

回答by Bernard

grep -R SuperException *in the base of your project (get grepfrom somewhere first) just to be sure.

grep -R SuperException *在您的项目的基础上(grep首先从某个地方获取)只是为了确定。