Fundamentally your general approach isn't possible. The reason you're able to take all non-value types and treat them as a Func<object>
is by relying on contravariance (Func<T>
is contravariant with respect to T
). As per the language specs, contravariance does not support value types.
Of course, the problem is easier if you just don't rely on using that approach.
If you just want to get the value use the PropertyInfo.GetValue
method:
public object GetPropValue(string name)
{
return GetType().GetProperty(name).GetValue(this);
}
If you want to return a Func<object>
which will fetch the value whenever it's called, just create a lambda around that reflection call:
public Func<object> GetPropValue2(string name)
{
return () => GetType().GetProperty(name).GetValue(this);
}