Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 3b2a662

Browse files
committed
Add support for GetCollectionType on LINQ Select() expression
1 parent 67f62d1 commit 3b2a662

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

src/ServiceStack.Text/PlatformExtensions.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,8 @@ public static Delegate CreateDelegate(this MethodInfo methodInfo, Type delegateT
535535

536536
public static Type GetCollectionType(this Type type)
537537
{
538-
return type.GetElementType() ?? type.GetGenericArguments().FirstOrDefault();
538+
return type.GetElementType()
539+
?? type.GetGenericArguments().LastOrDefault(); //new[] { str }.Select(x => new Type()) => WhereSelectArrayIterator<string,Type>
539540
}
540541

541542
static Dictionary<string, Type> GenericTypeCache = new Dictionary<string, Type>();

tests/ServiceStack.Text.Tests/ReflectionExtensionTests.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,16 @@ public void Can_get_method_from_type()
120120
helloVoidBoolIntDelegate(testInstance, true, 5);
121121
Assert.That(testInstance.Result, Is.EqualTo("Hello True 5"));
122122
}
123+
124+
[Test]
125+
public void Does_GetCollectionType()
126+
{
127+
Assert.That(new[] { new TestModel() }.GetType().GetCollectionType(), Is.EqualTo(typeof(TestModel)));
128+
Assert.That(new[] { new TestModel() }.ToList().GetType().GetCollectionType(), Is.EqualTo(typeof(TestModel)));
129+
Assert.That(new[] { new TestModel() }.Select(x => x).GetType().GetCollectionType(), Is.EqualTo(typeof(TestModel)));
130+
Assert.That(new[] { "" }.Select(x => new TestModel()).GetType().GetCollectionType(), Is.EqualTo(typeof(TestModel)));
131+
}
132+
123133
}
124134

125135
public class GenericType<T> { }

0 commit comments

Comments
 (0)