mardi 11 août 2015

Can you get MethodInfo from a static method of a static class using a lambda without a magic string?

Let's say I have this static class with a static method:

public static class MyClass
{
    public static void MyMethod() { }
}

I could get the MethodInfo like this:

MethodInfo MethodInfo = typeof(MyClass).GetMethod("MyMethod");

However, if I ever change the name of the method, the magical properties of the string will cause the method name to remain unchanged. If it were a non-static class/method, I could do this:

public class MyClass
{
    public void MyMethod() { }
}

MethodInfo MethodInfo = new Action<MyClass>(x => x.MyMethod()).Method;

This is great because it's a reference to the method signature itself, so I can change the original method name and then rename all references. However, this method doesn't work with the static version of my class/method.

Is there a way to use the lambda with a static class?





Aucun commentaire:

Enregistrer un commentaire