I\'m looking for a way to write code that tests whether a value is boxed.
My preliminary investigations indicate that .NET goes out of its way to conceal the fact,
This approach is similar to Jared Par's answer. But I think !typeof(T).IsValueTypeis cleaner than enumerating all types which could contain a boxed value.
public static bool IsBoxed(T value)
{
return !typeof(T).IsValueType && (value != null) && value.GetType().IsValueType;
}
Unlike Jared's code this will handle the case where T is System.ValueType correctly.
Another subtle point is that value.GetType().IsValueType comes after !typeof(T).IsValueType since otherwise GetType() would create a temporary boxed copy of the value.