I tried several things and I found something that works well for my needs. I have a Rule<T> inheritance from a base abstract class of rules using the generic IsBroken method:
abstract class Rule { string propertyName; Func<object, bool> objectRule; bool IsBroken<T>(T value) { Rule<T> rule = this as Rule<T>; if (rule == null) return objectRule(value); return rule.IsBroken(value); } }
As you can see, I am trying to convert a base class to its generic instance using the type parameter in the IsBroken method.
In addition, when creating an instance of Rule<T> I send Func<object, bool> to my constructor, protected by the base class:
public Rule(string propertyName, Func<T, bool> ruleLambda) : base(propertyName, ConvertToObjectFunc(ruleLambda)) { }
Using the conversion method, it looks like this:
static Func<object, bool> ConvertToObjectFunc(Func<T, bool> func) { return new Func<object, bool>(o => func((T)o)); }
However, if it cannot distinguish o from type T, it will work. So I wrote this ... thing:
static Func<object, bool> ConvertToObjectFunc(Func<T, bool> func) { return new Func<object, bool> ( o => { try { T obj = (T)o; return func(obj); } catch { return true; }
This is pretty ugly, but it works. Hope this helps someone else.