0

Assembly我使用类打开 DLL 文件。现在我想获取具有[OperationContract]属性的方法。怎么做?

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

3 回答 3

2
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;
于 2013-05-14T09:50:09.073 回答
1

没有一条指令可以做到这一点,您必须迭代方法并查看它是否具有该属性。你可以这样:

foreach (var type in classes)
{
  type.GetMethods().Where(m => m.GetCustomAttributes(false).Contains(typeof (OperationContract)));
}
于 2013-05-14T09:43:40.893 回答
1

尝试这个:

var result = assembly
    .DefinedTypes
    .SelectMany(type => type.GetMethods()
                            .Where(method => method
                                .GetCustomAttributes<OperationContractAttribute>()
                                .Count() > 0)
        );
于 2013-05-14T09:49:52.210 回答