C# / IronPython Interop with shared C# Class Library

后端 未结 2 1481
旧巷少年郎
旧巷少年郎 2021-02-03 14:24

I\'m trying to use IronPython as an intermediary between a C# GUI and some C# libraries, so that it can be scripted post compile time.

I have a Class library DLL that is

相关标签:
2条回答
  • 2021-02-03 15:13

    This error indicates that your assembly is getting loaded into multiple CLR loader contexts. Rather than adding the reference using clr.AddReferenceToFile you can either switch to clr.AddReference or you can load the assembly from C#. For the former you need to make sure that the assembly is available somewhere that .NET can normally load it (the GAC or in the application base of the process). For the latter you can just do:

    engine.Runtime.LoadAssembly(typeof(MyClass).Assembly);
    

    from your C# host code. Personally I like this 2nd solution a little bit more because not only does it work it saves your users from needing to do the clr.AddRef call from Python.

    0 讨论(0)
  • 2021-02-03 15:14

    You might try running your program under the debugger and breaking execution prior to the GetVariable call. Go to the "modules" window and see if there are two versions of your C# class library DLL loaded. If that's the case, then that's the explanation.

    If that's the problem, then the solution is to make sure C# and Python worlds agree on the types. One solution is to put everything in the same directory. Another possibility is to set up a reference to your class library in C# by using the properties on the ScriptScope class (I think) to set an assembly reference to your class library assembly that will be available to the Python code. I don't have a hybrid C#/IronPython project immediately available to test against, but I remember seeing that functionality.

    0 讨论(0)
提交回复
热议问题