1- using GameFrameX . Log ;
1+ using GameFrameX . Utility . Log ;
22
3- namespace GameFrameX . Apps
3+ namespace GameFrameX . Apps ;
4+
5+ public class DictionaryRepresentationConvention : ConventionBase , IMemberMapConvention
46{
5- public class DictionaryRepresentationConvention : ConventionBase , IMemberMapConvention
6- {
7- private readonly DictionaryRepresentation _dictionaryRepresentation ;
7+ private readonly DictionaryRepresentation _dictionaryRepresentation ;
88
9- public DictionaryRepresentationConvention ( DictionaryRepresentation dictionaryRepresentation = DictionaryRepresentation . ArrayOfDocuments )
10- {
11- _dictionaryRepresentation = dictionaryRepresentation ;
12- }
9+ public DictionaryRepresentationConvention ( DictionaryRepresentation dictionaryRepresentation = DictionaryRepresentation . ArrayOfDocuments )
10+ {
11+ _dictionaryRepresentation = dictionaryRepresentation ;
12+ }
1313
14- public void Apply ( BsonMemberMap memberMap )
14+ public void Apply ( BsonMemberMap memberMap )
15+ {
16+ var serializer = memberMap . GetSerializer ( ) ;
17+ if ( serializer is IDictionaryRepresentationConfigurable dictionaryRepresentationConfigurable )
1518 {
16- var serializer = memberMap . GetSerializer ( ) ;
17- if ( serializer is IDictionaryRepresentationConfigurable dictionaryRepresentationConfigurable )
18- {
19- var reconfiguredSerializer = dictionaryRepresentationConfigurable . WithDictionaryRepresentation ( _dictionaryRepresentation ) ;
20- memberMap . SetSerializer ( reconfiguredSerializer ) ;
21- }
19+ var reconfiguredSerializer = dictionaryRepresentationConfigurable . WithDictionaryRepresentation ( _dictionaryRepresentation ) ;
20+ memberMap . SetSerializer ( reconfiguredSerializer ) ;
2221 }
2322 }
23+ }
2424
25- public class EmptyContainerSerializeMethodConvention : ConventionBase , IMemberMapConvention
25+ public class EmptyContainerSerializeMethodConvention : ConventionBase , IMemberMapConvention
26+ {
27+ public void Apply ( BsonMemberMap memberMap )
2628 {
27- public void Apply ( BsonMemberMap memberMap )
29+ if ( memberMap . MemberType . IsGenericType )
2830 {
29- if ( memberMap . MemberType . IsGenericType )
31+ var genType = memberMap . MemberType . GetGenericTypeDefinition ( ) ;
32+ if ( genType == typeof ( List < > ) )
3033 {
31- var genType = memberMap . MemberType . GetGenericTypeDefinition ( ) ;
32- if ( genType == typeof ( List < > ) )
34+ memberMap . SetShouldSerializeMethod ( o =>
3335 {
34- memberMap . SetShouldSerializeMethod ( o =>
36+ var value = memberMap . Getter ( o ) ;
37+ if ( value is IList list )
3538 {
36- var value = memberMap . Getter ( o ) ;
37- if ( value is IList list )
38- {
39- return list != null && list . Count > 0 ;
40- }
39+ return list != null && list . Count > 0 ;
40+ }
4141
42- return true ;
43- } ) ;
44- }
42+ return true ;
43+ } ) ;
44+ }
4545
46- else if ( genType == typeof ( ConcurrentDictionary < , > ) || genType == typeof ( Dictionary < , > ) )
46+ else if ( genType == typeof ( ConcurrentDictionary < , > ) || genType == typeof ( Dictionary < , > ) )
47+ {
48+ memberMap . SetShouldSerializeMethod ( o =>
4749 {
48- memberMap . SetShouldSerializeMethod ( o =>
50+ if ( o != null )
4951 {
50- if ( o != null )
52+ var value = memberMap . Getter ( o ) ;
53+ if ( value != null )
5154 {
52- var value = memberMap . Getter ( o ) ;
53- if ( value != null )
55+ var countProperty = value . GetType ( ) . GetProperty ( "Count" ) ;
56+ if ( countProperty != null )
5457 {
55- PropertyInfo countProperty = value . GetType ( ) . GetProperty ( "Count" ) ;
56- if ( countProperty != null )
57- {
58- int count = ( int ) countProperty . GetValue ( value , null ) ;
59- return count > 0 ;
60- }
58+ var count = ( int ) countProperty . GetValue ( value , null ) ;
59+ return count > 0 ;
6160 }
6261 }
62+ }
6363
64- return true ;
65- } ) ;
66- }
64+ return true ;
65+ } ) ;
6766 }
6867 }
6968 }
69+ }
7070
71- public static class BsonClassMapHelper
71+ public static class BsonClassMapHelper
72+ {
73+ public static void SetConvention ( )
7274 {
73- public static void SetConvention ( )
74- {
75- ConventionRegistry . Register ( nameof ( DictionaryRepresentationConvention ) ,
76- new ConventionPack { new DictionaryRepresentationConvention ( DictionaryRepresentation . ArrayOfDocuments ) } , _ => true ) ;
75+ ConventionRegistry . Register ( nameof ( DictionaryRepresentationConvention ) ,
76+ new ConventionPack { new DictionaryRepresentationConvention ( ) , } , _ => true ) ;
7777
78- ConventionRegistry . Register ( nameof ( EmptyContainerSerializeMethodConvention ) ,
79- new ConventionPack { new EmptyContainerSerializeMethodConvention ( ) } , _ => true ) ;
80- }
78+ ConventionRegistry . Register ( nameof ( EmptyContainerSerializeMethodConvention ) ,
79+ new ConventionPack { new EmptyContainerSerializeMethodConvention ( ) , } , _ => true ) ;
80+ }
8181
82- /// <summary>
83- /// 提前注册,简化多态类型处理
84- /// </summary>
85- /// <param name="assembly"></param>
86- public static void RegisterAllClass ( System . Reflection . Assembly assembly )
82+ /// <summary>
83+ /// 提前注册,简化多态类型处理
84+ /// </summary>
85+ /// <param name="assembly"></param>
86+ public static void RegisterAllClass ( Assembly assembly )
87+ {
88+ var types = assembly . GetTypes ( ) ;
89+ foreach ( var t in types )
8790 {
88- var types = assembly . GetTypes ( ) ;
89- foreach ( var t in types )
91+ try
9092 {
91- try
93+ if ( ! BsonClassMap . IsClassMapRegistered ( t ) )
9294 {
93- if ( ! BsonClassMap . IsClassMapRegistered ( t ) )
94- {
95- RegisterClass ( t ) ;
96- }
97- }
98- catch ( Exception e )
99- {
100- LogHelper . Error ( e ) ;
95+ RegisterClass ( t ) ;
10196 }
10297 }
98+ catch ( Exception e )
99+ {
100+ LogHelper . Error ( e ) ;
101+ }
103102 }
103+ }
104104
105- public static void RegisterClass ( Type t )
106- {
107- var bsonClassMap = new BsonClassMap ( t ) ;
108- bsonClassMap . AutoMap ( ) ;
109- bsonClassMap . SetIgnoreExtraElements ( true ) ;
110- bsonClassMap . SetIgnoreExtraElementsIsInherited ( true ) ;
111- BsonClassMap . RegisterClassMap ( bsonClassMap ) ;
112- }
105+ public static void RegisterClass ( Type t )
106+ {
107+ var bsonClassMap = new BsonClassMap ( t ) ;
108+ bsonClassMap . AutoMap ( ) ;
109+ bsonClassMap . SetIgnoreExtraElements ( true ) ;
110+ bsonClassMap . SetIgnoreExtraElementsIsInherited ( true ) ;
111+ BsonClassMap . RegisterClassMap ( bsonClassMap ) ;
113112 }
114- }
113+ }
0 commit comments