3

I want to use a method of a static class.

This is my C# code:

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

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

    }

If it was a "normal" class I could use SomeMethod like

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 the static class I get

MissingMethodException: "Cannot create an abstract class.

How could I solve this?

denfromufa
  • 5,610
  • 13
  • 81
  • 138
Joe
  • 6,758
  • 2
  • 26
  • 47
  • 3
    static method is not a part of a class instance/object, i.e. you don't have to create an instance using constructor in order to invoke static method, here is the example of invoking static method https://stackoverflow.com/questions/3770256/trouble-invoking-static-method-using-reflection-and-c-sharp – Darjan Bogdan Apr 24 '18 at 07:30
  • I guess something like `type1.GetMethod("SomeMethod")` instead of `GetConstructor`. – Evk Apr 24 '18 at 07:34

1 Answers1

3

Thanks to the comments to the question I was able to find a solution using MethodBase.Invoke (Object, Object[])

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])  
Joe
  • 6,758
  • 2
  • 26
  • 47
  • More related info on this can be found in a similar question of mine https://stackoverflow.com/questions/49942487/python-for-net-how-to-explicitly-create-instances-of-c-sharp-classes-per-assem – Joe Apr 24 '18 at 13:39