@@ -149,11 +149,8 @@ internal static IntPtr ToPython(object value, Type type)
149149 return result ;
150150 }
151151
152- if ( Type . GetTypeCode ( type ) == TypeCode . Object
153- && value . GetType ( ) != typeof ( object )
154- && value is not Type
155- || type . IsEnum
156- ) {
152+ if ( EncodableByUser ( type , value ) )
153+ {
157154 var encoded = PyObjectConversions . TryEncode ( value , type ) ;
158155 if ( encoded != null ) {
159156 result = encoded . Handle ;
@@ -301,6 +298,13 @@ internal static IntPtr ToPython(object value, Type type)
301298 }
302299 }
303300
301+ static bool EncodableByUser ( Type type , object value )
302+ {
303+ TypeCode typeCode = Type . GetTypeCode ( type ) ;
304+ return type . IsEnum
305+ || typeCode is TypeCode . DateTime or TypeCode . Decimal
306+ || typeCode == TypeCode . Object && value . GetType ( ) != typeof ( object ) && value is not Type ;
307+ }
304308
305309 /// <summary>
306310 /// In a few situations, we don't have any advisory type information
@@ -523,8 +527,7 @@ internal static bool ToManagedValue(IntPtr value, Type obType,
523527 return false ;
524528 }
525529
526- TypeCode typeCode = Type . GetTypeCode ( obType ) ;
527- if ( typeCode == TypeCode . Object || obType . IsEnum )
530+ if ( DecodableByUser ( obType ) )
528531 {
529532 IntPtr pyType = Runtime . PyObject_TYPE ( value ) ;
530533 if ( PyObjectConversions . TryDecode ( value , pyType , obType , out result ) )
@@ -536,6 +539,13 @@ internal static bool ToManagedValue(IntPtr value, Type obType,
536539 return ToPrimitive ( value , obType , out result , setError ) ;
537540 }
538541
542+ static bool DecodableByUser ( Type type )
543+ {
544+ TypeCode typeCode = Type . GetTypeCode ( type ) ;
545+ return type . IsEnum
546+ || typeCode is TypeCode . Object or TypeCode . Decimal or TypeCode . DateTime ;
547+ }
548+
539549 internal delegate bool TryConvertFromPythonDelegate ( IntPtr pyObj , out object result ) ;
540550
541551 internal static int ToInt32 ( BorrowedReference value )
0 commit comments