我想在dotnet核心应用程序中获取当前正在执行的方法的名称.
有很多关于如何使用常规c#执行此操作的示例,例如
然而,这两种方法的api似乎还没有核心(参见https://github.com/dotnet/corefx/issues/1420)
有没有其他方法可以在.net核心中获取执行方法名称?
我想要调用当前Handler的名称.
MethodInfo.GetCurrentMethod().Name或者MethodBase.GetCurrentMethod().Name在调试模式下正常工作.
但是一旦我混淆了(使用confuserEx)我的项目,2个函数就会返回"System.Reflection.MethodBase ()".
我注意到我可以使用以下行获取我的函数名称:
((RoutedEventHandler)this.MyMethodName).GetMethodInfo().Name
它返回"MyMethodName"哪个是预期结果.
但它根本不是通用的.当我不知道当前方法的名称时,我想要一段代码.
private void Method1()
{
//Do something
Log("Something","Method1");
}
private void Method2()
{
//Do something
Log("Something","Method2");
}
private void Log(string message, string method)
{
//Write to a log file
Trace.TraceInformation(message + "happened at " + method);
}
Run Code Online (Sandbox Code Playgroud)
我有几个方法,如上面的Method1和Method2,我想有一些方法将方法的名称作为参数传递,而无需手动编辑代码.
那可能吗?
我正在尝试实现以下模式函数:
MethodInfo GetMethod(
Expression<Func<TTarget, EventHandler<TEventArgs>>> method)
Run Code Online (Sandbox Code Playgroud)
如果需要,我可以提供TTarget的实例
所需的用法是:
public static void Main(string[] args)
{
var methodInfo = GetMethod<Program, EventArgs>(t => t.Method);
Console.WriteLine("Hello, world!");
}
private void Method(object sender, EventArgs e)
{
}
Run Code Online (Sandbox Code Playgroud)
这是我到目前为止所尝试的:
private static MethodInfo GetMethod(TTarget target, Expression<Func<TTarget, EventHandler<TEventArgs>>> method)
{
var lambda = method as LambdaExpression;
var body = lambda.Body as UnaryExpression;
var call = body.Operand as MethodCallExpression;
var mInfo = call.Method as MethodInfo;
Console.WriteLine(mInfo);
throw new NotImplementedException();
}
Run Code Online (Sandbox Code Playgroud)
打印出来:
System.Delegate CreateDelegate(System.Type, System.Object, System.Reflection.Met
hodInfo)
我需要编写一堆采用1..N泛型类型参数的方法,例如:
int Foo<T1>();
int Foo<T1,T2>();
int Foo<T1,T2,T3>();
...
int Foo<T1,T2,T3...TN>();
Run Code Online (Sandbox Code Playgroud)
在内部,Foo()我想为每种类型做一些事情,例如
int Foo<T1,T2,T3>() {
this.data = new byte[3]; // allocate 1 array slot per type
}
Run Code Online (Sandbox Code Playgroud)
有没有办法参数化这个,以便我不编辑每个变体Foo(),类似于:
int Foo<T1,T2,T3>() {
this.data = new byte[_NUMBER_OF_GENERIC_PARAMETERS];
}
Run Code Online (Sandbox Code Playgroud)
理想情况下,我也希望能够获得一个数组或类型的集合:
int Foo<T1,T2,T3>() {
this.data = new byte[_NUMBER_OF_GENERIC_PARAMETERS];
// can do this
Type [] types = new Type[] { T1, T2, T3 };
// but would rather do this
Type [] types = _ARRAY_OR_COLLECTION_OF_THE_GENERIC_PARAMETERS;
}
Run Code Online (Sandbox Code Playgroud) 我的目标是在我的方法中添加一些#if DEBUG但是我不想编辑我复制的代码并粘贴到每个方法中.
是否有像这样的通用代码:
void DoSomething()
{
#if Debug
Log("Now In " + MethodName);
#endif
}
Run Code Online (Sandbox Code Playgroud)
在哪里填充MethodName等于DoSomething,或者哪个方法称为日志?
这就是我所说的:
Public Shared Sub Test1()
Test2()
End Sub
Public Shared Sub Test2()
MsgBox(Test2.LastMethod) ' Test1
End Sub
Run Code Online (Sandbox Code Playgroud)
我想如果这是可能的,System.Reflection会被利用吗?
有什么想法吗?
以下是我编写的一小段代码,用于演示此问题的基础知识.
private async void Form1_Load( object sender, EventArgs e ) {
var result = await TestAsyncMethodName();
}
private async Task<string> TestAsyncMethodName() {
string name = "Method: " + System.Reflection.MethodBase.GetCurrentMethod().Name;
int x = 0;
foreach ( StackFrame sf in (new StackTrace()).GetFrames()) {
x++;
name = name + "\nStack Frame [" + x + "] : " + sf.GetMethod().Name;
}
await Task.Run( () => { name = name + "\nAnonymous Method: " + System.Reflection.MethodBase.GetCurrentMethod().Name; } );
return name;
}
Run Code Online (Sandbox Code Playgroud)
Method: …Run Code Online (Sandbox Code Playgroud)