Getting all the methods in a type using LINQ

Hi

One of the most beautiful thing about the new LINQ queries in the Dot net 3.5 is the simplicity with which it lets you query any collection. Yesterday I was playing with the LINQ queries and suddenly a requirement came to me where by I wanted to display all the methods that string (Or any other class you want to choose) class has.

I know this can be done through reflection very easily. But the good thing about LINQ is the simplicity and small amount of code with which I could get the Job done.

All I had to do was declare an object of type “Type”. Assign the object the class I want to use. And use a very simple LINQ query to get all the methods in a collection. Here is the code.


Type tStr = typeof(string);
            var ms = from method in tStr.GetMethods()

                        select method;

            foreach( var m in ms)

            {

Console.WriteLine(m.Name);                Console.WriteLine("_________________________");

            }

            Console.Read();

Again in the same query if I wanted to get only static methods of the class then I will have to add a small where clause in the LINQ statement. Hence my new LINQ statement will look something like this.

            var ms = from method in tStr.GetMethods()

                     where method.IsStatic == true

                        select method;

This where clause will filter out only those methods which are static. There is still one problem with the result set. It returns back multiple records, as there are many overloads for each method. Now if I only want to see one property once I can use the group by clause and group the same by method name. Hence my query would be something like this.

var ms = from method in tStr.GetMethods()

                     where method.IsStatic == true

                     group  method by method.Name into g

                        select new

                        {

                            Name = g.Key

                        };

Here I have grouped the collection on the basis of the name. Also see how I have used the anonymous class to create a new type and use it. Now If I want to get the number of overload each one has then I can add that also. So at last my query becomes something like this.

            var ms = from method in tStr.GetMethods()

                     where method.IsStatic == true

                     group  method by method.Name into g

                        select new

                        {

                            Name = g.Key,

                            Count = g.Count()

                        };

       

            foreach( var m in ms)

            {

                Console.WriteLine(m.Name + ", Overload Count = " + m.Count.ToString());

                Console.WriteLine("_________________________");

            }

Here I have updated the anonymous type to add a property Count also. And I also add the count of overload for each method we have.

Now you can see why does LINQ makes it so simple for use to work with any collection.

Thanks
Vikram


Share this post   Email it

Feedback

Please post your comments:

Name:  
Email (optional): Your email address will not be posted.
URL (optional):
Comments: HTML will be ignored, URLs will be converted to hyperlinks  
Enter the text you see in the box:
 

Copyright © 2006 - 2010 Vikram Lakhotia