C# - 从程序集中的文件夹中获取所有接口(interface)

标签 c# .net reflection

我有一些 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/

相关文章:

c# - 使用 roslyn 扩展 C# 语法

c# - 使用 Fluent NHibernate 自动映射字典时,不考虑元素列长度

c# - 满足开放/封闭原则的工厂模式?

c# - 后台任务中的 Sqlite(Windows 运行时组件项目)

c# - Type.IsSubclassOf 和 TypeInfo.IsSubclassOf 有什么区别?

java - 动态调用方法名

c# - 键盘事件未在 RT 应用程序中触发

C# ListView - 搜索准确的时间值 (HH :mm:ss) or closest value

c# - 在 WPF 中定位自身时窗口闪烁

asp.net - ASP.NET MVC PartialView 可以有 _Layout