C# MRDS: Why are the handlers virtual?
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 ad开发者_如何学运维vance.
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#?
精彩评论