1

When I run this Code attrs values is empty

IEnumerable<object> attrs = ((typeof(Data).GetMethods().Select
(a => a.GetCustomAttributes(typeof(WebGetAttribute),true))));  
WebGetAttribute wg= attrs.First() as WebGetAttribute;    // wg is null

this is my class to reflect:

public class Data
    {
       [WebInvoke(Method = "GET", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, UriTemplate = "/GetData")]
       string GetData(int value)
       {
           return "";
       }
    }

Please I need Help to Know this information (Method Type/ResponseFormat/UriTemplate) about each method in WCF service

Islam Ibrahim
  • 85
  • 1
  • 9
  • perhap's I misunderstood the question (I seem to be in a bad day for understanding questions ;-), but shouldn't it be a.GetCustomAttributes(typeof(WebInvokeAttribute)... ? – jbl Aug 13 '13 at 14:03
  • I try WebInvokeAttribute but not working – Islam Ibrahim Aug 13 '13 at 14:12
  • GetMethods without using BindingFlags parameters returns only the public methods. GetData seems to be private – jbl Aug 13 '13 at 14:14

2 Answers2

2

You don't seem to select NonPublic methods or the correct attribute type.

You may give a try to :

IEnumerable<object> attrs = 
     typeof(Data).GetMethods(BindingFlags.Public|BindingFlags.NonPublic)
      .SelectMany(a => a.GetCustomAttributes(typeof(WebInvokeAttribute),true));  

WebInvokeAttribute wi = attrs.First() as WebInvokeAttribute ;    
jbl
  • 15,179
  • 3
  • 34
  • 101
  • jbl this code work successful but when i put class Data to WCF Service that contains interface (IData) and class (Data) this code not working – Islam Ibrahim Aug 19 '13 at 11:44
  • @user2678754 isn't the attribute on IData ? If so, it is on this interface that you should call your code, not Data. Or you can inspect all typeof(Data).GetInterfaces() in search for a WebInvoke attribute – jbl Aug 19 '13 at 11:57
  • i do that but still not working: `MyService.Service1Client obj = new MyService.Service1Client(); Type[]ins= obj.GetType().GetInterfaces(); MethodInfo t= ins[2].GetMethod("GetData"); // this my interface WebInvokeAttribute wii = t.GetCustomAttributes(typeof(WebInvokeAttribute), true).FirstOrDefault() as WebInvokeAttribute;` – Islam Ibrahim Aug 19 '13 at 12:25
  • Can i fill WebInvokeAttribute? – Islam Ibrahim Aug 19 '13 at 12:47
  • @user2678754 Difficult to tell here, but the number 2 in ins[2] seems like a magic number here. Are you sure it is IData ? I guess you should try to select the interface which has a ServiceContract attribute. – jbl Aug 19 '13 at 12:47
  • @user2678754 seems like you can http://stackoverflow.com/a/51282/1236044 but it is not recommended http://stackoverflow.com/a/2160498/1236044 One of the reason could be that you don't control the flow and don't know if third party code has read the value once and for all before you change it. – jbl Aug 19 '13 at 12:53
0

jbl is correct. Without the BindingFlags parameter, GetMethods will not return non-public methods. Also, as WebInvokeAttribute doesn't inherit WebGetAttribute it will not be returned by GetCustomAttributes.

The following code will select the WebInvokeAttributes for all public and non-public methods:

IEnumerable<object> attrs = typeof(Data)
    .GetMethods(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)
    .SelectMany(a => a.GetCustomAttributes(typeof(WebInvokeAttribute), true));
Talon
  • 143
  • 1
  • 7