Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
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 1 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 10 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 14 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
Morty Proxy This is a proxified and sanitized view of the page, visit original site.