@@ -192,6 +192,7 @@ public TypeMapEntry(DbType dbType, TypeMapEntryFlags flags)
192
192
public bool Equals ( TypeMapEntry other ) => other . DbType == DbType && other . Flags == Flags ;
193
193
public static readonly TypeMapEntry
194
194
DoNotSet = new ( ( DbType ) ( - 2 ) , TypeMapEntryFlags . None ) ,
195
+ DoNotSetFieldValue = new ( ( DbType ) ( - 2 ) , TypeMapEntryFlags . UseGetFieldValue ) ,
195
196
DecimalFieldValue = new ( DbType . Decimal , TypeMapEntryFlags . SetType | TypeMapEntryFlags . UseGetFieldValue ) ,
196
197
StringFieldValue = new ( DbType . String , TypeMapEntryFlags . SetType | TypeMapEntryFlags . UseGetFieldValue ) ,
197
198
BinaryFieldValue = new ( DbType . Binary , TypeMapEntryFlags . SetType | TypeMapEntryFlags . UseGetFieldValue ) ;
@@ -202,7 +203,11 @@ public static implicit operator TypeMapEntry(DbType dbType)
202
203
203
204
static SqlMapper ( )
204
205
{
205
- typeMap = new Dictionary < Type , TypeMapEntry > ( 41 )
206
+ typeMap = new Dictionary < Type , TypeMapEntry > ( 41
207
+ #if NET6_0_OR_GREATER
208
+ + 4 // {Date|Time}Only[?]
209
+ #endif
210
+ )
206
211
{
207
212
[ typeof ( byte ) ] = DbType . Byte ,
208
213
[ typeof ( sbyte ) ] = DbType . SByte ,
@@ -245,6 +250,12 @@ static SqlMapper()
245
250
[ typeof ( SqlDecimal ? ) ] = TypeMapEntry . DecimalFieldValue ,
246
251
[ typeof ( SqlMoney ) ] = TypeMapEntry . DecimalFieldValue ,
247
252
[ typeof ( SqlMoney ? ) ] = TypeMapEntry . DecimalFieldValue ,
253
+ #if NET6_0_OR_GREATER
254
+ [ typeof ( DateOnly ) ] = TypeMapEntry . DoNotSetFieldValue ,
255
+ [ typeof ( TimeOnly ) ] = TypeMapEntry . DoNotSetFieldValue ,
256
+ [ typeof ( DateOnly ? ) ] = TypeMapEntry . DoNotSetFieldValue ,
257
+ [ typeof ( TimeOnly ? ) ] = TypeMapEntry . DoNotSetFieldValue ,
258
+ #endif
248
259
} ;
249
260
ResetTypeHandlers ( false ) ;
250
261
}
@@ -257,7 +268,7 @@ static SqlMapper()
257
268
[ MemberNotNull ( nameof ( typeHandlers ) ) ]
258
269
private static void ResetTypeHandlers ( bool clone )
259
270
{
260
- typeHandlers = new Dictionary < Type , ITypeHandler > ( ) ;
271
+ typeHandlers = [ ] ;
261
272
AddTypeHandlerImpl ( typeof ( DataTable ) , new DataTableHandler ( ) , clone ) ;
262
273
AddTypeHandlerImpl ( typeof ( XmlDocument ) , new XmlDocumentHandler ( ) , clone ) ;
263
274
AddTypeHandlerImpl ( typeof ( XDocument ) , new XDocumentHandler ( ) , clone ) ;
@@ -370,10 +381,10 @@ public static void AddTypeHandlerImpl(Type type, ITypeHandler? handler, bool clo
370
381
var newCopy = clone ? new Dictionary < Type , ITypeHandler > ( snapshot ) : snapshot ;
371
382
372
383
#pragma warning disable 618
373
- typeof ( TypeHandlerCache < > ) . MakeGenericType ( type ) . GetMethod ( nameof ( TypeHandlerCache < int > . SetHandler ) , BindingFlags . Static | BindingFlags . NonPublic ) ! . Invoke ( null , new object ? [ ] { handler } ) ;
384
+ typeof ( TypeHandlerCache < > ) . MakeGenericType ( type ) . GetMethod ( nameof ( TypeHandlerCache < int > . SetHandler ) , BindingFlags . Static | BindingFlags . NonPublic ) ! . Invoke ( null , [ handler ] ) ;
374
385
if ( secondary is not null )
375
386
{
376
- typeof ( TypeHandlerCache < > ) . MakeGenericType ( secondary ) . GetMethod ( nameof ( TypeHandlerCache < int > . SetHandler ) , BindingFlags . Static | BindingFlags . NonPublic ) ! . Invoke ( null , new object ? [ ] { handler } ) ;
387
+ typeof ( TypeHandlerCache < > ) . MakeGenericType ( secondary ) . GetMethod ( nameof ( TypeHandlerCache < int > . SetHandler ) , BindingFlags . Static | BindingFlags . NonPublic ) ! . Invoke ( null , [ handler ] ) ;
377
388
}
378
389
#pragma warning restore 618
379
390
if ( handler is null )
@@ -1240,7 +1251,7 @@ internal enum Row
1240
1251
SingleOrDefault = 3
1241
1252
}
1242
1253
1243
- private static readonly int [ ] ErrTwoRows = new int [ 2 ] , ErrZeroRows = Array . Empty < int > ( ) ;
1254
+ private static readonly int [ ] ErrTwoRows = new int [ 2 ] , ErrZeroRows = [ ] ;
1244
1255
private static void ThrowMultipleRows ( Row row )
1245
1256
{
1246
1257
_ = row switch
@@ -2538,7 +2549,7 @@ private static bool IsValueTuple(Type? type) => (type?.IsValueType == true
2538
2549
filterParams = ! CompiledRegex . LegacyParameter . IsMatch ( identity . Sql ) ;
2539
2550
}
2540
2551
2541
- var dm = new DynamicMethod ( "ParamInfo" + Guid . NewGuid ( ) . ToString ( ) , null , new [ ] { typeof ( IDbCommand ) , typeof ( object ) } , type , true ) ;
2552
+ var dm = new DynamicMethod ( "ParamInfo" + Guid . NewGuid ( ) . ToString ( ) , null , [ typeof ( IDbCommand ) , typeof ( object ) ] , type , true ) ;
2542
2553
2543
2554
var il = dm . GetILGenerator ( ) ;
2544
2555
@@ -2909,7 +2920,7 @@ private static bool IsValueTuple(Type? type) => (type?.IsValueType == true
2909
2920
{
2910
2921
if ( locals is null )
2911
2922
{
2912
- locals = new Dictionary < Type , LocalBuilder > ( ) ;
2923
+ locals = [ ] ;
2913
2924
local = null ;
2914
2925
}
2915
2926
else
@@ -2946,14 +2957,14 @@ private static bool IsValueTuple(Type? type) => (type?.IsValueType == true
2946
2957
{
2947
2958
typeof ( bool ) , typeof ( sbyte ) , typeof ( byte ) , typeof ( ushort ) , typeof ( short ) ,
2948
2959
typeof ( uint ) , typeof ( int ) , typeof ( ulong ) , typeof ( long ) , typeof ( float ) , typeof ( double ) , typeof ( decimal )
2949
- } . ToDictionary ( x => Type . GetTypeCode ( x ) , x => x . GetPublicInstanceMethod ( nameof ( object . ToString ) , new [ ] { typeof ( IFormatProvider ) } ) ! ) ;
2960
+ } . ToDictionary ( x => Type . GetTypeCode ( x ) , x => x . GetPublicInstanceMethod ( nameof ( object . ToString ) , [ typeof ( IFormatProvider ) ] ) ! ) ;
2950
2961
2951
2962
private static MethodInfo ? GetToString ( TypeCode typeCode )
2952
2963
{
2953
2964
return toStrings . TryGetValue ( typeCode , out MethodInfo ? method ) ? method : null ;
2954
2965
}
2955
2966
2956
- private static readonly MethodInfo StringReplace = typeof ( string ) . GetPublicInstanceMethod ( nameof ( string . Replace ) , new Type [ ] { typeof ( string ) , typeof ( string ) } ) ! ,
2967
+ private static readonly MethodInfo StringReplace = typeof ( string ) . GetPublicInstanceMethod ( nameof ( string . Replace ) , [ typeof ( string ) , typeof ( string ) ] ) ! ,
2957
2968
InvariantCulture = typeof ( CultureInfo ) . GetProperty ( nameof ( CultureInfo . InvariantCulture ) , BindingFlags . Public | BindingFlags . Static ) ! . GetGetMethod ( ) ! ;
2958
2969
2959
2970
private static int ExecuteCommand ( IDbConnection cnn , ref CommandDefinition command , Action < IDbCommand , object ? > ? paramReader )
@@ -3117,7 +3128,7 @@ static Func<DbDataReader, object> ReadViaGetFieldValueFactory(Type type, int ind
3117
3128
return factory ( index ) ;
3118
3129
}
3119
3130
// cache of ReadViaGetFieldValueFactory<T> for per-value T
3120
- static readonly Hashtable s_ReadViaGetFieldValueCache = new ( ) ;
3131
+ static readonly Hashtable s_ReadViaGetFieldValueCache = [ ] ;
3121
3132
3122
3133
static Func < DbDataReader , object > UnderlyingReadViaGetFieldValueFactory < T > ( int index )
3123
3134
=> reader => reader . IsDBNull ( index ) ? null ! : reader . GetFieldValue < T > ( index ) ! ;
@@ -3147,14 +3158,14 @@ private static T Parse<T>(object? value)
3147
3158
}
3148
3159
3149
3160
private static readonly MethodInfo
3150
- enumParse = typeof ( Enum ) . GetMethod ( nameof ( Enum . Parse ) , new Type [ ] { typeof ( Type ) , typeof ( string ) , typeof ( bool ) } ) ! ,
3161
+ enumParse = typeof ( Enum ) . GetMethod ( nameof ( Enum . Parse ) , [ typeof ( Type ) , typeof ( string ) , typeof ( bool ) ] ) ! ,
3151
3162
getItem = typeof ( DbDataReader ) . GetProperties ( BindingFlags . Instance | BindingFlags . Public )
3152
3163
. Where ( p => p . GetIndexParameters ( ) . Length > 0 && p . GetIndexParameters ( ) [ 0 ] . ParameterType == typeof ( int ) )
3153
3164
. Select ( p => p . GetGetMethod ( ) ) . First ( ) ! ,
3154
3165
getFieldValueT = typeof ( DbDataReader ) . GetMethod ( nameof ( DbDataReader . GetFieldValue ) ,
3155
- BindingFlags . Instance | BindingFlags . Public , null , new Type [ ] { typeof ( int ) } , null ) ! ,
3166
+ BindingFlags . Instance | BindingFlags . Public , null , [ typeof ( int ) ] , null ) ! ,
3156
3167
isDbNull = typeof ( DbDataReader ) . GetMethod ( nameof ( DbDataReader . IsDBNull ) ,
3157
- BindingFlags . Instance | BindingFlags . Public , null , new Type [ ] { typeof ( int ) } , null ) ! ;
3168
+ BindingFlags . Instance | BindingFlags . Public , null , [ typeof ( int ) ] , null ) ! ;
3158
3169
3159
3170
/// <summary>
3160
3171
/// Gets type-map for the given type
@@ -3191,7 +3202,7 @@ public static ITypeMap GetTypeMap(Type type)
3191
3202
}
3192
3203
3193
3204
// use Hashtable to get free lockless reading
3194
- private static readonly Hashtable _typeMaps = new ( ) ;
3205
+ private static readonly Hashtable _typeMaps = [ ] ;
3195
3206
3196
3207
/// <summary>
3197
3208
/// Set custom mapping for type deserializers
@@ -3263,7 +3274,7 @@ public static Func<DbDataReader, object> GetTypeDeserializer(
3263
3274
private static LocalBuilder GetTempLocal ( ILGenerator il , ref Dictionary < Type , LocalBuilder > ? locals , Type type , bool initAndLoad )
3264
3275
{
3265
3276
if ( type is null ) throw new ArgumentNullException ( nameof ( type ) ) ;
3266
- locals ??= new Dictionary < Type , LocalBuilder > ( ) ;
3277
+ locals ??= [ ] ;
3267
3278
if ( ! locals . TryGetValue ( type , out LocalBuilder ? found ) )
3268
3279
{
3269
3280
found = il . DeclareLocal ( type ) ;
@@ -3294,7 +3305,7 @@ private static Func<DbDataReader, object> GetTypeDeserializerImpl(
3294
3305
}
3295
3306
3296
3307
var returnType = type . IsValueType ? typeof ( object ) : type ;
3297
- var dm = new DynamicMethod ( "Deserialize" + Guid . NewGuid ( ) . ToString ( ) , returnType , new [ ] { typeof ( DbDataReader ) } , type , true ) ;
3308
+ var dm = new DynamicMethod ( "Deserialize" + Guid . NewGuid ( ) . ToString ( ) , returnType , [ typeof ( DbDataReader ) ] , type , true ) ;
3298
3309
var il = dm . GetILGenerator ( ) ;
3299
3310
3300
3311
if ( IsValueTuple ( type ) )
@@ -3403,7 +3414,7 @@ private static void GenerateValueTupleDeserializer(Type valueTupleType, DbDataRe
3403
3414
3404
3415
if ( nullableUnderlyingType is not null )
3405
3416
{
3406
- var nullableTupleConstructor = valueTupleType . GetConstructor ( new [ ] { nullableUnderlyingType } ) ;
3417
+ var nullableTupleConstructor = valueTupleType . GetConstructor ( [ nullableUnderlyingType ] ) ;
3407
3418
3408
3419
il . Emit ( OpCodes . Newobj , nullableTupleConstructor ! ) ;
3409
3420
}
@@ -3668,7 +3679,7 @@ private static void LoadReaderValueViaGetFieldValue(ILGenerator il, int index, T
3668
3679
if ( underlyingType != memberType )
3669
3680
{
3670
3681
// Nullable<T>; wrap it
3671
- il . Emit ( OpCodes . Newobj , memberType . GetConstructor ( new [ ] { underlyingType } ) ! ) ; // stack is now [...][T?]
3682
+ il . Emit ( OpCodes . Newobj , memberType . GetConstructor ( [ underlyingType ] ) ! ) ; // stack is now [...][T?]
3672
3683
}
3673
3684
}
3674
3685
@@ -3731,13 +3742,13 @@ private static void LoadReaderValueOrBranchToDBNullLabel(ILGenerator il, int ind
3731
3742
3732
3743
if ( nullUnderlyingType is not null )
3733
3744
{
3734
- il . Emit ( OpCodes . Newobj , memberType . GetConstructor ( new [ ] { nullUnderlyingType } ) ! ) ; // stack is now [...][typed-value]
3745
+ il . Emit ( OpCodes . Newobj , memberType . GetConstructor ( [ nullUnderlyingType ] ) ! ) ; // stack is now [...][typed-value]
3735
3746
}
3736
3747
}
3737
3748
else if ( memberType . FullName == LinqBinary )
3738
3749
{
3739
3750
il . Emit ( OpCodes . Unbox_Any , typeof ( byte [ ] ) ) ; // stack is now [...][byte-array]
3740
- il . Emit ( OpCodes . Newobj , memberType . GetConstructor ( new Type [ ] { typeof ( byte [ ] ) } ) ! ) ; // stack is now [...][binary]
3751
+ il . Emit ( OpCodes . Newobj , memberType . GetConstructor ( [ typeof ( byte [ ] ) ] ) ! ) ; // stack is now [...][binary]
3741
3752
}
3742
3753
else
3743
3754
{
@@ -3762,7 +3773,7 @@ private static void LoadReaderValueOrBranchToDBNullLabel(ILGenerator il, int ind
3762
3773
FlexibleConvertBoxedFromHeadOfStack ( il , colType , nullUnderlyingType ?? unboxType , null ) ;
3763
3774
if ( nullUnderlyingType is not null )
3764
3775
{
3765
- il . Emit ( OpCodes . Newobj , unboxType . GetConstructor ( new [ ] { nullUnderlyingType } ) ! ) ; // stack is now [...][typed-value]
3776
+ il . Emit ( OpCodes . Newobj , unboxType . GetConstructor ( [ nullUnderlyingType ] ) ! ) ; // stack is now [...][typed-value]
3766
3777
}
3767
3778
}
3768
3779
}
@@ -3846,7 +3857,7 @@ private static void FlexibleConvertBoxedFromHeadOfStack(ILGenerator il, Type fro
3846
3857
il . Emit ( OpCodes . Ldtoken , via ?? to ) ; // stack is now [target][target][value][member-type-token]
3847
3858
il . EmitCall ( OpCodes . Call , typeof ( Type ) . GetMethod ( nameof ( Type . GetTypeFromHandle ) ) ! , null ) ; // stack is now [target][target][value][member-type]
3848
3859
il . EmitCall ( OpCodes . Call , InvariantCulture , null ) ; // stack is now [target][target][value][member-type][culture]
3849
- il . EmitCall ( OpCodes . Call , typeof ( Convert ) . GetMethod ( nameof ( Convert . ChangeType ) , new Type [ ] { typeof ( object ) , typeof ( Type ) , typeof ( IFormatProvider ) } ) ! , null ) ; // stack is now [target][target][boxed-member-type-value]
3860
+ il . EmitCall ( OpCodes . Call , typeof ( Convert ) . GetMethod ( nameof ( Convert . ChangeType ) , [ typeof ( object ) , typeof ( Type ) , typeof ( IFormatProvider ) ] ) ! , null ) ; // stack is now [target][target][boxed-member-type-value]
3850
3861
il . Emit ( OpCodes . Unbox_Any , to ) ; // stack is now [target][target][typed-value]
3851
3862
}
3852
3863
}
0 commit comments