简体   繁体   English

`GetReferencedAssemblies` 是 `AppDomain.CurrentDomain.GetAssemblies()` 的子集吗?

[英]Is `GetReferencedAssemblies` a subset of `AppDomain.CurrentDomain.GetAssemblies()`?

I want to test some reflection code I've written.我想测试一些我写的反射代码。

There's an edge case that would occur if the ReferencedAssemblies were ever not already loaded into the AppDomain, ie if the following expression ever returned true :如果 ReferencedAssemblies 尚未加载到 AppDomain 中,则会发生边缘情况,即如果以下表达式曾经返回true

typeof(MyType).Assembly.GetReferencedAssemblies()
    .Except(AppDomain.CurrentDomain.GetAssemblies().Select(assem => assem.GetName()))
    .Any()

I have 2 questions:我有两个问题:

  1. Can that ever return true ?那可以返回true吗? (I think "yes" [and hence the answer to the headline question is "no"], though I'm not certain). (我认为“是”[因此标题问题的答案是“否”],尽管我不确定)。
  2. Assuming it can... then how can I orchestrate that, for the purposes of a unit test?假设它可以......那么为了单元测试的目的,我该如何安排呢?

As I suspected, it is NOT a subset.正如我所怀疑的,它不是一个子集。

You CAN have a situation in which a ReferencedAssembly has not currently been loaded into the AppDomain.CurrentDomain .您可能会遇到当前尚未将ReferencedAssembly加载到AppDomain.CurrentDomain的情况。

To achieve this in a test:要在测试中实现这一点:

  1. Create a secondary csproj .创建辅助csproj ( ProjTwo ) ProjTwo
  2. Create a csproj -level dependency between your Test project and ProjTwo .在您的 Test 项目和ProjTwo之间创建一个csproj级别的依赖项。
  3. Add an unused class that references some code in ProjTwo .添加一个未使用的 class 引用ProjTwo中的一些代码。
    • If necessary add a static class with a static no-op method to ProjTwo , and a second static class which just invokes that method to your Test proj. If necessary add a static class with a static no-op method to ProjTwo , and a second static class which just invokes that method to your Test proj.
  4. Do NOT reference ProjTwo assembly anywhere in your TestFixtures.不要在 TestFixture 中的任何地方引用ProjTwo程序集。

Voila!瞧!


Step 4. appears to mean that ProjTwo does not get Loaded, whilst Step 3. ensures that the compiler can't remove the reference so it still turns up in GetReferencedAssemblies() , dodging this problem .步骤 4. 似乎意味着ProjTwo没有被加载,而步骤 3. 确保编译器无法删除引用,因此它仍然出现在GetReferencedAssemblies()中,从而避免了这个问题

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#WinForm中的AppDomain.CurrentDomain.GetAssemblies() - AppDomain.CurrentDomain.GetAssemblies() In C# WinForm AppDomain.CurrentDomain.GetAssemblies失败并显示ReflectionTypeLoadException - AppDomain.CurrentDomain.GetAssemblies fails with ReflectionTypeLoadException AppDomain.CurrentDomain.GetAssemblies()。SelectMany()发生了什么变化? - What has changed with AppDomain.CurrentDomain.GetAssemblies().SelectMany()? ASP.NET - AppDomain.CurrentDomain.GetAssemblies() - AppDomain重新启动后丢失的程序集 - ASP.NET - AppDomain.CurrentDomain.GetAssemblies() - Assemblies missing after AppDomain restart 使用Sybase时AppDomain.CurrentDomain.GetAssemblies()引发System.Reflection.ReflectionTypeLoadException - AppDomain.CurrentDomain.GetAssemblies() throws System.Reflection.ReflectionTypeLoadException when using Sybase 如果我使用AppDomain.CurrentDomain.GetAssemblies()。GetTypes(),有人可以执行代码注入攻击吗? - Would somebody be able to execute a code injection attack if I use AppDomain.CurrentDomain.GetAssemblies().GetTypes()? 为什么AppDomain.CurrentDomain.GetAssemblies()在某些条件下不返回Global.asax中的依赖程序集? - Why does AppDomain.CurrentDomain.GetAssemblies() not return dependant assemblies in Global.asax under certain conditions? UWP中AppDomain.GetAssemblies()的等价性? - Equivalence for AppDomain.GetAssemblies() in UWP? Silverlight 4中是否真的支持AppDomain.GetAssemblies()? - Is AppDomain.GetAssemblies() really supported in Silverlight 4? AppDomain.CurrentDomain.AssemblyResolve异步吗? - AppDomain.CurrentDomain.AssemblyResolve asynchronously?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM