@@ -64,6 +64,14 @@ public abstract class MySqlJsonTypeMapping : MySqlStringTypeMapping, IMySqlCShar
6464 private static readonly MethodInfo _getString
6565 = typeof ( DbDataReader ) . GetRuntimeMethod ( nameof ( DbDataReader . GetString ) , new [ ] { typeof ( int ) } ) ;
6666
67+ // Cache reflection lookups for performance
68+ private static readonly PropertyInfo _utf8Property
69+ = typeof ( System . Text . Encoding ) . GetProperty ( nameof ( System . Text . Encoding . UTF8 ) ) ;
70+ private static readonly MethodInfo _getBytesMethod
71+ = typeof ( System . Text . Encoding ) . GetMethod ( nameof ( System . Text . Encoding . GetBytes ) , new [ ] { typeof ( string ) } ) ;
72+ private static readonly ConstructorInfo _memoryStreamCtor
73+ = typeof ( System . IO . MemoryStream ) . GetConstructor ( new [ ] { typeof ( byte [ ] ) } ) ;
74+
6775 public MySqlJsonTypeMapping (
6876 [ NotNull ] string storeType ,
6977 [ NotNull ] Type clrType ,
@@ -145,18 +153,20 @@ public override Expression CustomizeDataReaderExpression(Expression expression)
145153
146154 if ( expression . Type == typeof ( string ) )
147155 {
148- // Convert string to MemoryStream: new MemoryStream(Encoding.UTF8.GetBytes(stringValue))
149- var utf8Property = typeof ( System . Text . Encoding ) . GetProperty ( nameof ( System . Text . Encoding . UTF8 ) ) ! ;
150- var getBytesMethod = typeof ( System . Text . Encoding ) . GetMethod (
151- nameof ( System . Text . Encoding . GetBytes ) ,
152- new [ ] { typeof ( string ) } ) ! ;
153- var memoryStreamCtor = typeof ( System . IO . MemoryStream ) . GetConstructor ( new [ ] { typeof ( byte [ ] ) } ) ! ;
156+ // Validate that reflection lookups succeeded
157+ if ( _utf8Property == null || _getBytesMethod == null || _memoryStreamCtor == null )
158+ {
159+ throw new InvalidOperationException (
160+ "Failed to find required reflection members for JSON type mapping. " +
161+ "This may indicate an incompatible version of the .NET runtime." ) ;
162+ }
154163
164+ // Convert string to MemoryStream: new MemoryStream(Encoding.UTF8.GetBytes(stringValue))
155165 return Expression . New (
156- memoryStreamCtor ,
166+ _memoryStreamCtor ,
157167 Expression . Call (
158- Expression . Property ( null , utf8Property ) ,
159- getBytesMethod ,
168+ Expression . Property ( null , _utf8Property ) ,
169+ _getBytesMethod ,
160170 expression ) ) ;
161171 }
162172
0 commit comments