用于.NET的Python-创建单例的实例

问题描述 投票:0回答:1

我正在尝试使用python创建单例类的实例。该类如下所示:

public sealed class Test
{
    private Test()
    {

    }

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

我已经尝试过这里的所有内容:

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

如何在python中创建此类的实例?

c# python
1个回答
0
投票

我找到了解决方法。

#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()
© www.soinside.com 2019 - 2024. All rights reserved.