问题
var pt = Type.GetType("<Program>$");
var m = pt.GetMethod("<Main>$", BindingFlags.Static);
// m is null
Okay, I grab the Program
class, this works fine. But when I go to grab the Main
method, system cannot find it, and it's not in pt.GetMembers()
either. What's going on?
回答1:
You just need to specify that you want to see non-public members:
using System;
using System.Reflection;
var pt = Type.GetType("<Program>$");
var m = pt.GetMethod("<Main>$", BindingFlags.Static | BindingFlags.NonPublic);
Console.WriteLine(m); // Prints Void <Main>$(System.String[])
Likewise using GetMembers
, you need to specify you want public and non-public members:
using System;
using System.Reflection;
var pt = Type.GetType("<Program>$");
var flags =
BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Instance | BindingFlags.Static;
foreach (var member in pt.GetMembers(flags))
{
Console.WriteLine(member);
}
来源:https://stackoverflow.com/questions/64966831/cannot-find-main-method-using-reflection-with-net-5-top-level-calls