diff --git a/src/embed_tests/Codecs.cs b/src/embed_tests/Codecs.cs index e7303a8e4..6fc5bb59b 100644 --- a/src/embed_tests/Codecs.cs +++ b/src/embed_tests/Codecs.cs @@ -373,6 +373,31 @@ from datetime import datetime public static void AcceptsDateTime(DateTime v) {} + [Test] + public void As_Object_AffectedByDecoders() + { + var everythingElseToSelf = new EverythingElseToSelfDecoder(); + PyObjectConversions.RegisterDecoder(everythingElseToSelf); + + var pyObj = PythonEngine.Eval("iter"); + var decoded = pyObj.As(); + Assert.AreSame(everythingElseToSelf, decoded); + } + + public class EverythingElseToSelfDecoder : IPyObjectDecoder + { + public bool CanDecode(PyObject objectType, Type targetType) + { + return targetType.IsAssignableFrom(typeof(EverythingElseToSelfDecoder)); + } + + public bool TryDecode(PyObject pyObj, out T value) + { + value = (T)(object)this; + return true; + } + } + class ValueErrorWrapper : Exception { public ValueErrorWrapper(string message) : base(message) { } diff --git a/src/runtime/pyobject.cs b/src/runtime/pyobject.cs index 4a61e65e8..a684dab88 100644 --- a/src/runtime/pyobject.cs +++ b/src/runtime/pyobject.cs @@ -163,20 +163,10 @@ public object AsManagedObject(Type t) } /// - /// As Method - /// - /// /// Return a managed object of the given type, based on the /// value of the Python object. - /// - public T As() - { - if (typeof(T) == typeof(PyObject) || typeof(T) == typeof(object)) - { - return (T)(this as object); - } - return (T)AsManagedObject(typeof(T)); - } + /// + public T As() => (T)this.AsManagedObject(typeof(T)); internal bool IsDisposed => obj == IntPtr.Zero; pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy