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.
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?