c# - 如何从不同的程序集中检索类的接口(interface)?

标签 c# reflection

在 WCF 中,通常将接口(interface)和实现类放在单独的程序集中。我正在构建一个基于配置创建 WCF 代理类的类(无论是来 self 的 web.config 中的服务或 serviceActivations 还是来自 svc 文件中的服务属性)。在我的类(class)中,我想检索可从服务类分配的接口(interface) - 在类和接口(interface)位于同一程序集中的情况下,这很容易:

Type type = Type.GetType("MyNamespace.MyClass, MyAssembly");
if (type != null)
{
    var interfaces =
    Assembly.GetAssembly(type)
            .GetTypes()
            .Where(t => t.IsInterface && t.IsAssignableFrom(type));               
}

除了通过获取所有程序集(使用 CurrentDomain.GetAssemblies())并以这种方式查找接口(interface)来使用一些强力反射解决方案之外,是否有规定的方法从不同的(未知)程序集获取接口(interface)?

此外,我知道如何使用 WCF 发现而不是自己完成这项工作 - 不幸的是,在我的情况下这不是一个选项。

最佳答案

原来有一个名为“GetInterfaces()”的方便的反射方法可以提供此信息:

Type type = Type.GetType(activation.Service);
if (type != null)
   Type[] interfaces = type.GetInterfaces();

关于c# - 如何从不同的程序集中检索类的接口(interface)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15422400/

相关文章:

c# - 事件默认初始化器

c# - Moq.CaSTLeProxyFactory' 无法加载文件或程序集 'CaSTLe.Core

reflection - 通过 MetadataType 属性获取应用于生成实体的自定义属性

java - 替代 String.equals 进行多次比较(无枚举)

swift - 在 Swift 中获取对象的类名作为字符串

c# - 应用程序.DoEvents();

c# - .NET ConcurrentDictionary.ToArray() 参数异常

c# - 使用简单注入(inject)器在 C# 中实现域事件处理程序模式

c# - Assembly.LoadFile 在可执行文件的位置查看依赖关系

c# - 一个方法如何只包含一个 NotImplementedException 并且仍然运行而不抛出?