Skip to content

Change as #1118

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ This document follows the conventions laid out in [Keep a CHANGELOG][].
- When calling C# from Python, enable passing argument of any type to a parameter of C# type `object` by wrapping it into `PyObject` instance. ([#881][i881])
- Added support for kwarg parameters when calling .NET methods from Python
- Changed method for finding MSBuild using vswhere
- Allow conversion to C# object in PyObject.As2<object>

### Fixed

Expand Down
10 changes: 10 additions & 0 deletions src/embed_tests/TestConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,15 @@ public void RawPyObjectProxy()
var proxiedHandle = pyObjectProxy.GetAttr("Handle").As<IntPtr>();
Assert.AreEqual(pyObject.Handle, proxiedHandle);
}

[Test]
public void ConvertToObject()
{
object pyInt = new PyInt(12).As2<object>();
Assert.AreSame(pyInt.GetType(), typeof(int));

object pyString = new PyString("hello").As2<object>();
Assert.AreSame(pyString.GetType(), typeof(string));
}
}
}
14 changes: 14 additions & 0 deletions src/runtime/pyobject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,20 @@ public T As<T>()
return (T)result;
}

public T As2<T>()
{
if (typeof(T) == typeof(PyObject))
{
return (T)(this as object);
}
object result;
if (!Converter.ToManaged(obj, typeof(T), out result, false))
{
throw new InvalidCastException("cannot convert object to target type");
}
return (T)result;
}


/// <summary>
/// Dispose Method
Expand Down
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