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

Commit 3c7724e

Browse files
committed
Add support for converting between covariant collections
1 parent deecbc0 commit 3c7724e

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

src/ServiceStack.Text/TranslateListWithElements.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,15 @@ public static object TryTranslateCollections(Type fromPropertyType, Type toPrope
9696
fromValue, toPropertyType, varArgs.Args1[0]);
9797
}
9898

99+
var fromElType = fromPropertyType.GetCollectionType();
100+
var toElType = toPropertyType.GetCollectionType();
101+
102+
if (fromElType == null || toElType == null)
103+
return null;
104+
105+
if (fromElType == typeof(object) || toElType.IsAssignableFromType(fromElType))
106+
return TranslateToGenericICollectionCache(fromValue, toPropertyType, toElType);
107+
99108
return null;
100109
}
101110

@@ -176,21 +185,21 @@ public static object LateBoundTranslateToGenericICollection(
176185
if (toInstanceOfType.IsArray)
177186
{
178187
var result = TranslateToGenericICollection(
179-
(ICollection<T>)fromList, typeof(List<T>));
188+
(IEnumerable)fromList, typeof(List<T>));
180189
return result.ToArray();
181190
}
182191

183192
return TranslateToGenericICollection(
184-
(ICollection<T>)fromList, toInstanceOfType);
193+
(IEnumerable)fromList, toInstanceOfType);
185194
}
186195

187196
public static ICollection<T> TranslateToGenericICollection(
188-
ICollection<T> fromList, Type toInstanceOfType)
197+
IEnumerable fromList, Type toInstanceOfType)
189198
{
190199
var to = (ICollection<T>)CreateInstance(toInstanceOfType);
191200
foreach (var item in fromList)
192201
{
193-
to.Add(item);
202+
to.Add((T)item);
194203
}
195204
return to;
196205
}

tests/ServiceStack.Text.Tests/AutoMappingTests.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,30 @@ public void Does_convert_from_ValueType_to_strings()
598598

599599
Assert.That(new DateTime(2001, 01, 01).ConvertTo<string>(), Is.EqualTo("2001-01-01"));
600600
}
601+
602+
[Test]
603+
public void Can_convert_from_List_object()
604+
{
605+
var from = 3.Times(i => (object)new Car { Age = i, Name = "Name" + i });
606+
var to = (List<Car>)TranslateListWithElements.TryTranslateCollections(
607+
typeof(List<object>), typeof(List<Car>), from);
608+
609+
Assert.That(to.Count, Is.EqualTo(3));
610+
Assert.That(to[0].Age, Is.EqualTo(0));
611+
Assert.That(to[0].Name, Is.EqualTo("Name0"));
612+
}
613+
614+
[Test]
615+
public void Can_convert_from_List_SubType()
616+
{
617+
var from = 3.Times(i => new SubCar { Age = i, Name = "Name" + i });
618+
var to = (List<Car>)TranslateListWithElements.TryTranslateCollections(
619+
typeof(List<SubCar>), typeof(List<Car>), from);
620+
621+
Assert.That(to.Count, Is.EqualTo(3));
622+
Assert.That(to[0].Age, Is.EqualTo(0));
623+
Assert.That(to[0].Name, Is.EqualTo("Name0"));
624+
}
601625
}
602626

603627
public class Test

0 commit comments

Comments
 (0)