FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Change as by koubaa · Pull Request #1118 · pythonnet/pythonnet · GitHub

Repository navigation

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .cs  (2) .md  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
1 change: 1 addition & 0 deletions CHANGELOG.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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

Back | FazBrowse Home | New Git URL