I know the title is probably really hard to understand, it was hard to think of a proper title, but here’s the essence of what I want to do.
Basically I want to have a method like this:
void Validate(bool validation)
{
if (!validation)
{
throw new Exception();
}
}
And then I want to call it like:
try
{
Validate(1 > 2);
}
catch (Exception e)
{
// This is where I would output the error to the user
}
I want to get the 1 > 2 part as a string without defining it as one elsewhere, or evaluating a string to a bool, or using predicates, or using external methods. Ideally this would be done via reflection. I’ll also take suggestions on a better way to do what I want to do. Assume that the bool could be anything: 1 > 2, "cheese" != "ham", objectA == objectB, etc.
You can’t. Well, perhaps you happen to can (in Python, one could hack something like this together, I suppose, although it wouldn’t be pretty, wouldn’t work reliably and would require having the source code at hand), but generally:
Before you’re looking for some nasty nasty hack to emulate this, check if it isn’t easier to add a string literal during compilation.