我想列出具有属性“OperationContractAttribute”的WCF服务的所有方法
为此,我使用以下代码:
var service = assembly.GetType(typeName);
if (service == null)
return webMethodsInfo;
var methodsInfo = service.GetMethods();
webMethods = methodsInfo.Where(method => method.GetCustomAttributes
(typeof(OperationContractAttribute), true).Any()).ToList();
因此,在接口(IClassA)中指定了OperationContractAttribute,当我尝试在ClassA类中搜索此方法属性时,它无法找到它,但我为方法GetCustomAttributes指定了标志为true以搜索祖先
答案 0 :(得分:1)
这样做
MethodInfo[] methods = typeof(ITimeService).GetMethods();
foreach (var method in methods)
{
if (((System.Attribute)(method.GetCustomAttributes(true)[0])).TypeId.ToString() == "System.ServiceModel.OperationContractAttribute")
{
string methodName = method.Name;
}
}
答案 1 :(得分:0)
webMethods = service.GetInterface(serviceContract).GetMethods().Where(
method => method.GetCustomAttributes
(typeof(OperationContractAttribute)).Any())
.ToList();