Hi I am having a slight problem with dynamically listing the methods of a selected class. The code I have written is:

private void ListMethods(ClassA aClass, ComboBox cmbMethods)
{
       Type type = typeof(Application35.ClassA);
       foreach(MethodInfo aMethod in type.GetMethods())
       {
            cmbMethods.Items.Add(aMethod.ToString());
        }
        cmbMethod.Visible = true;
}

There are a number of overloads for this method, each dealing with a different class.

The method functions, however the list box is populated with more than just the method name, it includes the return type as well, also all of the methods inherited by the class are listed.

Does anyone know how to list just the name of the method and only methods that are defined within the class and not within parent classes please?

Dani AI

Generated

: a reliable fix is twofold — ask Type for only the members declared on that type, and read MethodInfo.Name rather than MethodInfo.ToString. was on the right track: inspecting MethodInfo properties gives the required pieces.

Example pattern (keeps inherited methods out and returns only names):

using System.Reflection;

void ListDeclaredMethods(object instance, ComboBox cmbMethods)
{
    var type = instance.GetType();
    var flags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly;
    var methods = type.GetMethods(flags)
                      .Where(m => !m.IsSpecialName)
                      .OrderBy(m => m.Name);

    foreach (var m in methods)
        cmbMethods.Items.Add(m.Name);

    cmbMethods.Visible = true;
}

Notes and troubleshooting:

  • BindingFlags.DeclaredOnly excludes inherited members. Add BindingFlags.Static or BindingFlags.NonPublic if static or non-public methods are needed.
  • Property accessors and operator overloads are "special names"; filter with !m.IsSpecialName to skip them.
  • To show overload signatures instead of just names, combine m.Name, m.ReturnType, and m.GetParameters() (formatting shown above can be extended).
  • Extension methods live in separate static classes and will not appear on the target type via GetMethods unless those static classes are inspected.
  • The original method declared a ClassA aClass parameter but used typeof(...); using instance.GetType() ensures any derived runtime type is reflected.
  • Small bug to watch for: the parameter is cmbMethods but the code sets cmbMethod.Visible — confirm the correct control name.

See the .NET docs for details on Type.GetMethods, BindingFlags, and .

Recommended Answers

All 3 Replies

if MethodInfo instance.Property = ? get else don't get

Thanks for the reply. Could you please explain what the

= ?

means, as have never come across it before, or have i miss read and i need to replace the ? with somthing else?

Yes, you should read in the MethodInfo class properties, and you'll get the solution yourself, believe me, you'll.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.