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

妖精的绣舞 提交于 2020-01-13 20:39:05

问题


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?


回答1:


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])  


来源:https://stackoverflow.com/questions/49995729/python-for-net-how-to-call-a-method-of-a-static-class-using-reflection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!