共用方式為


評估 [監看運算式

Visual Studio 準備好要顯示監看運算式的值時,它會呼叫IDebugExpression2::EvaluateSync ,依序呼叫IDebugParsedExpression::EvaluateSync。 這會產生IDebugProperty2物件,其中包含的值和運算式的型別。

在此實作中的IDebugParsedExpression::EvaluateSync,運算式會評估一次。 這個實作會執行下列工作:

  1. 剖析並評估運算式,以產生的值,其型別會保留泛用物件。 在 C# 中,這以表示object而在 c + + 中,這表示為VARIANT。

  2. 具現化類別 (稱為CValueProperty在本例中) 實作IDebugProperty2介面,並以類別來儲存要傳回的值。

  3. 傳回IDebugProperty2介面從CValueProperty物件。

Managed 程式碼

這是實作的IDebugParsedExpression::EvaluateSync在 managed 程式碼。 Helper 方法Tokenize會剖析成剖析樹狀目錄的運算式。 Helper 函式EvalToken將語彙基元轉換成值。 Helper 函式FindTerm以遞迴方式周遊的剖析樹狀目錄中,呼叫EvalToken的每一個節點代表一個值,並套用運算式中的任何作業 (加或乘)。

namespace EEMC
{
    public class CParsedExpression : IDebugParsedExpression
    {
        public HRESULT EvaluateSync(
            uint evalFlags,
            uint timeout,
            IDebugSymbolProvider provider,
            IDebugAddress address,
            IDebugBinder binder,
            string resultType,
            out IDebugProperty2 result)
        {
            HRESULT retval = COM.S_OK;
            this.evalFlags = evalFlags;
            this.timeout = timeout;
            this.provider = provider;
            this.address = address;
            this.binder = binder;
            this.resultType = resultType;

            try
            {
                IDebugField field = null;
                // Tokenize, then parse.
                tokens = Tokenize(expression);
                result = new CValueProperty(
                             expression,
                             (int) FindTerm(EvalToken(tokens[0], out field),1),
                             field,
                             binder);
            }
            catch (ParseException)
            {
                result = new CValueProperty(expression, "Huh?");
                retval = COM.E_INVALIDARG;
            }
            return retval;
        }
    }
}

Unmanaged 程式碼

這是實作的IDebugParsedExpression::EvaluateSync unmanaged 程式碼中。 Helper 函式Evaluate剖析並評估運算式,並傳回VARIANT持有所產生的值。 Helper 函式VariantValueToProperty配搭VARIANT到CValueProperty物件。

[C++]
STDMETHODIMP CParsedExpression::EvaluateSync( 
    in  DWORD                 evalFlags,
    in  DWORD                 dwTimeout,
    in  IDebugSymbolProvider* pprovider,
    in  IDebugAddress*        paddress,
    in  IDebugBinder*         pbinder,
    in  BSTR                  bstrResultType,
    out IDebugProperty2**     ppproperty )
{
    // dwTimeout parameter is ignored in this implementation.
    if (pprovider == NULL)
        return E_INVALIDARG;

    if (paddress == NULL)
        return E_INVALIDARG;

    if (pbinder == NULL)
        return E_INVALIDARG;

    if (ppproperty == NULL)
        return E_INVALIDARG;
    else
        *ppproperty = 0;

    HRESULT hr;
    VARIANT value;
    BSTR    bstrErrorMessage = NULL;
    hr = ::Evaluate( pprovider,
                     paddress,
                     pbinder,
                     m_expr,
                     &bstrErrorMessage,
                     &value );
    if (hr != S_OK)
    {
        if (bstrErrorMessage == NULL)
            return hr;

        //we can display better messages ourselves.
        HRESULT hrLocal = S_OK;
        VARIANT varType;
        VARIANT varErrorMessage;

        VariantInit( &varType );
        VariantInit( &varErrorMessage );
        varErrorMessage.vt      = VT_BSTR;
        varErrorMessage.bstrVal = bstrErrorMessage;

        CValueProperty* valueProperty = new CValueProperty();
        if (valueProperty != NULL)
        {
            hrLocal = valueProperty->Init(m_expr, varType, varErrorMessage);
            if (SUCCEEDED(hrLocal)) 
            {
                hrLocal = valueProperty->QueryInterface( IID_IDebugProperty2,
                        reinterpret_cast<void**>(ppproperty) );
            }
        }

        VariantClear(&varType);
        VariantClear(&varErrorMessage); //frees BSTR
        if (!valueProperty)
            return hr;
        valueProperty->Release();
        if (FAILED(hrLocal))
            return hr;
    }
    else
    {
        if (bstrErrorMessage != NULL)
            SysFreeString(bstrErrorMessage);

        hr = VariantValueToProperty( pprovider,
                                     paddress,
                                     pbinder,
                                     m_radix,
                                     m_expr,
                                     value,
                                     ppproperty );
        VariantClear(&value);
        if (FAILED(hr))
            return hr;
    }

    return S_OK;
}

請參閱

概念

監看式視窗運算式評估

運算式評估的實作範例