使用反射调用静态方法时返回错误值?
我有两种类型,称为 Effect
和 EffectMethods
,这是我调用其方法的静态类:
public class EffectMethods
{
public static EffectResult Blend (Effect effect)
{
bool success = true;
return new EffectResult ( effect.Type, success );
}
}
我使用以下方法找到正确的方法:
Type.GetMethods ( BindingFlags.Public | BindingFlags.Static );
并过滤出正确的方法。
但是当我调用它时:
( EffectResult ) method.Invoke ( null, new object [ ] { this } );
public class Effect
{
public EffectResult Apply()
{
var methods = Type.GetMethods ( BindingFlags.Public | BindingFlags.Static );
var method = methods.First ( ... );
// This result value is now different (success = false)
return ( EffectResult ) method.Invoke ( null, new object [ ] { this } );
}
}
我得到了错误的结果。这里 this
是 Effect
的当前实例,因为它是包含反射调用的类型。
基本上,我计算的值之一是返回操作是否成功的标志。但是代码中这个值设置为true,但是方法通过反射返回后结果却不同了。
我这样做错了吗?我有什么遗漏的吗?我可以清楚地看到方法内部的值为 true,但在调用站点上,它的显示有所不同。
I have 2 types called Effect
and EffectMethods
which is the static class I am calling the method of:
public class EffectMethods
{
public static EffectResult Blend (Effect effect)
{
bool success = true;
return new EffectResult ( effect.Type, success );
}
}
I find the right method using:
Type.GetMethods ( BindingFlags.Public | BindingFlags.Static );
and filter out the right one.
But when I call it:
( EffectResult ) method.Invoke ( null, new object [ ] { this } );
public class Effect
{
public EffectResult Apply()
{
var methods = Type.GetMethods ( BindingFlags.Public | BindingFlags.Static );
var method = methods.First ( ... );
// This result value is now different (success = false)
return ( EffectResult ) method.Invoke ( null, new object [ ] { this } );
}
}
I am getting the wrong result. Here this
is the current instance of Effect
because it's the type that includes the reflection call.
Basically one of the values I calculate is a flag that returns whether the operation is successful. But this value is set to true in code, but after the method returns via reflection the result is different.
Am I doing this wrong? Is there something I am missing? I can clearly see the value to be true inside the method but on the call site, it shows up differently.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不明白为什么它应该返回“坏值”。你没有提供完整的代码,所以我只能给你我的两个猜测。
在
EffectResult
的构造函数中,您忘记将success
参数设置为属性,或者属性实现错误。用于获取方法的
Type
不是EffectMethods
,或者您的 AppDomain 中有具有不同实现的重复程序集。检查加载的模块。I don't see why it should return "bad value". You didn't provide complete code, so I can only give you my two guesses.
In the constructor of
EffectResult
, you forgot to set thesuccess
parameter to a property, or the property implementation is wrong.The
Type
you use to get the methods from is other thanEffectMethods
or you have duplicate assemblies with different implementations in your AppDomain. Check the loaded Modules.您可以发布更多代码吗?我根据您所显示的代码猜测一些定义。使用我猜测的定义我没有问题,当然我假设只有一个公共静态方法和一些基本定义等。
不过,对于这些类或骨架来说,查看实际代码会更有帮助。但使用这些,它是有效的。
Can you post more code? I'm guessing on some of the definitions based on the code you're showing. Using my guessed definitions I have no problems, of course I'm assuming there's only one public static method and some basic definitions, etc.
Would be more helpful to see your actual code, though, for those classes or a skeleton. Using these though, it works.