@@ -676,9 +676,9 @@ public static Dictionary<string, object> ToObjectDictionary(this object obj)
676
676
if ( obj is IDictionary < string , object > interfaceDict )
677
677
return new Dictionary < string , object > ( interfaceDict ) ;
678
678
679
+ var to = new Dictionary < string , object > ( ) ;
679
680
if ( obj is Dictionary < string , string > stringDict )
680
681
{
681
- var to = new Dictionary < string , object > ( ) ;
682
682
foreach ( var entry in stringDict )
683
683
{
684
684
to [ entry . Key ] = entry . Value ;
@@ -688,7 +688,6 @@ public static Dictionary<string, object> ToObjectDictionary(this object obj)
688
688
689
689
if ( obj is IDictionary d )
690
690
{
691
- var to = new Dictionary < string , object > ( ) ;
692
691
foreach ( var key in d . Keys )
693
692
{
694
693
to [ key . ToString ( ) ] = d [ key ] ;
@@ -698,56 +697,110 @@ public static Dictionary<string, object> ToObjectDictionary(this object obj)
698
697
699
698
if ( obj is NameValueCollection nvc )
700
699
{
701
- var to = new Dictionary < string , object > ( ) ;
702
700
for ( var i = 0 ; i < nvc . Count ; i ++ )
703
701
{
704
702
to [ nvc . GetKey ( i ) ] = nvc . Get ( i ) ;
705
703
}
706
704
return to ;
707
705
}
708
706
707
+ if ( obj is IEnumerable < KeyValuePair < string , object > > objKvps )
708
+ {
709
+ foreach ( var kvp in objKvps )
710
+ {
711
+ to [ kvp . Key ] = kvp . Value ;
712
+ }
713
+ return to ;
714
+ }
715
+ if ( obj is IEnumerable < KeyValuePair < string , string > > strKvps )
716
+ {
717
+ foreach ( var kvp in strKvps )
718
+ {
719
+ to [ kvp . Key ] = kvp . Value ;
720
+ }
721
+ return to ;
722
+ }
723
+
709
724
var type = obj . GetType ( ) ;
725
+ if ( type . GetKeyValuePairsTypes ( out var keyType , out var valueType , out var kvpType ) && obj is IEnumerable e )
726
+ {
727
+ var keyGetter = TypeProperties . Get ( kvpType ) . GetPublicGetter ( "Key" ) ;
728
+ var valueGetter = TypeProperties . Get ( kvpType ) . GetPublicGetter ( "Value" ) ;
729
+
730
+ foreach ( var entry in e )
731
+ {
732
+ var key = keyGetter ( entry ) ;
733
+ var value = valueGetter ( entry ) ;
734
+ to [ key . ConvertTo < string > ( ) ] = value ;
735
+ }
736
+ return to ;
737
+ }
738
+
739
+
740
+ if ( obj is KeyValuePair < string , object > objKvp )
741
+ return new Dictionary < string , object > { { objKvp . Key , objKvp . Value } } ;
742
+ if ( obj is KeyValuePair < string , string > strKvp )
743
+ return new Dictionary < string , object > { { strKvp . Key , strKvp . Value } } ;
744
+
745
+ if ( type . GetKeyValuePairTypes ( out _ , out var _ ) )
746
+ {
747
+ return new Dictionary < string , object > {
748
+ { TypeProperties . Get ( type ) . GetPublicGetter ( "Key" ) ( obj ) . ConvertTo < string > ( ) , TypeProperties . Get ( type ) . GetPublicGetter ( "Value" ) ( obj ) } ,
749
+ } ;
750
+ }
710
751
711
752
if ( ! toObjectMapCache . TryGetValue ( type , out var def ) )
712
753
toObjectMapCache [ type ] = def = CreateObjectDictionaryDefinition ( type ) ;
713
754
714
- var dict = new Dictionary < string , object > ( ) ;
715
-
716
755
foreach ( var fieldDef in def . Fields )
717
756
{
718
- dict [ fieldDef . Name ] = fieldDef . GetValueFn ( obj ) ;
757
+ to [ fieldDef . Name ] = fieldDef . GetValueFn ( obj ) ;
719
758
}
720
759
721
- return dict ;
760
+ return to ;
722
761
}
723
762
724
- public static Type GetKeyValuePairTypeDef ( this Type dictType )
763
+ public static Type GetKeyValuePairsTypeDef ( this Type dictType )
725
764
{
726
765
//matches IDictionary<,>, IReadOnlyDictionary<,>, List<KeyValuePair<string, object>>
727
766
var genericDef = dictType . GetTypeWithGenericTypeDefinitionOf ( typeof ( IEnumerable < > ) ) ;
728
767
if ( genericDef == null )
729
768
return null ;
730
769
731
770
var genericEnumType = genericDef . GetGenericArguments ( ) [ 0 ] ;
732
- return genericEnumType . GetTypeWithGenericTypeDefinitionOf ( typeof ( KeyValuePair < , > ) ) ;
771
+ return GetKeyValuePairTypeDef ( genericEnumType ) ;
733
772
}
773
+
774
+ public static Type GetKeyValuePairTypeDef ( this Type genericEnumType ) => genericEnumType . GetTypeWithGenericTypeDefinitionOf ( typeof ( KeyValuePair < , > ) ) ;
775
+
776
+ public static bool GetKeyValuePairsTypes ( this Type dictType , out Type keyType , out Type valueType ) =>
777
+ dictType . GetKeyValuePairsTypes ( out keyType , out valueType , out _ ) ;
734
778
735
- public static bool GetKeyValuePairsTypes ( this Type dictType , out Type keyType , out Type valueType )
779
+ public static bool GetKeyValuePairsTypes ( this Type dictType , out Type keyType , out Type valueType , out Type kvpType )
736
780
{
737
781
//matches IDictionary<,>, IReadOnlyDictionary<,>, List<KeyValuePair<string, object>>
738
782
var genericDef = dictType . GetTypeWithGenericTypeDefinitionOf ( typeof ( IEnumerable < > ) ) ;
739
783
if ( genericDef != null )
740
784
{
741
- var genericEnumType = genericDef . GetGenericArguments ( ) [ 0 ] ;
742
- var genericKvps = genericEnumType . GetTypeWithGenericTypeDefinitionOf ( typeof ( KeyValuePair < , > ) ) ;
743
- if ( genericKvps != null )
744
- {
745
- var genericArgs = genericEnumType . GetGenericArguments ( ) ;
746
- keyType = genericArgs [ 0 ] ;
747
- valueType = genericArgs [ 1 ] ;
785
+ kvpType = genericDef . GetGenericArguments ( ) [ 0 ] ;
786
+ if ( GetKeyValuePairTypes ( kvpType , out keyType , out valueType ) )
748
787
return true ;
749
- }
750
788
}
789
+ kvpType = keyType = valueType = null ;
790
+ return false ;
791
+ }
792
+
793
+ private static bool GetKeyValuePairTypes ( this Type kvpType , out Type keyType , out Type valueType )
794
+ {
795
+ var genericKvps = kvpType . GetTypeWithGenericTypeDefinitionOf ( typeof ( KeyValuePair < , > ) ) ;
796
+ if ( genericKvps != null )
797
+ {
798
+ var genericArgs = kvpType . GetGenericArguments ( ) ;
799
+ keyType = genericArgs [ 0 ] ;
800
+ valueType = genericArgs [ 1 ] ;
801
+ return true ;
802
+ }
803
+
751
804
keyType = valueType = null ;
752
805
return false ;
753
806
}
0 commit comments