Поделиться через


Реализация GetMethodProperty

Важно!

В Visual Studio 2015 такая реализация вычислителя выражений была сделана нерекомендуемой. Сведения о реализации вычислителей выражений в среде CLR см. на страницах CLR expression evaluators (Вычислители выражений CLR) и Managed expression evaluator sample (Пример управляемого вычислителя выражений).

Visual Studio вызывает обработчик отладки (DE) GetDebugProperty, который, в свою очередь, вызывает GetMethodProperty для получения сведений о текущем методе в кадре стека.

Эта реализация IDebugExpressionEvaluator::GetMethodProperty выполняет следующие задачи:

  1. Вызывает GetContainerField, передавая объект IDebugAddress . Поставщик символов возвращает идентификатор IDebugContainerField , представляющий метод, содержащий указанный адрес.

  2. Получает IDebugMethodField из .IDebugContainerField

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