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

发布于 2024-11-01 05:03:43 字数 231 浏览 1 评论 0原文

我有一些 WCF 服务,并且在某个文件夹内的程序集中有一个服务契约(接口)列表。我知道命名空间,它看起来像这样:

MyProject.Lib.ServiceContracts

我希望有一种方法能够获取该文件夹中的所有文件,这样我就可以迭代每个文件并获取每个方法的属性。

以上可能吗?如果是这样,关于如何执行上述操作有什么建议吗?

感谢您的任何帮助。

I have some WCF services and I have a list of service contracts (interfaces) in an assembly within a certain folder. I know the namespace and it will look something like this:

MyProject.Lib.ServiceContracts

I was hoping there was a way to be able to grab all files within that folder so I can iterate over each one and grab the attributes off of each method.

Is the above possible? If so, any advice on how to do the above?

Thanks for any assistance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

§普罗旺斯的薰衣草 2024-11-08 05:03:43

这应该会给你所有这样的接口

    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())
            {
                // ....

            }
        } 
    }

This should get you all such interfaces:

    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())
            {
                // ....

            }
        } 
    }
撑一把青伞 2024-11-08 05:03:43

@Aliostad 答案已经发布,但我会添加我的答案,我认为它更彻底......

// add usings:
          // using System.IO;
          // using System.Reflection;
          public Dictionary<string,string> FindInterfacesInDirectory(string directory)
          {
             //is directory real?
             if(!Directory.Exists(directory))
             {
                //exit if not...
                throw new DirectoryNotFoundException(directory);
             }

             // set up collection to hold file name and interface name
             Dictionary<string, string> returnValue = new Dictionary<string, string>();

             // drill into each file in the directory and extract the interfaces
             DirectoryInfo directoryInfo = new DirectoryInfo(directory);
             foreach (FileInfo fileInfo in directoryInfo.GetFiles() )
             {
                foreach (Type type in Assembly.LoadFile(fileInfo.FullName).GetTypes())
                {
                   if (type.IsInterface)
                   {
                      returnValue.Add(fileInfo.Name, type.Name);
                   }
                }
             }

             return returnValue;

          }

@Aliostad answer is already posted, but I will add mine as well as I think it a bit more thorough...

// add usings:
          // using System.IO;
          // using System.Reflection;
          public Dictionary<string,string> FindInterfacesInDirectory(string directory)
          {
             //is directory real?
             if(!Directory.Exists(directory))
             {
                //exit if not...
                throw new DirectoryNotFoundException(directory);
             }

             // set up collection to hold file name and interface name
             Dictionary<string, string> returnValue = new Dictionary<string, string>();

             // drill into each file in the directory and extract the interfaces
             DirectoryInfo directoryInfo = new DirectoryInfo(directory);
             foreach (FileInfo fileInfo in directoryInfo.GetFiles() )
             {
                foreach (Type type in Assembly.LoadFile(fileInfo.FullName).GetTypes())
                {
                   if (type.IsInterface)
                   {
                      returnValue.Add(fileInfo.Name, type.Name);
                   }
                }
             }

             return returnValue;

          }
海拔太高太耀眼 2024-11-08 05:03:43

对于使用 C++/CLI 创建的程序集(即具有托管/非托管代码的程序集),上述答案可能无法正常工作。

我建议将此行: 替换

foreach (Type ti in assembly.GetTypes().Where(x=>x.IsInterface))

为此行:

foreach (Type ti in assembly.GetExportedTypes().Where(x=>x.IsInterface))

The above answers may not work correctly for Assemblies created with C++/CLI (i.e. an assembly with both managed/unmanaged code).

I suggest replacing this line:

foreach (Type ti in assembly.GetTypes().Where(x=>x.IsInterface))

with this line:

foreach (Type ti in assembly.GetExportedTypes().Where(x=>x.IsInterface))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文