实现 GetMethodProperty

重要

在 Visual Studio 2015 中,这种实现表达式计算器的方法已弃用。 有关实现 CLR 表达式计算器的信息,请参阅 CLR 表达式计算器托管表达式计算器示例

Visual Studio 调用调试引擎的 GetDebugProperty,后者又调用 GetMethodProperty 以获取有关堆栈帧上当前方法的信息。

此实现执行 IDebugExpressionEvaluator::GetMethodProperty 以下任务:

  1. 调用 GetContainerField,传入 IDebugAddress 对象。 符号提供程序 (SP) 返回一个 IDebugContainerField ,表示包含指定地址的方法。

  2. 从 .IDebugContainerField. 获取 IDebugMethodField

  3. 实例化一个类(在此示例中调用 CFieldProperty ),该类实现 IDebugProperty2 接口,并包含 IDebugMethodField 从 SP 返回的对象。

  4. IDebugProperty2 对象返回 CFieldProperty 接口。

托管代码

此示例演示托管代码的 IDebugExpressionEvaluator::GetMethodProperty 实现。

namespace EEMC
{
    [GuidAttribute("462D4A3D-B257-4AEE-97CD-5918C7531757")]
    public class EEMCClass : IDebugExpressionEvaluator
    {
        public HRESULT GetMethodProperty(
                IDebugSymbolProvider symbolProvider,
                IDebugAddress        address,
                IDebugBinder         binder,
                int                  includeHiddenLocals,
            out IDebugProperty2      property)
        {
            IDebugContainerField containerField = null;
            IDebugMethodField methodField       = null;
            property = null;

            // Get the containing method field.
            symbolProvider.GetContainerField(address, out containerField);
            methodField = (IDebugMethodField) containerField;

            // Return the property of method field.
            property = new CFieldProperty(symbolProvider, address, binder, methodField);
            return COM.S_OK;
        }
    }
}

非托管代码

此示例演示非托管代码中的实现 IDebugExpressionEvaluator::GetMethodProperty

[CPP]
STDMETHODIMP CExpressionEvaluator::GetMethodProperty(
        in IDebugSymbolProvider *pprovider,
        in IDebugAddress        *paddress,
        in IDebugBinder         *pbinder,
        in BOOL                  includeHiddenLocals,
        out IDebugProperty2    **ppproperty
    )
{
    if (pprovider == NULL)
        return E_INVALIDARG;

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

    HRESULT hr;
    IDebugContainerField* pcontainer = NULL;

    hr = pprovider->GetContainerField(paddress, &pcontainer);
    if (FAILED(hr))
        return hr;

    IDebugMethodField*    pmethod    = NULL;
    hr = pcontainer->QueryInterface( IID_IDebugMethodField,
            reinterpret_cast<void**>(&pmethod));
    pcontainer->Release();
    if (FAILED(hr))
        return hr;

    CFieldProperty* pfieldProperty = new CFieldProperty( pprovider,
                                                         paddress,
                                                         pbinder,
                                                         pmethod );
    pmethod->Release();
    if (!pfieldProperty)
        return E_OUTOFMEMORY;

    hr = pfieldProperty->Init();
    if (FAILED(hr))
    {
        pfieldProperty->Release();
        return hr;
    }

    hr = pfieldProperty->QueryInterface( IID_IDebugProperty2,
            reinterpret_cast<void**>(ppproperty));
    pfieldProperty->Release();

    return hr;
}