L’énumération des variables localesEnumerating Locals

Important

Dans Visual Studio 2015, ce moyen d’implémenter des évaluateurs d’expression est déconseillée.In Visual Studio 2015, this way of implementing expression evaluators is deprecated. Pour plus d’informations sur l’implémentation des évaluateurs d’expression CLR, consultez évaluateurs d’Expression CLR et exemple d’évaluateur d’Expression managé.For information about implementing CLR expression evaluators, please see CLR Expression Evaluators and Managed Expression Evaluator Sample.

Lorsque Visual Studio est prêt à remplir la variables locales fenêtre, elle appelle EnumChildren sur la IDebugProperty2 objet retourné à partir de GetMethodProperty (consultez GetMethodProperty d’implémentation).When Visual Studio is ready to populate the Locals window, it calls EnumChildren on the IDebugProperty2 object returned from GetMethodProperty (see Implementing GetMethodProperty). IDebugProperty2::EnumChildren Retourne un IEnumDebugPropertyInfo2 objet.IDebugProperty2::EnumChildren returns an IEnumDebugPropertyInfo2 object.

Cette implémentation de IDebugProperty2::EnumChildren effectue les tâches suivantes :This implementation of IDebugProperty2::EnumChildren performs the following tasks:

  1. Garantit que cela est représentant une méthode.Ensures this is representing a method.

  2. Utilise le guidFilter argument pour déterminer la méthode à appeler sur le IDebugMethodField objet.Uses the guidFilter argument to determine which method to call on the IDebugMethodField object. Si guidFilter est égal à :If guidFilter equals:

    1. guidFilterLocals, appelez EnumLocals pour obtenir un IEnumDebugFields objet.guidFilterLocals, call EnumLocals to obtain an IEnumDebugFields object.

    2. guidFilterArgs, appelez EnumArguments pour obtenir un IEnumDebugFields objet.guidFilterArgs, call EnumArguments to obtain an IEnumDebugFields object.

    3. guidFilterLocalsPlusArgs, synthétiser une énumération qui combine les résultats de IDebugMethodField::EnumLocals et IDebugMethodField::EnumArguments.guidFilterLocalsPlusArgs, synthesize an enumeration that combines the results from IDebugMethodField::EnumLocals and IDebugMethodField::EnumArguments. Cette synthèse est représentée par la classe CEnumMethodField.This synthesis is represented by the class CEnumMethodField.

  3. Instancie une classe (appelée CEnumPropertyInfo dans cet exemple) qui implémente le IEnumDebugPropertyInfo2 interface et contient le IEnumDebugFields objet.Instantiates a class (called CEnumPropertyInfo in this example) that implements the IEnumDebugPropertyInfo2 interface and contains the IEnumDebugFields object.

  4. Retourne le IEnumDebugProperty2Info2 de l’interface à partir de la CEnumPropertyInfo objet.Returns the IEnumDebugProperty2Info2 interface from the CEnumPropertyInfo object.

Code managéManaged Code

Cet exemple illustre une implémentation de IDebugProperty2::EnumChildren dans le code managé.This example shows an implementation of IDebugProperty2::EnumChildren in managed code.

namespace EEMC  
{  
    public class CFieldProperty : IDebugProperty2  
    {  
        public HRESULT EnumChildren (  
                uint                    dwFields,  
                uint                    radix,  
            ref Guid                    guidFilter,   
                ulong                   attribFilter,   
                string                  nameFilter,   
                uint                    timeout,  
            out IEnumDebugPropertyInfo2 properties)   
        {  
            properties = null;  
            IEnumDebugFields fields = null;  

            // If this field is a method...  
            if (0 != ((uint) fieldKind & (uint) FIELD_KIND.FIELD_TYPE_METHOD))  
            {  
                IDebugMethodField methodField = (IDebugMethodField) field;  

                // Enumerate parameters.  
                if (guidFilter == FilterGuids.guidFilterArgs)  
                {  
                    methodField.EnumParameters(out fields);    
                }  
                // Enumerate local variables.  
                else if (guidFilter == FilterGuids.guidFilterLocals)  
                {  
                    methodField.EnumLocals(address, out fields);    
                }  
                // Enumerate all local variables, including invisible compiler temps.  
                else if (guidFilter == FilterGuids.guidFilterAllLocals)  
                {  
                    methodField.EnumAllLocals(address, out fields);    
                }  
                // Enumerate "this", if any, and all parameters and local variables.  
                else if (guidFilter == FilterGuids.guidFilterLocalsPlusArgs)  
                {  
                    IDebugClassField fieldThis   = null;  
                    IEnumDebugFields parameters = null;  
                    IEnumDebugFields locals     = null;  

                    methodField.GetThis(out fieldThis);  
                    methodField.EnumParameters(out parameters);  
                    methodField.EnumLocals(address, out locals);  

                    CEnumMethodField enumMethodField =   
                        new CEnumMethodField(fieldThis, parameters, locals);  
                    fields = (IEnumDebugFields) enumMethodField;  
                }  
                // Enumerate only "this".  
                else if (guidFilter == FilterGuids.guidFilterThis)  
                {  
                    IDebugClassField fieldThis   = null;  
                    methodField.GetThis(out fieldThis);  

                    CEnumMethodField enumMethodField =   
                        new CEnumMethodField(fieldThis, null, null);  
                    fields = (IEnumDebugFields) enumMethodField;  
                }  
                else throw new COMException();// E_FAIL  
            }  
            // Wrap a property enumerator around the field enumerator.  
            CEnumPropertyInfo propertiesInfo =   
                new CEnumPropertyInfo(provider, address, binder, radix, fields,   
                 (DEBUGPROP_INFO_FLAGS) dwFields);  

            properties = (IEnumDebugPropertyInfo2) propertiesInfo;  
            return COM.S_OK;  
        }  
    }  
}  

