python.netpython-3.xpython.net

How to get available modules with pythonnet


I'm trying to import a .net dll into python and would like to figure out how to see what modules are available.

import clr
clr.AddReference("System.Windows.Forms")
from System.Windows.Forms import Form

After the clr.AddReference line how can I see what is available to import? I would like to know that System.Windows.Forms is available and that Form is available.

I have an internal .net dll I am trying to work with, and couldn't figure out how to see what the modules I needed to call without an example. Once I got this point, I can see what's available with something like

form = Form()
print(dir(form))

I was able to get the info I needed with the following code:

dll_ref = System.Reflection.Assembly.LoadFile(full_path)
print(dll_ref.FullName)
print(dll_ref.Location)
for i in range(len(dll_ref.DefinedTypes)):
    print(dll_ref.DefinedTypes[i])

Solution

  • There are tools that can show you members in .NET DLL. One is Object Explorer in Visual Studio. There is also a free tool from JetBrains called dotPeek.

    If you want to do it from Python, you need to either dir on a namespace that you must know in advance, or use .NET reflection to inspect the DLL programmatically.