Share via


Método IModelKeyReference::GetKey (dbgmodel.h)

O método GetKey em uma referência de chave se comporta como o método GetKey em IModelObject faria. Ele retorna o valor da chave subjacente e todos os metadados associados à chave. Se o valor da chave for um acessador de propriedade, isso retornará o acessador de propriedade (IModelPropertyAccessor) em um IModelObject. Esse método não chamará os métodos GetValue ou SetValue subjacentes no acessador de propriedade.

Sintaxe

HRESULT GetKey(
  _COM_Errorptr_opt_ IModelObject **object,
  IKeyStore                       **metadata
);

Parâmetros

object

O valor da chave será retornado aqui.

metadata

Metadados opcionais associados à chave serão retornados aqui.

Retornar valor

Esse método retorna HRESULT que indica êxito ou falha.

Comentários

Exemplo de código

ComPtr<IModelObject> spObject; /* get an object */

ComPtr<IModelKeyReference> spKeyRef;
if (SUCCEEDED(spObject->GetKeyReference(L"Id", &spKeyRef, nullptr)))
{
    ComPtr<IModelObject> spKey;
    if (SUCCEEDED(spKeyRef->GetKey(&spKey, nullptr)))
    {
        // spKey contains the equivalent of spObject->GetKey(L"Id", &spKey, nullptr)
        // This may be a property accessor since this was not a GetKeyValue.  
        // Check and fetch.  Note that GetKeyValue would do this for you.
        ModelObjectKind kind;
        if (SUCCEEDED(spKey->GetKind(&kind)))
        {
            if (kind == ObjectPropertyAccessor)
            {
                VARIANT vtProp;
                if (SUCCEEDED(spKey->GetIntrinsicValue(&vtProp)))
                {
                    // We are guaranteed *in-process* that the IUnknown is 
                    // an IModelPropertyAccessor via the ObjectPropertyAccessor
                    IModelPropertyAccessor *pProperty =
                        static_cast<IModelPropertyAccessor *>(vtProp.punkVal);
                    
                    // In order to fetch, we need to know the context object and 
                    // the key name.  Fetch it from the key reference.
                    ComPtr<IModelObject> spContextObject;
                    if (SUCCEEDED(spKeyRef->GetContextObject(&spContextObject)))
                    {
                        BSTR keyName;
                        if (SUCCEEDED(spKeyRef->GetName(&keyName)))
                        {
                            ComPtr<IModelObject> spKeyValue; 
                            if (SUCCEEDED(pProperty->GetValue(keyName,
                                                              spContextObject.Get(),
                                                              &spKeyValue)))
                            {
                                // spKeyValue contains the value of the "Id" key.
                            }

                            SysFreeString(keyName);
                        }
                    }

                    VariantFree(&vtProp);
                }
            }
            else
            {
                // spKey contains the value of the "Id" key.
            }
        }
    }
}

Requisitos

Requisito Valor
Cabeçalho dbgmodel.h

Confira também

Interface IModelKeyReference