Question

I want to list all methods from a WCF service that has the attribute "OperationContractAttribute"

For that, I use the following code:

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();

So, the OperationContractAttribute is specified in the interface (IClassA) and when I try to search this method attribute in the ClassA class, it can't find it however I specified the flag true for method GetCustomAttributes for searching to ancestors

Était-ce utile?

La solution

This will do

 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;
                }
            }

Autres conseils

webMethods = service.GetInterface(serviceContract).GetMethods().Where(
    method => method.GetCustomAttributes
      (typeof(OperationContractAttribute)).Any())
      .ToList();
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top