« Back to blog

Retrieving all the Methods in a Service

Well, today i was in a real fix. I was asked to work out a manner by which i show all the methods of a service in a combo-box. Now, people told me to ideally have a look at the proxy and type out the method names as they were and then create a static list of strings which i could then bind to the combo-box. WOW!! So simple ain't it?!? "And what if the list of methods were to change", i asked. The reply came, well, you can guess what the reply was. Someone then suggested a method involving WSDL. I do not know what that meant, as the idea was instantly trashed and i was never explained the details. Either way i smelled unnecessary  labor there as well. So i put up a brave face and googled it. I refused to believe that the method list of a given class could not be retrieved. After all that was the crux of my project. So after a little bit of googling and a little asking around from a trusted colleague, i found a way: Type. [sourcecode language="csharp"] public List<string> GetMethodNames(){ Type type = client.GetType(); var methods = type.GetMethods(); List<string> methodNames = new List<string>(); methodNames = methods.select(x=>x.Name).ToList<string>(); return methodNames; } [/sourcecode] So i can actually send in the type of service client (for which the method name list is required) to the GetMethodNames function and get different lists for different services. Thus, allowing me to dynamically get the method lists, which will take care of cases when the service changes the methods in terms of names and numbers. Ofcourse, in case of a Silverlight project, we will have to take care of things like the "Async" prefix. So we just have to remove that. Simple enough. So that is that!! :D