Get OperationContracts from DLL file using Assembly

I open DLL files using Assembly class. Now I want to get methods that have [OperationContract] attribute. How to do that?

Assembly assembly = Assembly.LoadFrom(someDLLFilePath);
Type[] classes = assembly.GetTypes();

Answers


var foo = from type in assembly.GetTypes()
          where type.GetCustomAttributes(false).OfType<ServiceContractAttribute>().Any()
          from method in type.GetMethods()
          where method.GetCustomAttributes(false).OfType<OperationContractAttribute>().Any()
          select method;

There isn't a single instruction to do that, you have to iterate on methods and see if it has the attribute. You could something like this:

foreach (var type in classes)
{
  type.GetMethods().Where(m => m.GetCustomAttributes(false).Contains(typeof (OperationContract)));
}

Try this:

var result = assembly
    .DefinedTypes
    .SelectMany(type => type.GetMethods()
                            .Where(method => method
                                .GetCustomAttributes<OperationContractAttribute>()
                                .Count() > 0)
        );

Need Your Help

OSGi web application not running

web osgi wab

I'm trying to figure out how to implement a web application bundle. In order to do that I created a small sample following the instructions in this tutorial. The war-file can be deployed and starte...

How to write a Linq query to pick the last max number in the given strings ?

sql-server string linq linq-to-sql

I have these strings in my column in my database. I am basically generating slug and don't want to duplicate any of the string. The strings can be in any order.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.