How do you dynamically get a particular method name in a strongly typed way?

I'm a little unsure of how to state the title of this question, but I'm looking for the shortest / aseist method in VB.NET (or C #, but using VB.NET at the moment) to get the string value of the method name dynamically given by the method call.

For example, I have a class like this:

Public Class Blah
    Public Sub Foo()
    End Sub
End Class

Now Foo is a strongly typed cover for the dynamic parameter, and I have an event handler that fires when the setting is changed and returns the row name of the changed setting.

I would like to be able to switch / select on this line and have a case based on the Foo () method. To do this, I need to get the string name of the Foo method from the method call itself (for example, GetMethodName (blahInstance.Foo ())).

+3
source share
1 answer

I don't have VB.NET right now, but in C # I think this is the answer. Does this look like what you are looking for? If so, VB.NET syntax should be relatively easy to develop:

     Blah blahInstance = new Blah();
     System.Action fooFunc = blahInstance.Foo;
     Console.WriteLine(fooFunc.Method.Name);
+7
source

Source: https://habr.com/ru/post/1706012/


All Articles