C# 如何在仅在基类中实现的继承类中调用Member
场景是我正在更改“请求”的状态,有时新状态是暂时的,需要立即更改为其他状态。所以我在基类WorkflowCommandBase中的一个方法中:C# 如何在仅在基类中实现的继承类中调用Member,c#,inheritance,C#,Inheritance,场景是我正在更改“请求”的状态,有时新状态是暂时的,需要立即更改为其他状态。所以我在基类WorkflowCommandBase中的一个方法中: public virtual Request Execute() { ChangeRequestStatus(); QueueEmails(); TrackRequestStatus(); return ExecuteAutoTransitionStatus(GetTrans
public virtual Request Execute()
{
ChangeRequestStatus();
QueueEmails();
TrackRequestStatus();
return ExecuteAutoTransitionStatus(GetTransitionStatus());
}
最后一行尝试为新状态(从继承的类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});
}
这是类图的一部分,如果有帮助的话
.好吧,根据你的评论,你在问题中所写的内容和你想做的事情可能会有间断。所以我会尽力覆盖所有的基地 如果Execute方法是静态的,并且是在基类上定义的,则需要将BindingFlags.FlatterHierarchy添加到绑定标志中:
BindingFlags.InvokeMethod | BindingFlags.Public
| BindingFlags.Static | BindingFlags.FlatternHierarchy
这似乎是最可能出现的问题,因为您说它没有找到方法,但此标志仅在您搜索静态成员时有效
另一方面,如果该方法不是静态的,那么您拥有的绑定标志是正确的,但是您需要该类型的实例来运行它。根据构造具体类型的复杂程度,您可能不需要使用类,否则您可能需要创建某种工厂。好吧,根据您的评论,您在问题中所写的内容和您正在尝试的内容可能存在间断。所以我会尽力覆盖所有的基地 如果Execute方法是静态的,并且是在基类上定义的,则需要将BindingFlags.FlatterHierarchy添加到绑定标志中:
BindingFlags.InvokeMethod | BindingFlags.Public
| BindingFlags.Static | BindingFlags.FlatternHierarchy
这似乎是最可能出现的问题,因为您说它没有找到方法,但此标志仅在您搜索静态成员时有效
另一方面,如果该方法不是静态的,那么您拥有的绑定标志是正确的,但是您需要该类型的实例来运行它。根据构造具体类型的复杂程度,您可能只需要使用该类就可以成功,否则您可能需要创建某种工厂。@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
}
@马丁:谢谢你的帮助 我把这篇文章发出去,以防对其他人有所帮助
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
}
你的问题有错吗?您正在寻找一个名为Execute的静态方法,但您显示的Execute方法不是静态的。您的问题中是否有输入错误?您正在寻找一个名为Execute的静态方法,但是您显示的Execute方法不是静态的。谢谢。我做错了。参数用于类,而不是方法。我需要使用这些参数创建该类型的实例,然后调用该方法。谢谢,这为我指明了正确的方向。谢谢。我做错了。参数用于类,而不是方法。我需要使用这些参数创建该类型的实例,然后调用该方法。谢谢,这为我指明了正确的方向。