简体   繁体   English

反射:使用typeof()或IsAssignableFrom()检查类实现的接口时的行为不同

[英]Reflection : Different behavior while using typeof() or IsAssignableFrom() to check interface implemented by a class

I have two interfaces IConfigurationManager and ILocator defined in the same assembly (lets call it assembly "A") and the same namespace. 我在同一个程序ILocator定义了两个接口IConfigurationManagerILocator (将其称为程序集“ A”)和相同的名称空间。

There is a start-up project called "B" which has "project reference" to the assembly "A". 有一个名为“ B”的启动项目,该项目对程序集“ A”具有“项目引用”。

Assembly A contains concrete implementations of both the interfaces too. 程序集A也包含两个接口的具体实现。 I have a "post-build event" on assembly A which copies DLL to the "B" project's 'bin\\debug' folder from where application is run when i run it from visual studio 2012. 我在程序集A上有一个“生成后事件”,当我从Visual Studio 2012中运行DLL时,它将DLL复制到运行应用程序的“ B”项目的“ bin \\ debug”文件夹中。

In the app.config file, I mention which assembly and type to use when I want concrete implementations of both the interfaces. 在app.config文件中,我提到了当我想要两个接口的具体实现时要使用的程序集和类型。 (I need to resolve type mapping at run time and will have interfaces and implementations in different assemblies which I many not develope at all.) (我需要在运行时解析类型映射,并将在我根本不开发的不同程序集中具有接口和实现。)

In my code, I load assembly A (using Assembly.LoadFile() ) from the 'bin\\debug\\' folder of the start-up project. 在我的代码中,我从启动项目的“ bin \\ debug \\”文件夹中加载程序集A(使用Assembly.LoadFile() )。 Then I iterate through all types and check if a class is of type ILocator using this code: 然后,我遍历所有类型,并使用以下代码检查类是否为ILocator类型:

if(type.GetInterfaces.Contains(typeof(IConfigurationManager))

This returns FALSE but on the other hand, similar code for the ILocator returns TRUE. 这将返回FALSE,但是另一方面,类似ILocator代码将返回TRUE。 Why does this happen? 为什么会这样? I tried to use IsAsseignableFrom() too, but I got the same result. 我也尝试使用IsAsseignableFrom() ,但是得到了相同的结果。

Why one call succeeds and other fails when everything (like assembly in which interfaces declared, assembly from which type is loaded etc.) else is same while checking interfaces implemented by a class? 为什么当检查类实现的接口的其他所有内容(例如声明了接口的程序集,装入类型的程序集等)都相同时,为什么一个调用成功而另一个失败?

Note: I am aware of the Unity container etc. But I still have the same question related to type checking using reflection. 注意:我知道Unity容器等。但是我仍然有关于使用反射进行类型检查的相同问题。

Probably because the 'everything else' is not the same. 可能是因为“其他所有东西”都不一样。 Since your B project has a project reference to A, it ends up loading A twice: first by CLR, when B uses the type ILocator from A, and then when you explicitly load A with LoadFile . 由于您的B项目具有对A的项目引用,因此最终将A加载两次:首先是CLR,当B使用A的ILocator类型,然后使用LoadFile显式加载A时。 Because LoadFile loads assemblies into a different binding context , you get two copies of A in your application domain, with two sets of types which (confusingly) have the same FullName etc. but are nevertheless different . 因为LoadFile将程序集加载到不同的绑定上下文中 ,所以您将在应用程序域中获得A的两个副本,其中包含两组类型(令人困惑地)具有相同的FullName等,但是却有所不同 You may check this by comparing the Assembly objects in the IConfigurationManager from type.GetInterfaces () (or type for that matter) and from typeof (IConfigurationManager) . 您可以通过比较IConfigurationManagerAssembly对象type.GetInterfaces ()来自type.GetInterfaces () (或该type )和typeof (IConfigurationManager)

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

相关问题 typeof(接口).IsAssignableFrom(intptr)返回true - typeof(interface).IsAssignableFrom(intptr) returns true 使用Reflection获取未由接口实现的对象的所有属性 - Using Reflection to get all properties of an object not implemented by an interface 将代码从反射移植到使用接口,某些 API 未实现 - Porting code from Reflection to using an Interface, some API's not implemented 如何使用反射确定已实现接口的通用类型? - How do I determine the generic type of an implemented interface using reflection? typeof(ICollection &lt;&gt;).GetTypeInfo()。IsAssignableFrom(typeof(IList &lt;&gt;)) - typeof( ICollection<> ).GetTypeInfo().IsAssignableFrom( typeof(IList<>) ) 使用反射来检查是否已实现部分方法 - Using reflection to check if a partial method has been implemented 使用Foq模拟具有显式实现的接口的类 - Mocking a class with an explicitly implemented interface using Foq 从 Assembly 获取通用接口而不是使用 typeof(Interface) 会出现意外行为 - Getting a generic Interface from Assembly instead of using typeof(Interface) have an unexpeted behavior 使用反射来获取继承接口的类的属性 - using reflection to get properties of class inheriting an interface 使用反射实例化实现通用接口的类 - instantiate a class implementing a generic interface using reflection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM