I have a .cs file like
namespace SomeNamepace
{
public struct SomeStruct
{
....
}
public static class SomeClass
{
....
}
So far I use it with PythonNET like
import clr
clr.AddReference('c:\\Test\Module.dll')
from SomeNamepace import SomeClass, SomeStruct
SomeClass.SomeMethod(...)
My problem is now that I need to work with dlls with identical names and no version number set, so PythonNET will not see them as two different dlls but as the same. Even if I import them using the full path with AddReference.
Now I would like to use them explicitly as stated here:
Python for .NET: Using same .NET assembly in multiple versions
like
lib = clr.AddReference('c:\\Test\Module.dll')
I tried a lot of things to create an instance of SomeClass
like
lib.SomeNamespace.SomeClass()
or
import System
System.Activator.CreateInstance(lib.GetType('SomeNamespace.SomeClass'))
or using the the methods Initialize
or CreateInstance
or as mentioned in the link below
from System import Type
type1 = lib.GetType('SomeNamespace.SomeClass')
constructor1 = type1.GetConstructor(Type.EmptyTypes)
In the end everything failed, something not found, has no method, etc. etc.
What would be the correct syntax to do this?
I found an old mailing list conversation that might explain it
The solution posted there does not work anymore, I guess the .NET function has been depracated. But there is a solution to this. Instead of using PythonNet you must use the .NET framework directly:
Check that the correct DLL is used.
To use multiple DLLs with the same version just load it to another variable
Now you can use objects from the specified dll.
Instance of a public non-static class
Calling a method in a public static class
Creating a instance of a struct
From the docs on LoadFile
Methods that don't work:
adding references to other DLLs with different versions using
dll_ref = clr.AddReference(f)
does not work, even if the file name is specified explicitly anddll_ref
and Reflection are used to get the methodsusing a longname (like
'MyNamespace, Version=1.0.0.1, Culture=neutral, PublicKeyToken=null'
with System.Reflection.Assembly.Load with the version will still use the first versionSystem.Reflection.Assembly.LoadFrom
does not work with multiple version of the same DLL if an assembly with the same identity is already loaded. LoadFrom returns the loaded assembly even if a different path was specified. (Doc LoadFrom)