Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
5.00/5 (3 votes)
See more:
C#
if (val.GetType().FullName.StartsWith("System.Tuple`"))
	System.Diagnostics.Debugger.Break();


This works, but it's dodgy. I'm looking for something having to do with the generic type definition, like with typeof(Tuple<>) but it doesn't work.

I can't care how many arguments the tuple has. I just need to know if it is one.

What I have tried:

C#
if (val.GetType().FullName.StartsWith("System.Tuple`"))
	System.Diagnostics.Debugger.Break();


That works, but is dodgy. I want this to Break on typeof(Tuple<>) or similar.
Posted
Updated 23-Jul-19 6:51am

If you're using .NET 4.7.1 or later, you could test whether it implements the System.Runtime.CompilerServices.ITuple interface[^]:
C#
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.
C#
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.
 
Share this answer
 
Comments
honey the codewitch 23-Jul-19 12:52pm    
There's the magic I was hoping for! re: ITuple
Afzaal Ahmad Zeeshan 23-Jul-19 14:14pm    
5ed.
BillWoodruff 23-Jul-19 15:49pm    
+5
BillWoodruff 23-Jul-19 17:08pm    
The test 'foo is ValueType' appears valid for any ValueTuple instance with any number of parameters. Of course, I am using a later version FrameWork.

Can a Tuple<> exist: I know I can't declare one in VStudio, just as I can't declare a ValueTuple without at least one instance of a legal type,
honey the codewitch 23-Jul-19 17:11pm    
well yeah i think it is a value type in that it derives from ValueType instead of directly from object, but that's not really what i'm looking for as int also returns true there too.

you can't declare partial generics but you can call typeof on them so like typeof(IList<>) works but class foo : IList<> {} does not

i think the ValueTuple just uses ref internally (is a ref type) or something like that. i'd have to check
Since Tuple have a lot of definitions, each with a different number of generic arguments, I don't think there is a magic solution (except the one you are using) to get whether an instance is a tuple or not. Problem is typeof(Tuple<>) will only match Tuple<T1>, the singleton version.
 
Share this answer
 
Comments
honey the codewitch 23-Jul-19 12:54pm    
That's what I figured but Richard Demming below found the magic I was looking for. Requires .NET 4.71 but that's fine.
Afzaal Ahmad Zeeshan 23-Jul-19 14:15pm    
5ed.
This doesn't work?

if (val is Tuple)
 
Share this answer
 
Comments
OriginalGriff 23-Jul-19 12:31pm    
Posted as a solution:
"i'm pretty sure i tried that and it didn't work for me. My tuples i'm using as a test set have two arguments. That might have something to do with it. I also tried typeof(Tuple<>) on a lark. I can't do typeof(Tuple<,>) because i don't know how many arguments the tuples will have in advance."
codewitch honey crisis
honey the codewitch 23-Jul-19 12:32pm    
i'm pretty sure i tried that and it didn't work for me. My tuples i'm using as a test set have two arguments. That might have something to do with it. I also tried typeof(Tuple<>) on a lark. I can't do typeof(Tuple<,>) because i don't know how many arguments the tuples will have in advance. I'll try Tuple again just to be sure and amend my response if i'm wrong

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900