If you're using .NET 4.7.1 or later, you could test whether it implements the
System.Runtime.CompilerServices.ITuple interface[
^]:
if (value is System.Runtime.CompilerServices.ITuple)
This will also detect the new
ValueTuple<>
types.
Otherwise, you'd need to get the generic type definition and compare it to the eight open
Tuple<>
generic types, since they don't share a common base class.
private static readonly HashSet<Type> TupleTypes = new HashSet<Type>
{
typeof(Tuple<>),
typeof(Tuple<,>),
typeof(Tuple<,,>),
typeof(Tuple<,,,>),
typeof(Tuple<,,,,>),
typeof(Tuple<,,,,,>),
typeof(Tuple<,,,,,,>),
typeof(Tuple<,,,,,,,>),
};
public static bool IsTuple(Type type) => type != null
&& type.IsGenericType
&& TupleTypes.Contains(type.GetGenericTypeDefinition());
public static bool IsTuple(object value) => value != null
&& IsTuple(value.GetType());
This one
won't include the
ValueTuple<>
types unless you add them to the list.