在 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/