Assembly
我使用类打开 DLL 文件。现在我想获取具有[OperationContract]
属性的方法。怎么做?
Assembly assembly = Assembly.LoadFrom(someDLLFilePath);
Type[] classes = assembly.GetTypes();
Assembly
我使用类打开 DLL 文件。现在我想获取具有[OperationContract]
属性的方法。怎么做?
Assembly assembly = Assembly.LoadFrom(someDLLFilePath);
Type[] classes = assembly.GetTypes();
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;
没有一条指令可以做到这一点,您必须迭代方法并查看它是否具有该属性。你可以这样:
foreach (var type in classes)
{
type.GetMethods().Where(m => m.GetCustomAttributes(false).Contains(typeof (OperationContract)));
}
尝试这个:
var result = assembly
.DefinedTypes
.SelectMany(type => type.GetMethods()
.Where(method => method
.GetCustomAttributes<OperationContractAttribute>()
.Count() > 0)
);