给定一个将方法作为输入的方法,获取其 class 和方法名称
given a method that takes a method as input, get its class and method name
我想知道是否可以获取方法和class方法输入函数的名称。我会尝试更具体:
我有一个 class Form1.cs
,我在其中创建了这个方法:
public static void CalculateTime(Action<string> funcToCalc, string where)
{
var watch = System.Diagnostics.Stopwatch.StartNew();
funcToCalc(where);
watch.Stop();
var elapsedMs = watch.ElapsedMilliseconds;
// gets the textbox name
TextBox t = Application.OpenForms["Form1"].Controls["textBox1"] as TextBox;
// writes in the textbox of this form this string
t.AppendText(funcToCalc.Method.Name + " executed in " + elapsedMs + "ms;\n");
}
在这个class中我是这样调用这个方法的:
CalculateTime( Class1.CreateStuff, textPath.Text);
CalculateTime( Class2.CreateStuff, textPath.Text);
我想做的是打印出类似
的东西
"MyClass1.CreateStuff executed in 100ms"
"MyClass2.CreateStuff executed in 75ms"
等等
现在,我的方法打印出来
"CreateStuff executed in 100ms"
"CreateStuff executed in 75ms"
这不允许我识别所调用方法的 class。
在这种特殊情况下,有没有办法获取 class 名称?
请注意,如果在我的 CalculateTime
中我调用
System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name;
我得到字符串 "Form1"
。
最简单的方法是请求委托的 Method
属性 指向正在执行的方法。
请注意,虽然它适用于静态成员和实例成员,但在匿名委托上调用时不一定有意义(当返回无意义的 compiler-created 方法名称时):
using System;
public class Foo1
{
// static
public static void Method( string s )
{
}
}
public class Foo2
{
// instance
public void Method( string s )
{
}
}
public class Program
{
public static void Main(string[] args)
{
PrintDelegateInfo( Foo1.Method );
PrintDelegateInfo( new Foo2().Method );
PrintDelegateInfo( (Action<string>)(s => {}) );
}
private static void PrintDelegateInfo(Action<string> funcToCall)
{
var methodInfo = funcToCall.Method;
var name = string.Format( "{0}.{1}", methodInfo.DeclaringType.Name, methodInfo.Name );
Console.WriteLine( "{0} called", name );
}
}
输出:
Foo1.Method called
Foo2.Method called
Program.<Main>b__0 called
您可以在操作本身上使用扩展方法 GetMethodInfo(Delegate) 以获得委托的 MethodInfo。
假设您没有像 Lambda 函数那样传递匿名类型的委托,您可以使用此 MethodInfo 来使用 DeclaringType 获取 class 类型,如下所示:
public static void CalculateTime(Action<string> funcToCalc, string where)
{
var miAction = funcToCalc.GetMethodInfo();
var actionContainingType = miAction.DeclaringType;
var watch = System.Diagnostics.Stopwatch.StartNew();
funcToCalc(where);
watch.Stop();
var elapsedMs = watch.ElapsedMilliseconds;
// gets the textbox name
TextBox t = Application.OpenForms["Form1"].Controls["textBox1"] as TextBox;
// writes in the textbox of this form this string
t.AppendText($"{actionContainingType.Name}.{funcToCalc.Method.Name} executed in {elapsedMs} ms;\n");
}
我想知道是否可以获取方法和class方法输入函数的名称。我会尝试更具体:
我有一个 class Form1.cs
,我在其中创建了这个方法:
public static void CalculateTime(Action<string> funcToCalc, string where)
{
var watch = System.Diagnostics.Stopwatch.StartNew();
funcToCalc(where);
watch.Stop();
var elapsedMs = watch.ElapsedMilliseconds;
// gets the textbox name
TextBox t = Application.OpenForms["Form1"].Controls["textBox1"] as TextBox;
// writes in the textbox of this form this string
t.AppendText(funcToCalc.Method.Name + " executed in " + elapsedMs + "ms;\n");
}
在这个class中我是这样调用这个方法的:
CalculateTime( Class1.CreateStuff, textPath.Text);
CalculateTime( Class2.CreateStuff, textPath.Text);
我想做的是打印出类似
的东西"MyClass1.CreateStuff executed in 100ms"
"MyClass2.CreateStuff executed in 75ms"
等等
现在,我的方法打印出来
"CreateStuff executed in 100ms"
"CreateStuff executed in 75ms"
这不允许我识别所调用方法的 class。
在这种特殊情况下,有没有办法获取 class 名称?
请注意,如果在我的 CalculateTime
中我调用
System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name;
我得到字符串 "Form1"
。
最简单的方法是请求委托的 Method
属性 指向正在执行的方法。
请注意,虽然它适用于静态成员和实例成员,但在匿名委托上调用时不一定有意义(当返回无意义的 compiler-created 方法名称时):
using System;
public class Foo1
{
// static
public static void Method( string s )
{
}
}
public class Foo2
{
// instance
public void Method( string s )
{
}
}
public class Program
{
public static void Main(string[] args)
{
PrintDelegateInfo( Foo1.Method );
PrintDelegateInfo( new Foo2().Method );
PrintDelegateInfo( (Action<string>)(s => {}) );
}
private static void PrintDelegateInfo(Action<string> funcToCall)
{
var methodInfo = funcToCall.Method;
var name = string.Format( "{0}.{1}", methodInfo.DeclaringType.Name, methodInfo.Name );
Console.WriteLine( "{0} called", name );
}
}
输出:
Foo1.Method called
Foo2.Method called
Program.<Main>b__0 called
您可以在操作本身上使用扩展方法 GetMethodInfo(Delegate) 以获得委托的 MethodInfo。 假设您没有像 Lambda 函数那样传递匿名类型的委托,您可以使用此 MethodInfo 来使用 DeclaringType 获取 class 类型,如下所示:
public static void CalculateTime(Action<string> funcToCalc, string where)
{
var miAction = funcToCalc.GetMethodInfo();
var actionContainingType = miAction.DeclaringType;
var watch = System.Diagnostics.Stopwatch.StartNew();
funcToCalc(where);
watch.Stop();
var elapsedMs = watch.ElapsedMilliseconds;
// gets the textbox name
TextBox t = Application.OpenForms["Form1"].Controls["textBox1"] as TextBox;
// writes in the textbox of this form this string
t.AppendText($"{actionContainingType.Name}.{funcToCalc.Method.Name} executed in {elapsedMs} ms;\n");
}