Code non managéUnmanaged Code

Cet exemple illustre une implémentation de IDebugProperty2::EnumChildren en code non managé.This example shows an implementation of IDebugProperty2::EnumChildren in unmanaged code.

STDMETHODIMP CFieldProperty::EnumChildren(   
        in DEBUGPROP_INFO_FLAGS        infoFlags,  
        in DWORD                       radix,  
        in REFGUID                     guidFilter,  
        in DBG_ATTRIB_FLAGS            attribFilter,  
        in LPCOLESTR                   pszNameFilter,  
        in DWORD                       timeout,  
        out IEnumDebugPropertyInfo2 ** ppchildren )  
{  
    if (ppchildren == NULL)  
        return E_INVALIDARG;  
    else  
        *ppchildren = 0;  

    //get enumeration  
    HRESULT hr;  
    IEnumDebugFields*     pfields    = NULL;  

    if (m_fieldKind & FIELD_TYPE_METHOD)  
    {  
        //-----------------------------------------------------  
        // A Method  

        IDebugMethodField*  pmethod    = NULL;  

        //enumerate the requested properties  
        hr = m_field->QueryInterface( IID_IDebugMethodField,  
            reinterpret_cast<void**>(&pmethod) );  
        if (FAILED(hr))  
            return hr;  

        if (guidFilter == guidFilterArgs)  
        {  
            hr = pmethod->EnumParameters( &pfields );    
        }  
        else if (guidFilter == guidFilterLocals)  
        {  
            hr = pmethod->EnumLocals( m_address, &pfields );  
        }  
        else if (guidFilter == guidFilterAllLocals)  
        {  
            hr = pmethod->EnumAllLocals( m_address, &pfields );  
        }  
        else if (guidFilter == guidFilterLocalsPlusArgs)  
        {  
            //we create a special enumerator for this  
            IDebugClassField* pfieldThis  = NULL;  
            IEnumDebugFields* pparameters = NULL;  
            IEnumDebugFields* plocals     = NULL;  

            pmethod->GetThis( &pfieldThis );  
            pmethod->EnumParameters( &pparameters );  
            pmethod->EnumLocals( m_address, &plocals );  

            CEnumMethodField* penumMethodField =  
                new CEnumMethodField( pfieldThis, pparameters, plocals );  
            if (pfieldThis != NULL)  
                pfieldThis->Release();  
            if (pparameters != NULL)  
                pparameters->Release();  
            if (plocals != NULL)  
                plocals->Release();  
            if (!penumMethodField)   
            {   
                hr = E_OUTOFMEMORY;  
            }  
            else  
            {  
                hr = penumMethodField->QueryInterface( IID_IEnumDebugFields,  
                        reinterpret_cast<void**>(&pfields) );  
                penumMethodField->Release();  
            }  
        }  
        else if (guidFilter == guidFilterThis )  
        {  
            IDebugClassField* pfieldThis  = NULL;  

            hr = pmethod->GetThis( &pfieldThis );  
            if (SUCCEEDED(hr))  
            {  
                CEnumMethodField* penumMethodField =  
                    new CEnumMethodField( pfieldThis, NULL, NULL );  
                pfieldThis->Release();  
                if (!penumMethodField)   
                {  
                    hr = E_OUTOFMEMORY;  
                }  
                else  
                {  
                    hr = penumMethodField->QueryInterface( IID_IEnumDebugFields,  
                        reinterpret_cast<void**>(&pfields) );  
                    penumMethodField->Release();  
                }  
            }  
        }  
        else  
        {  
            hr = E_FAIL;  
        }  

        pmethod->Release();  
        if (hr != S_OK)  
            return hr;  
    }  

    //create a property info enumeration around the field enumerator  
    CEnumPropertyInfo* pproperties =  
        new CEnumPropertyInfo( m_provider, m_address, m_binder,   
                               radix, pfields, infoFlags );  
    if (pfields != NULL)  
        pfields->Release();  
    if (pproperties == NULL)  
        return E_OUTOFMEMORY;  

    hr = pproperties->QueryInterface( IID_IEnumDebugPropertyInfo2,  
        reinterpret_cast<void**>(ppchildren) );  
    pproperties->Release();  

    return hr;  
}  

Voir aussiSee Also

Exemple d’implémentation des variables locales Sample Implementation of Locals
Implémentation de GetMethodProperty Implementing GetMethodProperty
Contexte d’évaluationEvaluation Context