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

98 views Asked by At

The parameter in the AssertManager.Record is in action but i need the value inside the lambda action which is the asserts.So i need to get what type of assertion i used which i passed from one class to another. I used lambda expression for certain reason so i cannot edit that. I just need a string type which says whatever type of assertion i did. in my example it should output a "Assert.True" or "Assert.Equal" to the console.

Heres the sample code i use:

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 )
    }
}

please let me know if you have a solution for this. Thank you.

1

There are 1 answers

0
Rico Suter On BEST ANSWER

You need to use Expression<Action> instead of Action then you can reflect the lambda...

See http://msdn.microsoft.com/en-us/library/bb397951.aspx