相关疑难解决方法(0)

获取dotnet核心中当前正在执行的方法的名称

我想在dotnet核心应用程序中获取当前正在执行的方法的名称.

有很多关于如何使用常规c#执行此操作的示例,例如

然而,这两种方法的api似乎还没有核心(参见https://github.com/dotnet/corefx/issues/1420)

有没有其他方法可以在.net核心中获取执行方法名称?

c# .net-core

13
推荐指数
1
解决办法
6787
查看次数

获取当前方法名称

我想要调用当前Handler的名称.

MethodInfo.GetCurrentMethod().Name或者MethodBase.GetCurrentMethod().Name在调试模式下正常工作.

但是一旦我混淆了(使用confuserEx)我的项目,2个函数就会返回"System.Reflection.MethodBase ()".

我注意到我可以使用以下行获取我的函数名称: ((RoutedEventHandler)this.MyMethodName).GetMethodInfo().Name

它返回"MyMethodName"哪个是预期结果.

但它根本不是通用的.当我不知道当前方法的名称时,我想要一段代码.

c# methods

11
推荐指数
1
解决办法
1万
查看次数

将方法的名称作为参数传递

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,我想有一些方法将方法的名称作为参数传递,而无需手动编辑代码.

那可能吗?

c# methods parameter-passing

8
推荐指数
2
解决办法
2348
查看次数

从表达式树中提取方法名称?

我正在尝试实现以下模式函数:

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)

.net c# linq reflection expression-trees

7
推荐指数
1
解决办法
2127
查看次数

C#泛型:任何方式将泛型参数类型称为集合?

我需要编写一堆采用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)

.net c# generics

7
推荐指数
1
解决办法
723
查看次数

我在哪种方法?

我的目标是在我的方法中添加一些#if DEBUG但是我不想编辑我复制的代码并粘贴到每个方法中.

是否有像这样的通用代码:

void DoSomething()
            {
#if Debug
            Log("Now In " + MethodName);
#endif
            }
Run Code Online (Sandbox Code Playgroud)

在哪里填充MethodName等于DoSomething,或者哪个方法称为日志?

c# debugging methods winforms

2
推荐指数
2
解决办法
129
查看次数

是否有可能获得调用另一种方法的名称方法?

这就是我所说的:

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会被利用吗?

有什么想法吗?

vb.net methods

1
推荐指数
1
解决办法
198
查看次数

使用反射在异步方法中获取方法名称不会返回预期结果

以下是我编写的一小段代码,用于演示此问题的基础知识.

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)

c# reflection asynchronous

-1
推荐指数
1
解决办法
2894
查看次数