Apparently ITuple
is internal, disabling a solution such as typeof(ITuple).IsAssignableFrom(type)
. By alternative, what is the most effective way to determine Tuple<>
till Tuple<,,,,,,,>
? A solution without type name comparison is preferable.
Method #1: Using type() This inbuilt function can be used as shorthand to perform this task. It checks for the type of variable and can be employed to check tuple as well.
A tuple can have any number of items and they may be of different types (integer, float, list, string, etc.). A tuple can also be created without using parentheses. This is known as tuple packing.
Method #1: Using any() any function is used to perform this task. It just tests one by one if the element is present as the tuple element. If the element is present, true is returned else false is returned.
A tuple can have any number of items and they may be of different types (integer, float, list, string, etc.). A tuple can also be created without using parentheses.
Try this:
public static bool IsTupleType(Type type, bool checkBaseTypes = false)
{
if (type == null)
throw new ArgumentNullException(nameof(type));
if (type == typeof(Tuple))
return true;
while (type != null)
{
if (type.IsGenericType)
{
var genType = type.GetGenericTypeDefinition();
if (genType == typeof(Tuple<>)
|| genType == typeof(Tuple<,>)
|| genType == typeof(Tuple<,,>)
|| genType == typeof(Tuple<,,,>)
|| genType == typeof(Tuple<,,,,>)
|| genType == typeof(Tuple<,,,,,>)
|| genType == typeof(Tuple<,,,,,,>)
|| genType == typeof(Tuple<,,,,,,,>)
|| genType == typeof(Tuple<,,,,,,,>))
return true;
}
if (!checkBaseTypes)
break;
type = type.BaseType;
}
return false;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With