using System;
using System.Reflection;
using System.Runtime.Serialization;
namespace Python.Runtime
{
using MaybeMethodInfo = MaybeMethodBase;
///
/// Implements a Python descriptor type that manages CLR properties.
///
[Serializable]
internal class PropertyObject : ExtensionType, IDeserializationCallback
{
internal MaybeMemberInfo info;
[NonSerialized]
private MethodInfo? getter;
[NonSerialized]
private MethodInfo? setter;
public PropertyObject(PropertyInfo md)
{
info = new MaybeMemberInfo(md);
CacheAccessors();
}
void CacheAccessors()
{
PropertyInfo md = info.Value;
getter = md.GetGetMethod(true) ?? md.GetBaseGetMethod(true);
setter = md.GetSetMethod(true) ?? md.GetBaseSetMethod(true);
}
///
/// Descriptor __get__ implementation. This method returns the
/// value of the property on the given object. The returned value
/// is converted to an appropriately typed Python object.
///
public static NewReference tp_descr_get(BorrowedReference ds, BorrowedReference ob, BorrowedReference tp)
{
var self = (PropertyObject)GetManagedObject(ds)!;
if (!self.info.Valid)
{
return Exceptions.RaiseTypeError(self.info.DeletedMessage);
}
var info = self.info.Value;
MethodInfo? getter = self.getter;
object result;
if (getter == null)
{
return Exceptions.RaiseTypeError("property cannot be read");
}
if (ob == null || ob == Runtime.PyNone)
{
if (!getter.IsStatic)
{
return new NewReference(ds);
}
try
{
result = info.GetValue(null, null);
return Converter.ToPython(result, info.PropertyType);
}
catch (Exception e)
{
return Exceptions.RaiseTypeError(e.Message);
}
}
var co = GetManagedObject(ob) as CLRObject;
if (co == null)
{
return Exceptions.RaiseTypeError("invalid target");
}
try
{
result = getter.Invoke(co.inst, Array.Empty