C# 使用反射查找程序集中按名称包含接口的所有类
我正在研究Apache Thrift的一个实现,Thrift生成的所有服务类都包含一个名为Iface的嵌套接口 我还有一些额外的代码,它接受thrift生成的内容,并基于命名约定构建页面对象模式 我需要能够做的是,使用反射枚举生成的程序集中具有名为Iface的嵌套接口的所有类 所有这些代码以前都是用protobuf.net和Google协议缓冲区实现的,我们正在转换为更一致的多语言支持 在protobuf实现中,我们使用这一行查找正确的服务:C# 使用反射查找程序集中按名称包含接口的所有类,c#,reflection,thrift,C#,Reflection,Thrift,我正在研究Apache Thrift的一个实现,Thrift生成的所有服务类都包含一个名为Iface的嵌套接口 我还有一些额外的代码,它接受thrift生成的内容,并基于命名约定构建页面对象模式 我需要能够做的是,使用反射枚举生成的程序集中具有名为Iface的嵌套接口的所有类 所有这些代码以前都是用protobuf.net和Google协议缓冲区实现的,我们正在转换为更一致的多语言支持 在protobuf实现中,我们使用这一行查找正确的服务: _inputDll.GetTypes().Where
_inputDll.GetTypes().Where(x => typeof(IService).IsAssignableFrom(x) && x.Name != "Stub")
这种结构不再有效,因为thrift内部没有一个决定界面
下面是我试图搜索的节俭生成代码的示例:
public partial class ServiceA {
public interface Iface {
Thrift.ActionResult SetupPreferences(Thrift.BillingPreferencesInfo info);
}
// Thrift implementations
}
public partial class ServiceB {
public interface Iface {
Thrift.ActionResult SetupAddress(Thrift.AddressInfo info);
}
// Thrift implementations
}
如果源类如下所示:
public class SomeClass
{
public interface IFace { … }
}
someAssembly.GetTypes().Where(t => t.IsClass && t.GetNestedType("Iface") != null)
然后您的查询需要使用,如下所示:
public class SomeClass
{
public interface IFace { … }
}
someAssembly.GetTypes().Where(t => t.IsClass && t.GetNestedType("Iface") != null)
试试这个,这是一个有点旧的代码,但它可以工作:
public static System.Collections.Generic.List<Type> GetTypesImplementingInterface(string interfaceTypeName, string assembliesNameStartWith)
{
var returnTypes = new System.Collections.Generic.List<Type>();
var list = BuildManager.GetReferencedAssemblies().Cast<Assembly>().Where(referencedAssembly => referencedAssembly.FullName.StartsWith(assembliesNameStartWith, StringComparison.InvariantCultureIgnoreCase)).ToList();
try
{
returnTypes.AddRange(
from ass in list
from t in ass.GetTypes()
where (TypeImplementsInterface(t, interfaceTypeName))
select t
);
}
catch (Exception ex)
{
Trace.TraceWarning("GetTypesImplementingInterface:{0}:interfaceTypeName:{1}:assembliesNameStartWith:{2}",
ex.ToString(), interfaceTypeName, assembliesNameStartWith);
returnTypes = new System.Collections.Generic.List<Type>();
}
return returnTypes;
}
private static bool TypeImplementsInterface(Type theType, string interfaceName)
{
Type interFaceType = theType.GetInterface(interfaceName, true);
return (interFaceType != null);
}
public static System.Collections.Generic.List GetTypeSimplementInterface(string interfaceTypeName,string assembliesNameStartWith)
{
var returnTypes=new System.Collections.Generic.List();
var list=BuildManager.getReferencedAssembly().Cast().Where(referencedAssembly=>referencedAssembly.FullName.StartWith(AssembleNameStartWith,StringComparison.InvariantCultureIgnoreCase)).ToList();
尝试
{
returnTypes.AddRange(
从名单上的驴子
来自ass.GetTypes()中的t
式中(输入实现接口(t,接口名称))
选择t
);
}
捕获(例外情况除外)
{
Trace.TraceWarning(“GetTypeSimplementInterface:{0}:interfaceTypeName:{1}:assembliesNameStartWith:{2}”,
例如,ToString(),interfaceTypeName,assembliesNameStartWith);
returnTypes=new System.Collections.Generic.List();
}
退货类型;
}
私有静态布尔类型实现接口(键入类型,字符串接口名称)
{
Type interFaceType=Type.GetInterface(interfaceName,true);
返回(interFaceType!=null);
}
特别是在这个实现中,我没有尝试过任何东西,因为我不知道如何继续,因为我没有访问多个不同接口的权限来获取其类型,我将为这个问题添加更多的细节。所谓“嵌套接口”,你真的是指类范围内的接口声明吗,或者您使用“嵌套”一词而不是“实现”?你最好提供一个示例类。不,我指的是在类中声明的接口。这是我遇到的节俭的一个缺点。我仍然在试图找出你想要通过节俭实现什么?背后的想法是什么?特别是,为什么要使用反射来查找特定的Thrift接口?Thrift实现用于Ui自动化测试,并且基于我们在Thrift中定义的服务的名称,我们有一些代码生成其他代码来创建页面对象模式或状态机。因此,无论是谁编写测试,都不必知道所有的服务,也不必知道每一时刻都可以执行哪些操作。谢谢。问题是我要找的类在ass.GetTypes()中没有实现任何来自t的接口。。。这到底是什么回报?:)程序集中的类型或战利品的类型:P