C#MRDS:为什么处理程序是虚拟的?

时间:2011-09-01 00:28:01

标签: c#-3.0 virtual-method robotics-studio

有人熟悉微软机器人工作室吗请解释为什么处理程序操作是虚拟的,有些是非虚拟的?

[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;
}

提前致谢。

1 个答案:

答案 0 :(得分:0)

虚拟处理程序用在您希望用作其他类的基础的类中。非虚拟处理程序要么在派生类中声明为覆盖,要么在未派生的类中声明(在示例代码中通常就是这种情况)。

请参阅以下文章:

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#?