Skip to content

Commit

Permalink
Support clr.GetClrType() - as in IronPython (pythonnet#433)
Browse files Browse the repository at this point in the history
* 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)
  • Loading branch information
vivainio authored and yagweb committed Apr 7, 2017
1 parent 9afd7cf commit 2e44f8a
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 1 deletion.
1 change: 1 addition & 0 deletions AUTHORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
- Sam Winstanley ([@swinstanley](https://github.com/swinstanley))
- Sean Freitag ([@cowboygneox](https://github.com/cowboygneox))
- Serge Weinstock ([@sweinst](https://github.com/sweinst))
- Ville M. Vainio ([@vivainio](https://github.com/vivainio))
- Virgil Dupras ([@hsoft](https://github.com/hsoft))
- Wenguang Yang ([@yagweb](https://github.com/yagweb))
- Xavier Dupré ([@sdpython](https://github.com/sdpython))
Expand Down
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ This document follows the conventions laid out in [Keep a CHANGELOG][].
## [unreleased][]

### Added

- Added clr.GetClrType (#432)(#433)
- Added `Foo` feature

### Changed
Expand Down
17 changes: 17 additions & 0 deletions src/runtime/moduleobject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,23 @@ public static Assembly AddReference(string name)
return assembly;
}

/// <summary>
/// Get a Type instance for a class object.
/// clr.GetClrType(IComparable) gives you the Type for IComparable,
/// that you can e.g. perform reflection on. Similar to typeof(IComparable) in C#
/// or clr.GetClrType(IComparable) in IronPython.
///
/// </summary>
/// <param name="type"></param>
/// <returns>The Type object</returns>

[ModuleFunction]
[ForbidPythonThreads]
public static Type GetClrType(Type type)
{
return type;
}

[ModuleFunction]
[ForbidPythonThreads]
public static string FindAssembly(string name)
Expand Down
20 changes: 20 additions & 0 deletions src/tests/test_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,26 @@ def test_clr_add_reference():
with pytest.raises(FileNotFoundException):
AddReference("somethingtotallysilly")

def test_clr_get_clr_type():
"""Test clr.GetClrType()."""
from clr import GetClrType
import System
from System import IComparable
from System import ArgumentException
assert GetClrType(System.String).FullName == "System.String"
comparable = GetClrType(IComparable)
assert comparable.FullName == "System.IComparable"
assert comparable.IsInterface
assert GetClrType(int).FullName == "System.Int32"
assert GetClrType(str).FullName == "System.String"
assert GetClrType(float).FullName == "System.Double"
dblarr = System.Array[System.Double]
assert GetClrType(dblarr).FullName == "System.Double[]"

with pytest.raises(TypeError):
GetClrType(1)
with pytest.raises(TypeError):
GetClrType("thiswillfail")

def test_assembly_load_thread_safety():
from Python.Test import ModuleTest
Expand Down

0 comments on commit 2e44f8a

Please sign in to comment.