Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - Get all interfaces from a folder in an assembly

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.

like image 442
Brandon Avatar asked Apr 13 '11 13:04

Brandon


1 Answers

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

            }
        } 
    }
like image 105
Aliostad Avatar answered Nov 09 '22 04:11

Aliostad