@@ -168,9 +168,11 @@ internal static Type CreateDerivedType(string name,
168
168
169
169
// add a field for storing the python object pointer
170
170
// FIXME: fb not used
171
- FieldBuilder fb = typeBuilder . DefineField ( "__pyobj__" ,
171
+ FieldBuilder fb = typeBuilder . DefineField ( PyObjName ,
172
+ #pragma warning disable CS0618 // Type or member is obsolete. OK for internal use.
172
173
typeof ( UnsafeReferenceWithRun ) ,
173
- FieldAttributes . Public ) ;
174
+ #pragma warning restore CS0618 // Type or member is obsolete
175
+ FieldAttributes . Private ) ;
174
176
175
177
// override any constructors
176
178
ConstructorInfo [ ] constructors = baseClass . GetConstructors ( ) ;
@@ -646,6 +648,9 @@ private static ModuleBuilder GetModuleBuilder(string assemblyName, string module
646
648
[ Obsolete ( Util . InternalUseOnly ) ]
647
649
public class PythonDerivedType
648
650
{
651
+ internal const string PyObjName = "__pyobj__" ;
652
+ internal const BindingFlags PyObjFlags = BindingFlags . Instance | BindingFlags . NonPublic ;
653
+
649
654
/// <summary>
650
655
/// This is the implementation of the overridden methods in the derived
651
656
/// type. It looks for a python method with the same name as the method
@@ -849,15 +854,17 @@ public static void PyFinalize(IPythonDerivedType obj)
849
854
Finalizer . Instance . AddDerivedFinalizedObject ( ref self . RawObj , self . Run ) ;
850
855
}
851
856
857
+ internal static FieldInfo ? GetPyObjField ( Type type ) => type . GetField ( PyObjName , PyObjFlags ) ;
858
+
852
859
internal static UnsafeReferenceWithRun GetPyObj ( IPythonDerivedType obj )
853
860
{
854
- FieldInfo fi = obj . GetType ( ) . GetField ( "__pyobj__" ) ;
861
+ FieldInfo fi = GetPyObjField ( obj . GetType ( ) ) ! ;
855
862
return ( UnsafeReferenceWithRun ) fi . GetValue ( obj ) ;
856
863
}
857
864
858
865
static void SetPyObj ( IPythonDerivedType obj , BorrowedReference pyObj )
859
866
{
860
- FieldInfo fi = obj . GetType ( ) . GetField ( "__pyobj__" ) ;
867
+ FieldInfo fi = GetPyObjField ( obj . GetType ( ) ) ! ;
861
868
fi . SetValue ( obj , new UnsafeReferenceWithRun ( pyObj ) ) ;
862
869
}
863
870
}
0 commit comments