How to use a C# dll in IronPython

后端 未结 6 790
轻奢々
轻奢々 2020-12-03 05:18

I have created a dll using C#. How do use the dll in IronPython. I have tried to add the dll using clr.AddReference(\"yxz.dll\"). But it fails. I have tried pla

6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-03 06:14

    I got this behaviour only from the IronPython console. When I run a script it is fine. When I run a script the IronPython, sys.path contains an absolute path to my current working directory so it works. When I type in the console, sys.path only includes a '.' for the current working directory. That may explain the difference in behaviour.

    As a bit of a hacky solution, I created a file fixpath.py

    """This hacky script fixes the sys.path when I run the ipy console."""
    
    import sys
    import os
    
    sys.path.insert(0, os.getcwd())
    
    del sys
    del os
    

    Then I set up an environment variable IRONPYTHONSTARTUP with the absolute path to this file. Then whenever I start my IronPython console, this script is run and my sys.path includes an absolute reference to my current working directory and the subsequent calls to clr.AddReference work properly.

提交回复
热议问题