Beytan Kurt
Beytan Kurt

Reputation: 2263

Get all the method names in all of the classes

I'm trying to get all the method names in all of the classes in a vb.net project through a c# forms application.

I've come this far:

private void BindMethods()
{
    var assembly = typeof(VBProject.Class1).Assembly;
    var publicClasses = assembly.GetExportedTypes().Where(p => p.IsClass);

    foreach (var classItem in publicClasses)
    {
        BindMethodNames<classItem>();
    }
}

private void BindMethodNames<T>()
{
    MethodInfo[] methodInfos = typeof(T).GetMethods(BindingFlags.Public | BindingFlags.Static);

    Array.Sort(methodInfos,
        delegate(MethodInfo methodInfo1, MethodInfo methodInfo2)
        {
            return methodInfo1.Name.CompareTo(methodInfo2.Name)
        });

    foreach (var methodInfo in methodInfos)
    {
        this.comboMethods.Items.Add(methodInfo.Name);
    }
}

Now here is the problem, (since I'm doing something wrong) it doesn't allow me to use a type in the call of BindMethodNames<>() as for "classItem".

I guess the whole approach is wrong and I'd love to get some suggestions.

Upvotes: 2

Views: 5531

Answers (3)

Charleh
Charleh

Reputation: 14002

Your call to 'BindMethodNames' should use a type for the generic , not an instance, but you can't write code to do this without using reflection.

Then again, there is no point in using a generics here - wouldn't this work?

private void BindMethods() 
{ 
    var assembly = typeof(VBProject.Class1).Assembly; 
    var publicClasses = assembly.GetExportedTypes().Where(p => p.IsClass); 

    foreach (var classItem in publicClasses) 
    { 
        BindMethodNames(classItem); 
    } 
} 

private void BindMethodNames(Type type) 
{ 
    MethodInfo[] methodInfos = type.GetMethods(BindingFlags.Public | BindingFlags.Static); 

    Array.Sort(methodInfos, 
        delegate(MethodInfo methodInfo1, MethodInfo methodInfo2) 
        { 
            return methodInfo1.Name.CompareTo(methodInfo2.Name) 
        }); 

    foreach (var methodInfo in methodInfos) 
    { 
        this.comboMethods.Items.Add(methodInfo.Name); 
    } 
} 

Upvotes: 2

Jo&#227;o Angelo
Jo&#227;o Angelo

Reputation: 57718

Generics for this situation are not particular helpful. You already are iterating a list of Type instances retrieved from the selected assembly so your second method just needs to receive the target type as a parameter.

private void BindMethodNames(Type target)
{
    MethodInfo[] methodInfos = target.GetMethods(
        BindingFlags.Public | BindingFlags.Static);

    Array.Sort(methodInfos,
        delegate(MethodInfo methodInfo1, MethodInfo methodInfo2)
        {
            return methodInfo1.Name.CompareTo(methodInfo2.Name);
        });

    foreach (var methodInfo in methodInfos)
    {
        this.comboMethods.Items.Add(methodInfo.Name);
    }
}

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1503290

You'd need to call the method with reflection:

var method = typeof(Foo).GetMethod("BindMethodNames", BindingFlags.Instance |
                                                      BindingFlags.NonPublic);
var generic = method.MakeGenericMethod(classItem);
generic.Invoke(this);

Upvotes: 0

Related Questions