简体   繁体   English

如何在仅在基类中实现的继承类中调用InvokeMember

[英]How do I InvokeMember in inherited class that is only implemented in base

The scenario is I am changing the status of a "request" and sometimes the new status is transitory and needs to immediately change to another status. 场景是我正在更改“请求”的状态,有时新状态是暂时的,需要立即更改为另一状态。 So I'm in a method in my base class, WorkflowCommandBase: 因此,我处于基类WorkflowCommandBase中的一个方法中:

    public virtual Request Execute()
    {
        ChangeRequestStatus();
        QueueEmails();
        TrackRequestStatus();
        return ExecuteAutoTransitionStatus(GetTransitionStatus());
    }

The last line tries to execute the method again for the new status (from the inherited class, calledType), but the method I am calling doesn't exist in calledType, it is implemented here in the base. 最后一行尝试针对新状态再次执行该方法(来自继承的类namedType),但是我调用的方法在namedType中不存在,它在基础中实现。 It is possible to do this? 有可能这样做吗? Or do I need to override the Execute method in the inherited classes when this situation exists? 还是在这种情况下需要在继承的类中重写Execute方法?

private Request ExecuteAutoTransitionStatus(string commandName)
{
    if (String.IsNullOrEmpty(commandName))
    {
        return Request;
    }

    Type calledType = Type.GetType(commandName);
    return (Request)calledType.InvokeMember(
           "Execute",
           BindingFlags.InvokeMethod
                | BindingFlags.Public | BindingFlags.Instance,
           null,
           null,
           new Object[]
               {Request, TarsUser, GrantRepository, TrackingRepository});
    }

Here's part of the class diagram, if it helps. 如果有帮助,这是类图的一部分。

类图 .

Well, based on your comments there may be a discontinuity from what you have written in your question and what you are trying to do. 好吧,根据您的评论,您在问题中所写的内容和尝试做的内容可能会出现间断。 So I'll try and cover all the bases. 因此,我将尝试涵盖所有基础。

If the Execute method is static and defined on a base class you need to add the BindingFlags.FlattenHierarchy to your binding flags: 如果Execute方法是静态的并且在基类上定义,则需要将BindingFlags.FlattenHierarchy添加到绑定标志中:

 BindingFlags.InvokeMethod | BindingFlags.Public
 | BindingFlags.Static | BindingFlags.FlatternHierarchy

This seems the most likely problem because you say that it isn't finding the method, but this flag is only effective if you are searching for a static member. 这似乎是最可能出现的问题,因为您说它找不到方法,但是此标志仅在搜索静态成员时才有效。

On the other hand if the method isn't static then the binding flags that you have are correct, but you'll need an instance of the type to run it against. 另一方面,如果该方法不是静态的,则您具有的绑定标志是正确的,但是您需要使用该类型的实例来对其进行操作。 Depending on how complicated it is to construct your concrete type you may be able to get away with just using the Activator class, otherwise you may need to create a factory of some kind. 根据构造具体类型的复杂程度,您可能只需要使用Activator类就可以摆脱困境 ,否则您可能需要创建某种工厂。

@Martin: Thanks for your help. @马丁:谢谢你的帮助。

I'm posting this in case it might help someone else. 我发布此信息以防其他人使用。

    private Request ExecuteAutoTransition(Type newStatus)
    {
        // No transition needed
        if (newStatus == null)
        {
            return Request;
        }

        // Create an instance of the type
        object o = Activator.CreateInstance(
            newStatus, // type
            new Object[] // constructor parameters
                {
                    Request,
                    TarsUser,
                    GrantRepository,
                    TrackingRepository
                });

        // Execute status change
        return (Request) newStatus.InvokeMember(
                             "Execute", // method
                             BindingFlags.Public 
                                | BindingFlags.Instance 
                                | BindingFlags.InvokeMethod, // binding flags
                             Type.DefaultBinder, // binder
                             o, // type
                             null); // method parameters
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在继承的 class 的规格中包含基本 class 的规格? - How do I include specs for a base class in the specs for an inherited class? C#-当每个继承的类都需要基类的getter但只对一个继承的类进行setter时,我该怎么办 - C# - What should I do when every inherited class needs getter from base class, but setter only for ONE inherited class 如何使用从基类继承的事件在抽象类中定义EventHandler? - How do I define an EventHandler in an abstract class using an event inherited from a base class? 如何使基类方法的属性适用于继承的类? - How do I make attributes on a base class method apply in the inherited class? 如何重写从基类继承的方法,该方法又从接口实现该方法? - How to override a method inherited from a base class, which in turn implemented it from an interface? 如何在继承的类上使用运算符重载以匹配C#中的基本运算符 - How do I use operator overload on an inherited class to match the base operator in c# 如何在C#中对继承的基本构造函数进行操作? - How do I do operations on an inherited base constructor in C#? 如何将对象转换为继承的类? - How do I cast object to inherited class? 您如何使用WebKitBrowser调用会员? - How do you InvokeMember with WebKitBrowser? 我如何找到实现接口的类 - How do I find the class that implemented an interface
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM