8000 Support clr.GetClrType() - as in IronPython (#433) · attackgithub/pythonnet@39e4673 · GitHub
[go: up one dir, main page]

Skip to content

Commit 39e4673

Browse files
vivainiovmuriart
authored andcommitted
Support clr.GetClrType() - as in IronPython (pythonnet#433)
* Support clr.GetClrType() - as in IronPython Implements pythonnet#432 * Tests for clr.GetClrType() * clr.GetClrType test: ensure bad type raises ArgumentException * clr.GetClrType - added xml doc comment, updated AUTHORS.md and CHANGELOG.md * Simplified implementation of clr.GetClrType (taken from IronPython)
1 parent c8f3756 commit 39e4673

File tree

4 files changed

+39
-1
lines changed

4 files changed

+39
-1
lines changed

AUTHORS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
- Sam Winstanley ([@swinstanley](https://github.com/swinstanley))
3333
- Sean Freitag ([@cowboygneox](https://github.com/cowboygneox))
3434
- Serge Weinstock ([@sweinst](https://github.com/sweinst))
35+
- Ville M. Vainio ([@vivainio](https://github.com/vivainio))
3536
- Virgil Dupras ([@hsoft](https://github.com/hsoft))
3637
- Wenguang Yang ([@yagweb](https://github.com/yagweb))
3738
- Xavier Dupré ([@sdpython](https://github.com/sdpython))

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ This document follows the conventions laid out in [Keep a CHANGELOG][].
88
## [unreleased][]
99

1010
### Added
11-
11+
- Added clr.GetClrType (#432)(#433)
1212
- Added `Foo` feature
1313

1414
### Changed

src/runtime/moduleobject.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,23 @@ public static Assembly AddReference(string name)
403403
return assembly;
404404
}
405405

406+
/// <summary>
407+
/// Get a Type instance for a class object.
408+
/// clr.GetClrType(IComparable) gives you the Type for IComparable,
409+
/// that you can e.g. perform reflection on. Similar to typeof(IComparable) in C#
410+
/// or clr.GetClrType(IComparable) in IronPython.
411+
///
412+
/// </summary>
413+
/// <param name="type"></param>
414+
/// <returns>The Type object</returns>
415+
416+
[ModuleFunction]
417+
[ForbidPythonThreads]
418+
public static Type GetClrType(Type type)
419+
{
420+
return type;
421+
}
422+
406423
[ModuleFunction]
407424
[ForbidPythonThreads]
408425
public static string FindAssembly(string name)

src/tests/test_module.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,26 @@ def test_clr_add_reference():
352352
with pytest.raises(FileNotFoundException):
353353
AddReference("somethingtotallysilly")
354354

355+
def test_clr_get_clr_type():
356+
"""Test clr.GetClrType()."""
357+
from clr import GetClrType
358+
import System
359+
from System import IComparable
360+
from System import ArgumentException
361+
assert GetClrType(System.String).FullName == "System.String"
362+
comparable = GetClrType(IComparable)
363+
assert comparable.FullName == "System.IComparable"
364+
assert comparable.IsInterface
365+
assert GetClrType(int).FullName == "System.Int32"
366+
assert GetClrType(str).FullName == "System.String"
367+
assert GetClrType(float).FullName == "System.Double"
368+
dblarr = System.Array[System.Double]
369+
assert GetClrType(dblarr).FullName == "System.Double[]"
370+
371+
with pytest.raises(TypeError):
372+
GetClrType(1)
373+
with pytest.raises(TypeError):
374+
GetClrType("thiswillfail")
355375

356376
def test_assembly_load_thread_safety():
357377
from Python.Test import ModuleTest

0 commit comments

Comments
 (0)
0