@@ -1147,5 +1147,101 @@ public void Can_convert_between_structs_with_explicit_casts()
1147
1147
d = c . ConvertTo < D > ( ) ;
1148
1148
Assert . That ( d . Id , Is . EqualTo ( c . Id ) ) ;
1149
1149
}
1150
+
1151
+ [ Test ]
1152
+ public void Can_Convert_from_ObjectDictionary_Containing_Another_Object_Dictionary ( )
1153
+ {
1154
+ var map = new Dictionary < string , object >
1155
+ {
1156
+ { "name" , "Foo" } ,
1157
+ { "otherNames" , new List < object >
1158
+ {
1159
+ new Dictionary < string , object > { { "name" , "Fu" } } ,
1160
+ new Dictionary < string , object > { { "name" , "Fuey" } }
1161
+ }
1162
+ }
1163
+ } ;
1164
+
1165
+ var fromDict = map . ConvertTo < AutoMappingObjectDictionaryTests . PersonWithIdentities > ( ) ;
1166
+
1167
+ Assert . That ( fromDict . Name , Is . EqualTo ( "Foo" ) ) ;
1168
+ Assert . That ( fromDict . OtherNames . Count , Is . EqualTo ( 2 ) ) ;
1169
+ Assert . That ( fromDict . OtherNames . First ( ) . Name , Is . EqualTo ( "Fu" ) ) ;
1170
+ Assert . That ( fromDict . OtherNames . Last ( ) . Name , Is . EqualTo ( "Fuey" ) ) ;
1171
+
1172
+ var toDict = fromDict . ConvertTo < Dictionary < string , object > > ( ) ;
1173
+ Assert . That ( toDict [ "Name" ] , Is . EqualTo ( "Foo" ) ) ;
1174
+ Assert . That ( toDict [ "OtherNames" ] , Is . EqualTo ( fromDict . OtherNames ) ) ;
1175
+
1176
+ var toObjDict = fromDict . ConvertTo < ObjectDictionary > ( ) ;
1177
+ Assert . That ( toObjDict [ "Name" ] , Is . EqualTo ( "Foo" ) ) ;
1178
+ Assert . That ( toObjDict [ "OtherNames" ] , Is . EqualTo ( fromDict . OtherNames ) ) ;
1179
+ }
1180
+
1181
+ class Person
1182
+ {
1183
+ public int Id { get ; set ; }
1184
+ public string Name { get ; set ; }
1185
+
1186
+ protected bool Equals ( Person other ) => Id == other . Id && string . Equals ( Name , other . Name ) ;
1187
+
1188
+ public override bool Equals ( object obj )
1189
+ {
1190
+ if ( ReferenceEquals ( null , obj ) ) return false ;
1191
+ if ( ReferenceEquals ( this , obj ) ) return true ;
1192
+ return obj . GetType ( ) == this . GetType ( ) && Equals ( ( Person ) obj ) ;
1193
+ }
1194
+
1195
+ public override int GetHashCode ( )
1196
+ {
1197
+ unchecked
1198
+ {
1199
+ return ( Id * 397 ) ^ ( Name != null ? Name . GetHashCode ( ) : 0 ) ;
1200
+ }
1201
+ }
1202
+ }
1203
+
1204
+ [ Test ]
1205
+ public void Can_convert_between_Poco_and_ObjectDictionary ( )
1206
+ {
1207
+ var person = new Person { Id = 1 , Name = "foo" } ;
1208
+
1209
+ Assert . That ( person . ConvertTo < Dictionary < string , object > > ( ) , Is . EqualTo ( new Dictionary < string , object > {
1210
+ { "Id" , 1 } ,
1211
+ { "Name" , "foo" } ,
1212
+ } ) ) ;
1213
+ Assert . That ( new Dictionary < string , object > {
1214
+ { "Id" , 1 } ,
1215
+ { "Name" , "foo" } ,
1216
+ } . ConvertTo < Person > ( ) , Is . EqualTo ( person ) ) ;
1217
+
1218
+ Assert . That ( person . ConvertTo < ObjectDictionary > ( ) , Is . EqualTo ( new ObjectDictionary {
1219
+ { "Id" , 1 } ,
1220
+ { "Name" , "foo" } ,
1221
+ } ) ) ;
1222
+ Assert . That ( new ObjectDictionary {
1223
+ { "Id" , 1 } ,
1224
+ { "Name" , "foo" } ,
1225
+ } . ConvertTo < Person > ( ) , Is . EqualTo ( person ) ) ;
1226
+
1227
+ Assert . That ( person . ConvertTo < Dictionary < string , string > > ( ) , Is . EqualTo ( new Dictionary < string , string > {
1228
+ { "Id" , "1" } ,
1229
+ { "Name" , "foo" } ,
1230
+ } ) ) ;
1231
+
1232
+ Assert . That ( new Dictionary < string , string > {
1233
+ { "Id" , "1" } ,
1234
+ { "Name" , "foo" } ,
1235
+ } . ConvertTo < Person > ( ) , Is . EqualTo ( person ) ) ;
1236
+
1237
+ Assert . That ( person . ConvertTo < StringDictionary > ( ) , Is . EqualTo ( new StringDictionary {
1238
+ { "Id" , "1" } ,
1239
+ { "Name" , "foo" } ,
1240
+ } ) ) ;
1241
+ Assert . That ( new StringDictionary {
1242
+ { "Id" , "1" } ,
1243
+ { "Name" , "foo" } ,
1244
+ } . ConvertTo < Person > ( ) , Is . EqualTo ( person ) ) ;
1245
+ }
1150
1246
}
1151
1247
}
0 commit comments