[英]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:我有两个问题:
true
?那可以返回true
吗? (I think "yes" [and hence the answer to the headline question is "no"], though I'm not certain). (我认为“是”[因此标题问题的答案是“否”],尽管我不确定)。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:要在测试中实现这一点:
csproj
.创建辅助csproj
。 ( ProjTwo
) ( ProjTwo
)csproj
-level dependency between your Test project and ProjTwo
.在您的 Test 项目和ProjTwo
之间创建一个csproj
级别的依赖项。ProjTwo
.添加一个未使用的 class 引用ProjTwo
中的一些代码。
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.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.