I'm writing a reflection tool that I'll use for invoking methods from various types, and I'm testing it on simple programs.
I'm curious as why it doesn't return my Main() method on standard Visual Studio generated Program class
class Program
{
static void Main(string[] args)
{ return ; }
When I load type Program, and call type.GetMethods(); it returns 4 methods inherited from Object : ToString, GetHashCode, GetType and Equals.
I'm guessing Main is a special method as it's program's entry point, but there should be a way to retrieve its MethodInfo. Is there a way to get it?
Your Main method is private, so you need to include BindingFlags.NonPublic.
(BindingFlags.Static is included by default, but NonPublic isn't.)
So:
var methods = type.GetMethods(BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Static | BindingFlags.Instance);
(I'm assuming you want the public and instance methods as well, of course.)
Although Main is identified as the entry point here, there's nothing else that's particularly special about it - you can find it in the same way as other methods, and invoke it too.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With