0

I'm trying to use code from a C# .dll in Python.

The class has the constructor protected, so the only way to get the instance is by calling the static method Instance (see source code sample below).

In python, I'm using clr library to load the .dll, and I tried to invoke the instance like this:

import clr

clr.AddReference("Common.Namespace")
from CommonNamespace import Common

instance = Common.Instance

but I get the following error:

TypeError: Exception has been thrown by the target of an invocation.

I'm trying to resolve this for quite some time - with different approaches as well - but without any luck...

Sample of the source code from my .dll:

namespace CommonNamespace
{
  public class Common
  {
    private static Common instance;

    public static Common Instance
    {
      get
      {
        if (Common.instance == null)
          Common.instance = new Common();
        return Common.instance;
      }
    }

    protected Common()
    {
    }
  }
}

Could somebody help me?

Thank you!

Note: I want to call a static method of a non-static class, while the constructor is protected

s_7
  • 33
  • 1
  • 1
  • 6
  • Possible duplicate of [Python for .NET: How to call a method of a static class using Reflection?](https://stackoverflow.com/questions/49995729/python-for-net-how-to-call-a-method-of-a-static-class-using-reflection) – quamrana Nov 11 '19 at 11:38
  • I think it's not a duplication because my constructor is protected, so I cannot get its constructor with GetConstructor(). Also, I want to call a static method of a non-static class, which is different from the other question. – s_7 Nov 11 '19 at 12:49
  • Can you verify that the syntax is correct on the python side by creating some demo C# code with a static method of a class returning something like an `int`? (So not a 'getter' method). – quamrana Nov 11 '19 at 14:00

0 Answers0