none
Ordering a MethodInfo array which is part of the Reflection namespace RRS feed

  • Question

  • Hi

    I only use c# from time to time. I'm writing a small program to help me get familiar with the different types of methods that are in a Class.

    The below program works ok but I would like to display the MethodInfo array by Name order. Can anyone advise how I should do this please :-

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Reflection;
    namespace FindingClassInformation
    {
        public partial class FormMain : Form
        {
            public FormMain()
            {
                InitializeComponent();
            }
            private void btnGetMethods_Click(object sender, EventArgs e)
            {
                //Obtain a list of methods that are inside the TextBox Class
                Type t = typeof(System.Windows.Forms.TextBox);
                MethodInfo[] mI = t.GetMethods();
                
                //Re-order them into Name order
                //This is the part I am stuck with :)
                //Then display the Methods in the TextBox Class
                foreach (MethodInfo mi in mI)
                {
                    ListViewClasses.Items.Add(mi.Name);
                }
                
                
            }
        }
    }

    Thank to anyone who can help me here.

    Kind Regards

    Matt

    • Moved by Lisa Zhu Monday, February 25, 2013 9:06 AM
    Saturday, February 23, 2013 1:51 PM

Answers

  • I would suggest to use the lambda version of the LINQ OrderBy extension methods as follows:

    Type t = typeof(System.Windows.Forms.TextBox);
    MethodInfo[] mI = t.GetMethods();
    
    //Re-order them into Name order Class
    foreach (MethodInfo mi in mI.OrderBy( x => x.Name))
    {
       Console.WriteLine(mi.Name);
    }


    Twitter: @CHBernasconiC
    Blog: www.claudiobernasconi.ch


    Saturday, February 23, 2013 3:34 PM

All replies

  • I would suggest to use the lambda version of the LINQ OrderBy extension methods as follows:

    Type t = typeof(System.Windows.Forms.TextBox);
    MethodInfo[] mI = t.GetMethods();
    
    //Re-order them into Name order Class
    foreach (MethodInfo mi in mI.OrderBy( x => x.Name))
    {
       Console.WriteLine(mi.Name);
    }


    Twitter: @CHBernasconiC
    Blog: www.claudiobernasconi.ch


    Saturday, February 23, 2013 3:34 PM
  • Claudio

    Thats brilliant. Thank you.

    The Linq lamda expression works brillaintly. Could you tell me why loads of methods appear that have an underscore in them. The AppendText method is the first one to show when I enter txtClassName. which is shown by IntelliSense.

    Is is because the underscore methods are Privates? but Type.Gethods gets all methods even non Public ones? In that case, how can I alter the program to just show the Public Method members please?

    Kind Regards

    Matt

    The output is along the lines of :-


    • Edited by jmatty2000 Saturday, February 23, 2013 10:28 PM correction
    Saturday, February 23, 2013 10:27 PM
  • Edited on 2013-02-25 (Original post was truncated)

    The methods starting with add_Xxx or remove_Xxx are the "setter" and the "remover" of delegate methods to the public events of the Control class.

    Some classes can implement specific add and remove methods (see below) used when you write un client code

    MyControl.Paint+=MyPaintMethod;

    or

    MyControl.Paint-=MyPaintMethod;

    For optimization reasons, it is possible to implement Events using specific add and remove methods.

    For complex components that handle many events, reserving a memory slot for each event can be very memory consuming. Using the add and remove methods allows the component writer (Microsoft in the System.Windows.Forms.Control case, the base class of TextBox class) to allocate memory dynamically to store delegates attached to event only if they are used and limit the memory consumption of each control in a WinForms application.

    In C# you will have something like this :

        public event PaintEventHandler Paint
        {
          add
          {
            events.AddHandler(Ctrl.EventPaint, (Delegate) value);
          }
          remove
          {
            events.RemoveHandler(Ctrl.EventPaint, (Delegate) value);
          }
        }
    You may want to hide these specific methods if needed.



    Sunday, February 24, 2013 3:46 PM
  • Hi Matt,

    From your description, I ‘d like to move this post to  the most related forum.

    There are more  experts in this aspect, so you will get  better support and  may have more luck getting answers.

    Thanks for your understanding.

    Regards,


    Lisa Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, February 25, 2013 9:04 AM
  • Hi Lisa

    To me, it looks like RBancel has added a discussion to this one which is nothing to do with my original topic which I raised at the beginning. I think that RBancel notes should be deleted from this discussion please and then this topic should appear in one of the c# discussion forums.

    Kind Regards

    Matt

    Monday, February 25, 2013 9:26 AM
  • I restored the original text of my truncated message. I expect it will help.
    Monday, February 25, 2013 8:33 PM
  • @Matt

    The methods you mention in your post above are methods created by the compiler. You didn't define them in you code file, right? So there are methods created for UI-specific things like the event register methods.

    This is kind a 'for dummies' explanation, but I think it's the easiest way to understand.

    You should know that when you access an assembly via Reflection you have some more low level information than while using Visual Studio.

    Another example would be .NET properties. While you're able to define a property like that:

    public string myProp { get; set; }

    The compiler will generate get_myProp and set_myProp methods, because the language itself only support methods, but not properties. It's the compiler which translates these properties into methods.

    I hope this helps you getting a better understanding. I recommend reading through several tutorial and articles, if you're interested.


    Twitter: @CHBernasconiC
    Blog: www.claudiobernasconi.ch


    Tuesday, February 26, 2013 6:45 PM
  • Hi Cladio

    I'm still not sure that I understand - maybe I'm not at that point yet.

    Would you like to join my c# beginner to expert group. I'd love for you to be a member, to help us learn and to stay motivated :-

    http://csharpbegin.codeplex.com/

    Kind Regards

    Matt

    Friday, March 1, 2013 6:10 PM