c# - Can I get a string representation of a condition passed as a bool into a function? -


i know title hard understand, hard think of proper title, here's essence of want do.

basically want have method this:

void validate(bool validation) {     if (!validation)     {         throw new exception();     } } 

and want call like:

try {     validate(1 > 2); } catch (exception e) {     // output error user } 

i want 1 > 2 part string without defining 1 elsewhere, or evaluating string bool, or using predicates, or using external methods. ideally done via reflection. i'll take suggestions on better way want do. assume bool anything: 1 > 2, "cheese" != "ham", objecta == objectb, etc.

you can't. well, perhaps happen can (in python, 1 hack together, suppose, although wouldn't pretty, wouldn't work reliably , require having source code @ hand), generally:

  • you don't have string repesentations of code @ runtime.
  • arguments (expressions) evaluated before function called.
  • the evaluation yields nothing lone bool doesn't remember slightest bit came from.

before you're looking nasty nasty hack emulate this, check if isn't easier add string literal during compilation.


Comments