我有一些 WCF 服务,并且在某个文件夹的程序集中有一个服务契约(Contract)(接口(interface))列表。我知道命名空间,它看起来像这样:
MyProject.Lib.ServiceContracts
我希望有一种方法能够获取该文件夹中的所有文件,这样我就可以遍历每个文件并获取每个方法的属性。
以上是否可行?如果是这样,关于如何执行上述操作有什么建议吗?
感谢您的帮助。
最佳答案
这应该为您提供所有这样的接口(interface):
string directory = "/";
foreach (string file in Directory.GetFiles(directory,"*.dll"))
{
Assembly assembly = Assembly.LoadFile(file);
foreach (Type ti in assembly.GetTypes().Where(x=>x.IsInterface))
{
if(ti.GetCustomAttributes(true).OfType<ServiceContractAttribute>().Any())
{
// ....
}
}
}
关于C# - 从程序集中的文件夹中获取所有接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5650293/