@@ -658,32 +658,74 @@ public void SetValue(object instance, object value)
658
658
}
659
659
}
660
660
661
+ private static Dictionary < string , object > ConvertToDictionary < T > (
662
+ IEnumerable < KeyValuePair < string , T > > collection ,
663
+ Func < string , object , object > mapper = null )
664
+ {
665
+ if ( mapper != null )
666
+ {
667
+ return mapper . MapToDictionary ( collection ) ;
668
+ }
669
+
670
+ var to = new Dictionary < string , object > ( ) ;
671
+ foreach ( var entry in collection )
672
+ {
673
+ string key = entry . Key ;
674
+ object value = entry . Value ;
675
+ to [ key ] = value ;
676
+ }
677
+ return to ;
678
+ }
679
+
680
+ private static Dictionary < string , object > MapToDictionary < T > (
681
+ this Func < string , object , object > mapper ,
682
+ IEnumerable < KeyValuePair < string , T > > collection )
683
+ {
684
+ return collection . ToDictionary (
685
+ pair => pair . Key ,
686
+ pair => mapper ( pair . Key , pair . Value ) ) ;
687
+ }
688
+
661
689
public static Dictionary < string , object > ToObjectDictionary ( this object obj )
690
+ {
691
+ return ToObjectDictionary ( obj , null ) ;
692
+ }
693
+
694
+ public static Dictionary < string , object > ToObjectDictionary (
695
+ this object obj ,
696
+ Func < string , object , object > mapper )
662
697
{
663
698
if ( obj == null )
664
699
return null ;
665
700
666
701
if ( obj is Dictionary < string , object > alreadyDict )
702
+ {
703
+ if ( mapper != null )
704
+ return mapper . MapToDictionary ( alreadyDict ) ;
667
705
return alreadyDict ;
706
+ }
668
707
669
708
if ( obj is IDictionary < string , object > interfaceDict )
709
+ {
710
+ if ( mapper != null )
711
+ return mapper . MapToDictionary ( interfaceDict ) ;
670
712
return new Dictionary < string , object > ( interfaceDict ) ;
713
+ }
671
714
672
715
var to = new Dictionary < string , object > ( ) ;
673
716
if ( obj is Dictionary < string , string > stringDict )
674
717
{
675
- foreach ( var entry in stringDict )
676
- {
677
- to [ entry . Key ] = entry . Value ;
678
- }
679
- return to ;
718
+ return ConvertToDictionary ( stringDict , mapper ) ;
680
719
}
681
720
682
721
if ( obj is IDictionary d )
683
722
{
684
723
foreach ( var key in d . Keys )
685
724
{
686
- to [ key . ToString ( ) ] = d [ key ] ;
725
+ string k = key . ToString ( ) ;
726
+ object v = d [ key ] ;
727
+ v = mapper ? . Invoke ( k , v ) ?? v ;
728
+ to [ k ] = v ;
687
729
}
688
730
return to ;
689
731
}
@@ -692,26 +734,21 @@ public static Dictionary<string, object> ToObjectDictionary(this object obj)
692
734
{
693
735
for ( var i = 0 ; i < nvc . Count ; i ++ )
694
736
{
695
- to [ nvc . GetKey ( i ) ] = nvc . Get ( i ) ;
737
+ string k = nvc . GetKey ( i ) ;
738
+ object v = nvc . Get ( i ) ;
739
+ v = mapper ? . Invoke ( k , v ) ?? v ;
740
+ to [ k ] = v ;
696
741
}
697
742
return to ;
698
743
}
699
744
700
745
if ( obj is IEnumerable < KeyValuePair < string , object > > objKvps )
701
746
{
702
- foreach ( var kvp in objKvps )
703
- {
704
- to [ kvp . Key ] = kvp . Value ;
705
- }
706
- return to ;
747
+ return ConvertToDictionary ( objKvps , mapper ) ;
707
748
}
708
749
if ( obj is IEnumerable < KeyValuePair < string , string > > strKvps )
709
750
{
710
- foreach ( var kvp in strKvps )
711
- {
712
- to [ kvp . Key ] = kvp . Value ;
713
- }
714
- return to ;
751
+ return ConvertToDictionary ( strKvps , mapper ) ;
715
752
}
716
753
717
754
var type = obj . GetType ( ) ;
@@ -724,22 +761,60 @@ public static Dictionary<string, object> ToObjectDictionary(this object obj)
724
761
{
725
762
var key = keyGetter ( entry ) ;
726
763
var value = valueGetter ( entry ) ;
727
- to [ key . ConvertTo < string > ( ) ] = value ;
764
+ string k = key . ConvertTo < string > ( ) ;
765
+ value = mapper ? . Invoke ( k , value ) ?? value ;
766
+ to [ k ] = value ;
728
767
}
729
768
return to ;
730
769
}
731
-
770
+
732
771
733
772
if ( obj is KeyValuePair < string , object > objKvp )
734
- return new Dictionary < string , object > { { nameof ( objKvp . Key ) , objKvp . Key } , { nameof ( objKvp . Value ) , objKvp . Value } } ;
773
+ {
774
+ string kk = nameof ( objKvp . Key ) ;
775
+ object kv = objKvp . Key ;
776
+ kv = mapper ? . Invoke ( kk , kv ) ?? kv ;
777
+
778
+ string vk = nameof ( objKvp . Value ) ;
779
+ object vv = objKvp . Value ;
780
+ vv = mapper ? . Invoke ( vk , vv ) ?? vv ;
781
+
782
+ return new Dictionary < string , object >
783
+ {
784
+ [ kk ] = kv ,
785
+ [ vk ] = vv
786
+ } ;
787
+ }
735
788
if ( obj is KeyValuePair < string , string > strKvp )
736
- return new Dictionary < string , object > { { nameof ( strKvp . Key ) , strKvp . Key } , { nameof ( strKvp . Value ) , strKvp . Value } } ;
737
-
789
+ {
790
+ string kk = nameof ( objKvp . Key ) ;
791
+ object kv = strKvp . Key ;
792
+ kv = mapper ? . Invoke ( kk , kv ) ?? kv ;
793
+
794
+ string vk = nameof ( strKvp . Value ) ;
795
+ object vv = strKvp . Value ;
796
+ vv = mapper ? . Invoke ( vk , vv ) ?? vv ;
797
+
798
+ return new Dictionary < string , object >
799
+ {
800
+ [ kk ] = kv ,
801
+ [ vk ] = vv
802
+ } ;
803
+ }
738
804
if ( type . GetKeyValuePairTypes ( out _ , out var _ ) )
739
805
{
740
- return new Dictionary < string , object > {
741
- { "Key" , TypeProperties . Get ( type ) . GetPublicGetter ( "Key" ) ( obj ) . ConvertTo < string > ( ) } ,
742
- { "Value" , TypeProperties . Get ( type ) . GetPublicGetter ( "Value" ) ( obj ) } ,
806
+ string kk = "Key" ;
807
+ object kv = TypeProperties . Get ( type ) . GetPublicGetter ( "Key" ) ( obj ) . ConvertTo < string > ( ) ;
808
+ kv = mapper ? . Invoke ( kk , kv ) ?? kv ;
809
+
810
+ string vk = "Value" ;
811
+ object vv = TypeProperties . Get ( type ) . GetPublicGetter ( "Value" ) ( obj ) ;
812
+ vv = mapper ? . Invoke ( vk , vv ) ?? vv ;
813
+
814
+ return new Dictionary < string , object >
815
+ {
816
+ [ kk ] = kv ,
817
+ [ vk ] = vv
743
818
} ;
744
819
}
745
820
@@ -748,7 +823,10 @@ public static Dictionary<string, object> ToObjectDictionary(this object obj)
748
823
749
824
foreach ( var fieldDef in def . Fields )
750
825
{
751
- to [ fieldDef . Name ] = fieldDef . GetValueFn ( obj ) ;
826
+ string k = fieldDef . Name ;
827
+ object v = fieldDef . GetValueFn ( obj ) ;
828
+ v = mapper ? . Invoke ( k , v ) ?? v ;
829
+ to [ k ] = v ;
752
830
}
753
831
754
832
return to ;
0 commit comments