0

I'm trying to use python to create an instance of a singleton class. The class looks like this:

public sealed class Test
{
    private Test()
    {

    }

    public static Test GetInstance()
    {
       .......
    }
}

I've tried everything from here:

Python for .NET: How to explicitly create instances of C# classes using different versions of the same DLL?

How can I create an instance of this class in python?

Nix
  • 3
  • 3
  • 1
    You can´t. It´s what a singleton is about: the framework guarantees that there´s ever only a single instance existing. So either don´t use singletons, or use that single instance using `Test.GetInstance()`. – MakePeaceGreatAgain Nov 07 '19 at 15:23
  • Sorry I do not understand now. I just want to create one instance and that with Python. Maybe I did not explain it well. I created an API with C #. This should also be usable with Pyhton. That should work. – Nix Nov 07 '19 at 20:21
  • it doesn't matter `clr.AddReference('Assemblypath') from Namespace import Test Test.GetInstance()` would work – Selvin Nov 12 '19 at 13:09
  • Not in my case. "from Namespace import" i get allways the error "No module named Namespace" – Nix Nov 12 '19 at 13:48

1 Answers1

0

I have found a solution.

#set reference
ref = clr.AddReference(r'Assemblypath')

#get type 
testType= ref.GetType('Namespace.Test')

#GetUninitializedObject
testUninitialize=System.Runtime.Serialization.FormatterServices.GetUninitializedObject(testType);

#Now it's possible to call the method
test = testUninitialize.GetInstance()
Nix
  • 3
  • 3