2009-06-18 20 views
0

该方案是我正在更改“请求”的状态,有时新状态是暂时的,需要立即更改为其他状态。所以我在我的基类中的方法,WorkflowCommandBase:如何在仅基于实施的继承类中调用成员

最后一行再次尝试新状态执行方法(从继承的类,calledType),但该方法我打电话没有按不存在于calledType中,它在这里被实现。有可能做到这一点?或者当这种情况存在时,我需要重写继承类中的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}); 
    } 

这是类图的一部分,如果有帮助的话。

Class Diagram http://i44.tinypic.com/fxzcc7.png

+0

在您的问题中是否存在拼写错误?您正在寻找一个名为Execute的静态方法,但您显示的Execute方法不是静态的。 – 2009-06-18 13:12:21

回答

1

好吧,根据您的意见,您可能会在您的问题中写下的内容与您正在尝试做的事情不一致。所以我会尽力涵盖所有的基础。

如果Execute方法是静态的,你需要的BindingFlags.FlattenHierarchy添加到您的绑定标志一个基类中定义:

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

这似乎是最有可能的问题,因为你说这是不是找到该方法,但此标志仅在搜索静态成员时才有效。

另一方面,如果方法不是 static,那么您拥有的绑定标志是正确的,但是您需要一个类型的实例来运行它。根据构建混凝土类型的复杂程度,您可能只能使用Activator类别,否则您可能需要创建某种工厂。

+0

谢谢。我做这一切都是错误的。参数是针对类的,而不是方法。我需要使用这些参数创建一个类型的实例,然后调用该方法。谢谢,这表明我正确的方向。 – Leslie 2009-06-18 13:39:37

1

@Martin:谢谢你的帮助。

我发布此信息以防万一它可能帮助别人。

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 
    } 
相关问题