簡體   English   中英

如何獲取在傳遞參數的lambda中使用的方法名稱

[英]How to get the method name used in lambda that was passed a parameter

AssertManager.Record中的參數處於活動狀態,但是我需要在lambda動作中的值即是asserts.So,因此我需要獲取我使用的哪種類型的斷言,它是從一個類傳遞到另一類的。 我出於某些原因使用了lambda表達式,因此無法對其進行編輯。 我只需要一個字符串類型,它可以說明我所做的任何斷言類型。 在我的示例中,它應該向控制台輸出“ Assert.True”或“ Assert.Equal”。

這是我使用的示例代碼:

public class ClassTest
{
    AssertManager = new AssertManager();

    [Fact]
    public void sampleTestAssert()
    {

      AssertManager.Record(() => Assert.True(true));
      AssertManager.Record(() => Assert.Equal("Dog","Dog"));


    }
}

public class AssertManager
{ 
    public void Record(Action testMethod)
    { 
     //is it possible to use testMethod to get the Assert inside the lambda in the 
     //Output what assert i did (ex. Assert.True, Assert.Equal )
    }
}

如果您有解決方案,請告訴我。 謝謝。

您需要使用Expression<Action>而不是Action然后才能反映出lambda ...

請參閱http://msdn.microsoft.com/en-us/library/bb397951.aspx

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM