| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 71ca063 commit 9d18a24
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -9,6 +9,8 @@ This document follows the conventions laid out in [Keep a CHANGELOG][]. | |||
| 9 | 9 | ||
| 10 | 10 | ### Added | |
| 11 | 11 | ||
| 12 | + - Added `ToPythonAs<T>()` extension method to allow for explicit conversion using a specific type. ([#2311][i2311]) | ||
| 13 | + | ||
| 12 | 14 | ### Changed | |
| 13 | 15 | ||
| 14 | 16 | ### Fixed | |
@@ -960,3 +962,4 @@ This version improves performance on benchmarks significantly compared to 2.3. | |||
| 960 | 962 | [i238]: https://github.com/pythonnet/pythonnet/issues/238 | |
| 961 | 963 | [i1481]: https://github.com/pythonnet/pythonnet/issues/1481 | |
| 962 | 964 | [i1672]: https://github.com/pythonnet/pythonnet/pull/1672 | |
| 965 | + [i2311]: https://github.com/pythonnet/pythonnet/issues/2311 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -185,6 +185,15 @@ public void RawPyObjectProxy() | |||
| 185 | 185 | Assert.AreEqual(pyObject.DangerousGetAddressOrNull(), proxiedHandle); | |
| 186 | 186 | } | |
| 187 | 187 | ||
| 188 | + [Test] | ||
| 189 | + public void GenericToPython() | ||
| 190 | + { | ||
| 191 | + int i = 42; | ||
| 192 | + var pyObject = i.ToPythonAs<IConvertible>(); | ||
| 193 | + var type = pyObject.GetPythonType(); | ||
| 194 | + Assert.AreEqual(nameof(IConvertible), type.Name); | ||
| 195 | + } | ||
| 196 | + | ||
| 188 | 197 | // regression for https://github.com/pythonnet/pythonnet/issues/451 | |
| 189 | 198 | [Test] | |
| 190 | 199 | public void CanGetListFromDerivedClass() | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -133,7 +133,8 @@ internal static NewReference ToPython(object? value, Type type) | |||
| 133 | 133 | if (EncodableByUser(type, value)) | |
| 134 | 134 | { | |
| 135 | 135 | var encoded = PyObjectConversions.TryEncode(value, type); | |
| 136 | - if (encoded != null) { | ||
| 136 | + if (encoded != null) | ||
| 137 | + { | ||
| 137 | 138 | return new NewReference(encoded); | |
| 138 | 139 | } | |
| 139 | 140 | } | |
@@ -334,7 +335,7 @@ internal static bool ToManagedValue(BorrowedReference value, Type obType, | |||
| 334 | 335 | ||
| 335 | 336 | if (obType.IsGenericType && obType.GetGenericTypeDefinition() == typeof(Nullable<>)) | |
| 336 | 337 | { | |
| 337 | - if( value == Runtime.PyNone ) | ||
| 338 | + if (value == Runtime.PyNone) | ||
| 338 | 339 | { | |
| 339 | 340 | result = null; | |
| 340 | 341 | return true; | |
@@ -980,5 +981,11 @@ public static PyObject ToPython(this object? o) | |||
| 980 | 981 | if (o is null) return Runtime.None; | |
| 981 | 982 | return Converter.ToPython(o, o.GetType()).MoveToPyObject(); | |
| 982 | 983 | } | |
| 984 | + | ||
| 985 | + public static PyObject ToPythonAs<T>(this T? o) | ||
| 986 | + { | ||
| 987 | + if (o is null) return Runtime.None; | ||
| 988 | + return Converter.ToPython(o, typeof(T)).MoveToPyObject(); | ||
| 989 | + } | ||
| 983 | 990 | } | |
| 984 | 991 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments