Introspecting .NET Types and Methods from IronPython

For Python methods you can use the inspect module to get information about methods / functions; like what arguments they take and so on.

Methods on .NET types don't have the attributes used by inspect (although methods of Python objects defined in IronPython work fine). We can get around this by using .NET reflection to introspect them them.

Reflection is normally done with classes from the System.Reflection namespace. We don't have to use these classes directly, but get them indirectly.

Here's an example of getting information about the 'OpenStandardOutput' method of the System.Console class. We can then get the return type (something not available in Python) and the names of all the parameters.

>>> import clr >>> from System import Console >>> >>> clr_type = clr.GetClrType(Console) >>> all_methods = clr_type.GetMethods >>> method_list = [m for m in all_methods if m.Name == 'OpenStandardOutput'] >>> len(method_list) 2 >>> # Two overloads!! >>> >>> for method in method_list: ...  return_type = method.ReturnType ...  print return_type.Namespace, return_type.Name ...  params = method.GetParameters ...  param_names = [p.Name for p in params] ...  if not method.IsStatic: ...    param_names = ['self'] + param_names ...  print param_names ...  print ... System.IO Stream []

System.IO Stream ['bufferSize']

>>>

Note that most .NET types and methods do have useful docstrings. You access these normally through the __doc__ attribute, but they are pulled in from the .NET XML documentation by IronPython.

This technique uses classes like:


 * System.Type
 * System.Reflection.ParameterInfo
 * System.Reflection.MethodInfo

and so on. These have useful members for exploring the methods of .NET types.

Back to Contents.