C# MRDS:为什么处理程序是虚拟的?
熟悉微软机器人工作室的人可以解释一下为什么处理程序操作是虚拟的而有些设置为非虚拟的吗?
[ServiceHandler(ServiceHandlerBehavior.Concurrent)]
public virtual IEnumerator<ITask> GetHandler(Get get)
{
get.ResponsePort.Post(_state);
yield break;
}
vs
[ServiceHandler(ServiceHandlerBehavior.Concurrent)]
public IEnumerator<ITask> SubscribeHandler(Subscribe subscribe)
{
SubscribeRequestType request = subscribe.Body;
LogInfo("Subscribe request from: " + request.Subscriber);
// Use the Subscription Manager to handle the subscribers
yield return Arbiter.Choice(
SubscribeHelper(_submgrPort, request, subscribe.ResponsePort),
delegate(SuccessResult success)
{
// Send a notification on successful subscription so that the
// subscriber can initialize its own state
base.SendNotificationToTarget<Replace>(request.Subscriber, _submgrPort, _state);
},
delegate(Exception e)
{
LogError(null, "Subscribe failed", e);
}
);
yield break;
}
提前致谢。
Can somebody familiar with microsoft robotics studio please explain why the handler operations are virtual and some are set as non-virtual ?
[ServiceHandler(ServiceHandlerBehavior.Concurrent)]
public virtual IEnumerator<ITask> GetHandler(Get get)
{
get.ResponsePort.Post(_state);
yield break;
}
vs
[ServiceHandler(ServiceHandlerBehavior.Concurrent)]
public IEnumerator<ITask> SubscribeHandler(Subscribe subscribe)
{
SubscribeRequestType request = subscribe.Body;
LogInfo("Subscribe request from: " + request.Subscriber);
// Use the Subscription Manager to handle the subscribers
yield return Arbiter.Choice(
SubscribeHelper(_submgrPort, request, subscribe.ResponsePort),
delegate(SuccessResult success)
{
// Send a notification on successful subscription so that the
// subscriber can initialize its own state
base.SendNotificationToTarget<Replace>(request.Subscriber, _submgrPort, _state);
},
delegate(Exception e)
{
LogError(null, "Subscribe failed", e);
}
);
yield break;
}
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
虚拟处理程序用在您希望用作其他类的基础的类中。非虚拟处理程序要么在派生类中声明为重写,要么在不会派生的类中声明为重写(示例代码中经常出现这种情况)。
请参阅以下文章:
http://msdn .microsoft.com/en-us/library/9fkccyh4(v=VS.100).aspx
什么是虚拟方法?
C# 虚拟方法问题
为什么应该在 C# 中显式重写虚拟方法?
Virtual handlers are used in classes you expect to use as base for other classes. Handlers that are not virtual are either declared as overrides in derived classes, or in classes that will not be derived (as is often the case in sample code).
Refer to the following articles:
http://msdn.microsoft.com/en-us/library/9fkccyh4(v=VS.100).aspx
What are Virtual Methods?
C# virtual methods question
WHy should virtual methods be explicitly overridden in C#?