@@ -48,13 +48,13 @@ public static void DeepCloneRefProperties(IDeepCloneable input, IDeepCloneable o
48
48
{
49
49
IList newList ;
50
50
if ( propertyInfo . PropertyType . IsGenericType
51
- && ( propertyInfo . PropertyType . GetGenericTypeDefinition ( ) == typeof ( IEnumerable < > )
52
- || propertyInfo . PropertyType . GetGenericTypeDefinition ( ) == typeof ( ICollection < > )
51
+ && ( propertyInfo . PropertyType . GetGenericTypeDefinition ( ) == typeof ( IEnumerable < > )
52
+ || propertyInfo . PropertyType . GetGenericTypeDefinition ( ) == typeof ( ICollection < > )
53
53
|| propertyInfo . PropertyType . GetGenericTypeDefinition ( ) == typeof ( IList < > ) ) )
54
54
{
55
55
//if it is a IEnumerable<>, IList<T> or ICollection<> we'll use a List<>
56
- var genericType = typeof ( List < > ) . MakeGenericType ( propertyInfo . PropertyType . GetGenericArguments ( ) ) ;
57
- newList = ( IList ) Activator . CreateInstance ( genericType ) ;
56
+ var genericType = typeof ( List < > ) . MakeGenericType ( propertyInfo . PropertyType . GetGenericArguments ( ) ) ;
57
+ newList = ( IList ) Activator . CreateInstance ( genericType ) ;
58
58
}
59
59
else if ( propertyInfo . PropertyType . IsArray
60
60
|| ( propertyInfo . PropertyType . IsInterface && propertyInfo . PropertyType . IsGenericType == false ) )
@@ -86,22 +86,37 @@ public static void DeepCloneRefProperties(IDeepCloneable input, IDeepCloneable o
86
86
var enumerable = ( IEnumerable ) propertyInfo . GetValue ( input , null ) ;
87
87
if ( enumerable == null ) continue ;
88
88
89
- var isDeepClonableItems = false ;
89
+ var isUsableType = true ;
90
+
91
+ //now clone each item
90
92
foreach ( var o in enumerable )
91
93
{
94
+ //first check if the item is deep cloneable and copy that way
92
95
var dc = o as IDeepCloneable ;
93
96
if ( dc != null )
94
97
{
95
- isDeepClonableItems = true ;
96
98
newList . Add ( dc . DeepClone ( ) ) ;
97
99
}
98
- else if ( isDeepClonableItems )
100
+ else if ( o is string || o . GetType ( ) . IsValueType )
101
+ {
102
+ //check if the item is a value type or a string, then we can just use it
103
+ newList . Add ( o ) ;
104
+ }
105
+ else
99
106
{
100
- //if not all items are deep cloneable throw an exception
101
- throw new InvalidOperationException ( "Cannot deep clone items in a collection that are not all " + typeof ( IDeepCloneable ) ) ;
107
+ //this will occur if the item is not a string or value type or IDeepCloneable, in this case we cannot
108
+ // clone each element, we'll need to skip this property, people will have to manually clone this list
109
+ isUsableType = false ;
110
+ break ;
102
111
}
103
112
}
104
113
114
+ //if this was not usable, skip this property
115
+ if ( isUsableType == false )
116
+ {
117
+ continue ;
118
+ }
119
+
105
120
if ( propertyInfo . PropertyType . IsArray )
106
121
{
107
122
//need to convert to array
0 commit comments