8000 Support clr.GetClrType() - as in IronPython by vivainio · Pull Request #433 · pythonnet/pythonnet · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
8000
1 change: 1 addition & 0 deletions AUTHORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,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
20 changes: 20 additions & 0 deletions src/runtime/moduleobject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,26 @@ 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(object type)
{
var converted = type as Type;
if (converted == null)
{
throw new ArgumentException("Cannot convert object to Type");
}
return converted;
}

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

def test_clr_get_clr_type():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add the test to invoke the ArgumentException error?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

"""Test clr.GetClrType()."""
from clr import GetClrType
from System import String
from System import IComparable
from System import ArgumentException

assert GetClrType(String).FullName == "System.String"
comparable = GetClrType(IComparable)
assert comparable.FullName == "System.IComparable"
assert comparable.IsInterface

with pytest.raises(ArgumentException):
GetClrType("thiswillfail")

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