@@ -181,7 +181,31 @@ public bool HasAttr(PyObject name)
181
181
{
182
182
return Runtime . PyObject_HasAttr ( obj , name . obj ) != 0 ;
183
183
}
184
-
184
+
185
+ /// <summary>
186
+ /// GetAttr Method For Dynamic Type
187
+ /// </summary>
188
+ /// <remarks>
189
+ /// Returns the named attribute of the Python object, or raises a
190
+ /// PythonException if the attribute access fails.
191
+ /// </remarks>
192
+ public object GetAttrDynamic ( string name )
193
+ {
194
+ IntPtr op = Runtime . PyObject_GetAttrString ( obj , name ) ;
195
+ if ( op == IntPtr . Zero )
196
+ {
197
+ throw new PythonException ( ) ;
198
+ }
199
+ if ( ManagedType . IsManagedType ( op ) )
200
+ {
201
+ ManagedType managedMethod = ManagedType . GetManagedObject ( op ) ;
202
+ if ( managedMethod is CLRObject )
203
+ {
204
+ return ( ( CLRObject ) managedMethod ) . inst ;
205
+ }
206
+ }
207
+ return CheckNone ( new PyObject ( op ) ) ;
208
+ }
185
209
186
210
/// <summary>
187
211
/// GetAttr Method
@@ -891,7 +915,7 @@ public override bool TryGetMember(GetMemberBinder binder, out object result)
891
915
{
892
916
if ( this . HasAttr ( binder . Name ) )
893
917
{
894
- result = CheckNone ( this . GetAttr ( binder . Name ) ) ;
918
+ result = this . GetAttrDynamic ( binder . Name ) ;
895
919
return true ;
896
920
}
897
921
else
@@ -904,7 +928,15 @@ public override bool TrySetMember(SetMemberBinder binder, object value)
904
928
{
905
929
if ( this . HasAttr ( binder . Name ) )
906
930
{
907
- this . SetAttr ( binder . Name , ( PyObject ) value ) ;
931
+ if ( value is PyObject )
932
+ {
933
+ this . SetAttr ( binder . Name , ( PyObject ) value ) ;
934
+ }
935
+ else
936
+ {
937
+ var ptr = CLRObject . GetInstHandle ( value , value . GetType ( ) ) ;
938
+ this . SetAttr ( binder . Name , new PyObject ( ptr ) ) ;
939
+ }
908
940
return true ;
909
941
}
910
942
else
0 commit comments