问题
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