reflection - Is this possible in C#? -


I have an extension method to test, so I can:

  Var steve = new zombie (); Steve Mood Shale ("I'm hungry for the brain!");  

Extension Method:

  Public Static Zero ShouldBe & lt; T & gt; (This T is actual, expected) {Assert.That (actual, is.EqualTo (expected)); }  

It shows:

  Expected: "I'm hungry for the brain!" But it was: "I want to reshuffle randomly"  

Can anyone hack to get the name of the property "brain-processed" within my extension method? Bonus points will be example variables and type zombie.

Update:

New ShouldBe:

  Public Static Zero ShouldBe & lt; T & gt; (This t real, expected t) {var frame = new stacktrace (true) .GetFrame (1); Var filename = frame.gatefilename (); Var lineNumber = frame.GetFileLineNumber () - 1; Var code = file. Reedlines (filename). Elementat (Lincoln) Trim (). Hung (';'); Var codeMessage = new Regex (@ "(^. *) (\. \ S * ShouldBe \ s * \ () ([^ \)] +) \)"). Change (code, @ "should be $ 1 $ 3"); Var actualMessage = Actual. ToString (); If (actual string is) actualMessage = "\" "+ actual +" \ ""; Var message = string.Format (@ "{0} was but {1}", code message, actual message); Reveal it. That (real, is. Aqual (expected), message); }  

and prints it:

  Steve. Should be "I'm hungry for the brain!" But "I wanted to reshuffle randomly"  

Thanks everyone, special Matt Dotsan, this amazing BTW silky troll does not feed people.

You can get the code if it is debug build by using some of the diagnostic classes to see it That this unit is for testing, DEBUG is probably justified.

  Public Static Zero ShouldBe & lt; T & gt; (This t is actual, t required)  

{

var frame = new stacktrace (true) .GetFrame (1); Var filename = frame.gatefilename (); Var lineNumber = frame.GetFileLineNumber () - 1; String code = file Readline (filename). Elementat (Lincoln) Trim (); Debug Asset (Actual. Ecloss (expected), code);

}

For your example, code = "steve.brainsConsumed.ShouldBe (0);"

Obviously you should check some error for this code, and possibly you can do it fast by not reading all the rows in the file.


Comments

Popular posts from this blog

c# - How to capture HTTP packet with SharpPcap -

php - Multiple Select with Explode: only returns the word "Array" -

php - jQuery AJAX Post not working -