Is there a way to use reflection to get private static methods in a class without any local functions defined in these methods?
For example, I have a class:
public class Foo {
private static void FooMethod(){
void LocalFoo(){
}
}
}
If I use reflection to capture private static methods as follows:
var methods = typeof(Foo).GetMethods(BindingFlags.Static|BindingFlags.NonPublic)
.Select(m=>m.Name).ToList();
Then I get something like:
FooMethod
<FooMethod>g__LocalFoo5_0
With gnarly compiler-generated local function name.
So far, the best I've been able to come up with is to add a Where clause that will filter out local functions, for example:
var methods = typeof(Foo).GetMethods(BindingFlags.Static|BindingFlags.NonPublic)
.Where(m=>!m.Name.Contains("<")
.Select(m=>m.Name).ToList();
or
var methods = typeof(Foo).GetMethods(BindingFlags.Static|BindingFlags.NonPublic)
.Where(m=>!m.Name.Contains("__")
.Select(m=>m.Name).ToList();
source
share