I’m trying to write a validation method. Eg: for double it looks like this:
protected bool ValidateLoopAttributes(string i_value, double i_threshold)
{
double result;
if (!(double.TryParse(i_value, out result) && result >= i_threshold))
{
return false;
}
return true;
}
Is it possible to write this as:
protected bool ValidateLoopAttributes<T>(string i_value, T i_threshold)
and then use something like
T.GetType().TryParse() // how can i use here the type's methods??
Is using a switch/if statement the only way to do this? Eg:
If (T.GetType() is int)
Int32.TryParse(i_threshold)
Is there a more elegant way?
Try this:
My answer uses Marc Gravell’s answer taken from here.
With this you can do
If you find it useful you can also use an extension method
which leads you to use