سؤال

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

هل كانت مفيدة؟

المحلول

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

نصائح أخرى

webMethods = service.GetInterface(serviceContract).GetMethods().Where(
    method => method.GetCustomAttributes
      (typeof(OperationContractAttribute)).Any())
      .ToList();
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top