@@ -83,15 +83,36 @@ protected DictionaryMessagePackSerializer( SerializationContext ownerContext, Po
8383 /// <returns>The count of the <paramref name="dictionary"/>.</returns>
8484 protected override int GetCount ( TDictionary dictionary )
8585 {
86- #if ( ! UNITY ) || AOT_CHECK
86+ #if ! NETSTANDARD2_0
8787 return dictionary . Count ;
88- #else
88+ #else // NETSTANDARD2_0
89+ if ( SerializerOptions . CanEmit )
90+ {
91+ return this . GetCountCore ( dictionary ) ;
92+ }
93+ else
94+ {
95+ return this . GetCountCoreAotSafe ( dictionary ) ;
96+ }
97+ #endif // !NETSTANDARD2_0
98+ }
99+
100+ #if NETSTANDARD2_0
101+
102+ private int GetCountCore ( TDictionary dictionary )
103+ {
104+ return dictionary . Count ;
105+ }
106+
107+ private int GetCountCoreAotSafe ( TDictionary dictionary )
108+ {
89109 // .constraind call for TDictionary.get_Count/TDictionary.GetEnumerator() causes AOT error.
90110 // So use cast and invoke as normal call (it might cause boxing, but most collection should be reference type).
91111 return ( dictionary as IDictionary < TKey , TValue > ) . Count ;
92- #endif // ( !UNITY ) || AOT_CHECK
93112 }
94113
114+ #endif // NETSTANDARD2_0
115+
95116 /// <summary>
96117 /// Adds the deserialized item to the collection on <typeparamref name="TDictionary"/> specific manner
97118 /// to implement <see cref="DictionaryMessagePackSerializerBase{TDictionary,TKey,TValue}.UnpackToCore(MsgPack.Unpacker,TDictionary)"/>.
@@ -104,14 +125,36 @@ protected override int GetCount( TDictionary dictionary )
104125 /// </exception>
105126 protected override void AddItem ( TDictionary dictionary , TKey key , TValue value )
106127 {
107- #if ( ! UNITY && ! XAMARIN ) || AOT_CHECK
128+ #if ! NETSTANDARD2_0
108129 dictionary . Add ( key , value ) ;
109- #else
130+ #else // !NETSTANDARD2_0
131+ if ( SerializerOptions . CanEmit )
132+ {
133+ this . AddItemCore ( dictionary , key , value ) ;
134+ }
135+ else
136+ {
137+ this . AddItemCoreAotSafe ( dictionary , key , value ) ;
138+ }
139+ #endif // !NETSTANDARD2_0
140+ }
141+
142+ #if NETSTANDARD2_0
143+
144+ private void AddItemCore ( TDictionary dictionary , TKey key , TValue value )
145+ {
146+ dictionary . Add ( key , value ) ;
147+ }
148+
149+ private void AddItemCoreAotSafe ( TDictionary dictionary , TKey key , TValue value )
150+ {
110151 // .constraind call for TDictionary.Add causes AOT error.
111152 // So use cast and invoke as normal call (it might cause boxing, but most collection should be reference type).
112153 ( dictionary as IDictionary < TKey , TValue > ) . Add ( key , value ) ;
113- #endif // ( !UNITY && !XAMARIN ) || AOT_CHECK
114154 }
155+
156+ #endif // NETSTANDARD2_0
157+
115158 }
116159
117160#if UNITY
0 commit comments