[英]Call non-static member of static property in static class with reflection
I have a static class which looks for an implementation of an abstract type and stores it as a static property similar to the below: 我有一个静态类,用于寻找抽象类型的实现,并将其存储为类似于以下内容的静态属性:
public static class MyStaticClass
{
private static MyAbstractType _MyAbstractImplementation;
public static MyAbstractType MyAbstractImplementation
{
get => _MyAbstractImplementation ?? ( _MyAbstractImplementation = FindImplementation());
private set => _MyAbstractImplementation = value;
}
}
And I am trying to call a method of MyAbstractImplementation (which contains no static properties or methods) via reflection: 我试图通过反射调用MyAbstractImplementation的方法(其中不包含静态属性或方法):
var myAssembly = Assembly.Load("MyStaticClassAssembly")
var myType = myAssembly.GetTypes().First(t => t.Name == "MyAbstractType");
var myImplementation = myType.GetProperties()
.First(p=>p.ReflectedType?.Name == "MyAbstractImplementation")
.GetValue(null);
var obj = myType.GetMethod("SomeMethod")?.Invoke(
null,
new object[]
{
// Some args
});
The above code causes the following exception on getting the value of MyAbstractImplementation
: 上面的代码在获取
MyAbstractImplementation
的值时导致以下异常:
System.Reflection.TargetException: Non-static method requires a target.
Evidently, this is because I am passing null to GetValue()
, so I try passing myAssembly
rather than null and I get the following exception: 显然,这是因为我将null传递给
GetValue()
,所以我尝试传递myAssembly
而不是null,并且得到以下异常:
System.Reflection.TargetException: Object does not match target type.
Out of desperation, I try passing myType
and myImplementation
but I still get the same exception. 出于绝望,我尽量传递
myType
和myImplementation
,但我仍然得到同样的异常。
What am I meant to be passing to GetValue()
? 我要传递给
GetValue()
意思是什么?
From the error you are getting MyAbstractImplementation
is not a static property, so it needs an instance to be run on. 从错误中您将获得
MyAbstractImplementation
不是静态属性,因此需要在其上运行实例。 You are basically trying to write the following code: 您基本上是在尝试编写以下代码:
new MyAbstractType().MyAbstractImplementation.SomeMethod();
Both the property and method access need the target to run against (the left side of '.'). 属性和方法访问都需要目标运行(“。”的左侧)。 So you need an instance of
myType
. 因此,您需要一个
myType
实例。 Also the method will need the instance, which is the result of getting the property ( myImplementation
). 该方法还需要实例,这是获得属性(
myImplementation
)的结果。
var myAssembly = Assembly.Load("MyStaticClassAssembly");
var myType = myAssembly.GetTypes().First(t => t.Name == "MyAbstractType");
var myTypeInstance = Activator.CreateInstance(myType); // Asuming has a default constructor
var myImplementation = myType.GetProperties()
.First(p => p.ReflectedType?.Name == "MyAbstractImplementation")
.GetValue(myTypeInstance);
var obj = myType.GetMethod("SomeMethod")?.Invoke(
myImplementation,
new object[]
{
// Some args
});
From how you write the code myImplementation
should also be of type myType
( myType.GetMethod("SomeMethod")
) if it isn't replace it with: myImplementation.GetType().GetMethod("SomeMethod")
. 从你如何编写代码
myImplementation
也应该是类型myType
( myType.GetMethod("SomeMethod")
如果它不将其替换为: myImplementation.GetType().GetMethod("SomeMethod")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.