Python for C# – .NET : How to call a method of a static class using Reflection?

Python for C# – .NET : How to call a method of a static class using Reflection? … here is a solution to the problem.

Python for C# – .NET : How to call a method of a static class using Reflection?

I want to use a method of a static class.

Here is my C# code:

namespace SomeNamepace
{
    public struct SomeStruct
    {
        ....
    }

public static class SomeClass
    {
        public static double SomeMethod
        {
            ....
        }

}

If it’s a “normal” class, I can use something like SomeMethod

lib = clr. AddReference('c:\\Test\Module.dll')
from System import Type
type1 = lib. GetType('SomeNamespace.SomeClass')
constructor1 = type1. GetConstructor(Type.EmptyTypes)  
my_instance = constructor1. Invoke([])  
my_instance. SomeMethod() 

But when trying to do this with a static class, I got it

MissingMethodException: "Cannot create an abstract class.

How do I fix this?

Solution

Thanks to the comment on the issue, I was able to use MethodBase.Invoke (Object, Object[]) to find the solution

lib = clr. AddReference('c:\\Test\Module.dll')
from System import Type
my_type = lib. GetType('SomeNamespace.SomeClass')
method = my_type. GetMethod('SomeMethod')  

# RetType is void in my case, so None works
RetType = None
# parameters passed to the functions need to be a list
method. Invoke(RetType, [param1, param2])  

Related Problems and Solutions