使用反射调用静态方法时返回错误值?

发布于 2024-10-29 00:42:00 字数 1086 浏览 1 评论 0原文

我有两种类型,称为 EffectEffectMethods,这是我调用其方法的静态类:

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 } );
    }
}

我得到了错误的结果。这里 thisEffect 的当前实例,因为它是包含反射调用的类型。

基本上,我计算的值之一是返回操作是否成功的标志。但是代码中这个值设置为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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

静谧幽蓝 2024-11-05 00:42:00

我不明白为什么它应该返回“坏值”。你没有提供完整的代码,所以我只能给你我的两个猜测。

  1. EffectResult的构造函数中,您忘记将success参数设置为属性,或者属性实现错误。

  2. 用于获取方法的 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.

  1. In the constructor of EffectResult, you forgot to set the success parameter to a property, or the property implementation is wrong.

  2. The Type you use to get the methods from is other than EffectMethods or you have duplicate assemblies with different implementations in your AppDomain. Check the loaded Modules.

贵在坚持 2024-11-05 00:42:00

您可以发布更多代码吗?我根据您所显示的代码猜测一些定义。使用我猜测的定义我没有问题,当然我假设只有一个公共静态方法和一些基本定义等。

不过,对于这些类或骨架来说,查看实际代码会更有帮助。但使用这些,它是有效的。

using System;
using System.Reflection;

public enum EffectType
{
    A,
    B
}

public class Effect
{
    public EffectType Type { get; set; }
}

public class EffectResult
{
    public EffectType Type { get; set; }
    public bool Success { get; set; }

    public EffectResult(EffectType type, bool success)
    {
        Type = type;
        Success = success;
    }
}

public class EffectMethods
{
    public static EffectResult Blend(Effect effect)
    {
        bool success = true;
        return new EffectResult(effect.Type, success);
    }
}

public static class Program
{
    public static void Main()
    {
        var methods = typeof (EffectMethods).GetMethods(BindingFlags.Public | BindingFlags.Static);

        var result = methods[0].Invoke(null, new object[] { new Effect { Type = EffectType.A } });

        Console.WriteLine(result);
    }
}

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.

using System;
using System.Reflection;

public enum EffectType
{
    A,
    B
}

public class Effect
{
    public EffectType Type { get; set; }
}

public class EffectResult
{
    public EffectType Type { get; set; }
    public bool Success { get; set; }

    public EffectResult(EffectType type, bool success)
    {
        Type = type;
        Success = success;
    }
}

public class EffectMethods
{
    public static EffectResult Blend(Effect effect)
    {
        bool success = true;
        return new EffectResult(effect.Type, success);
    }
}

public static class Program
{
    public static void Main()
    {
        var methods = typeof (EffectMethods).GetMethods(BindingFlags.Public | BindingFlags.Static);

        var result = methods[0].Invoke(null, new object[] { new Effect { Type = EffectType.A } });

        Console.WriteLine(result);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文