Add support to tuples in inline values (#4771)#4772
Conversation
| if (genericType.Assembly == typeof(ValueTuple).Assembly && | ||
| genericType.Namespace == typeof(ValueTuple).Namespace && | ||
| genericType.Name.StartsWith("ValueTuple`", StringComparison.Ordinal)) | ||
| { | ||
| typeArgs = type.GetGenericArguments(); | ||
| return true; | ||
| } |
There was a problem hiding this comment.
There should probably be tests of tuples with more than seven elements, since the C# type (T1, T2, T3, T4, T5, T6, T7, T8) maps to the type ValueTuple<T1, T2, T3, T4, T4, T5, T6, T7, ValueTuple<T8>>.
(System.Tuple and System.ValueTuple require the TRest parameter to be a tuple, and this mapping is recursive to allow any number of elements.)
|
@manfred-brands @stevenaw Can you have a look at this? It's a bit of code here. Are we good with this? |
manfred-brands
left a comment
There was a problem hiding this comment.
@MaxKot Thanks for your contribution.
It looks good.
I have made one possible suggestion to replace the string comparisons.
| Type genericType = type.GetGenericTypeDefinition(); | ||
| if (genericType.Assembly == typeof(Tuple).Assembly && | ||
| genericType.Namespace == typeof(Tuple).Namespace && | ||
| genericType.Name.StartsWith("Tuple`", StringComparison.Ordinal)) |
There was a problem hiding this comment.
Maybe we could have a static field:
private static Type[] TupleTypes = [typeof(Tuple), typeof(Tuple<,>), typeof(Tuple<,,>), ...]Then use:
typeargs = type.GetGenericArguments();
if (typeargs.Length <= TupleTypes.Length && genericType == TupleTypes[typeargs.Length])This would eliminate 3 string comparisons (or 6 if counting the ValueTuple).
Probably try it out in your benchmark if it makes any difference.
Fixes #4771
Please note that merging this also requires an update to
NUnit1031diagnostic in the analyzers repository.