Thursday, 5 September 2013

Generic Method That Inverts Booleans

Generic Method That Inverts Booleans

I have a method with a generic parameter that filters values based on
runtime string values. It either lets the value through or returns the
default();
public T Filter<T>(string target, string actual, T value)
{
bool isMatch = !string.IsNullOrEmpty(target)
&& !string.IsNullOrEmpty(actual)
&& target.ToUpper() == actual.ToUpper();
return isMatch ? value : default(T);
}
This works fine for most everything except booleans. When a boolean false
comes in and should be filtered, it comes back out as false -- the
default() for boolean.
So what I want this to do when a bool is passed in is to return the
inverse of the boolean. How to get from T to bool and back into the return
type T?
Thanks!

No comments:

Post a Comment