Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 545
Joe Avatar asked May 19 '26 15:05

Joe


1 Answers

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])  
like image 89
Joe Avatar answered May 21 '26 04:05

Joe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!