IModelObject::GetKey 方法(dbgmodel.h)

GetKey 方法将按名称获取给定键的值(以及与关联的元数据)。 大多数客户端应改用 GetKeyValue 方法。 如果键是属性访问器,则调用此方法将返回装箱到 IModelObject中的属性访问器(IModelPropertyAccessor 接口)。 与 GetKeyValue 不同,此方法不会通过调用 GetValue 方法自动解析密钥的基础值。 责任是调用方的责任。

语法

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

参数

key

要为其获取值的键的名称。

object

将在此参数中返回键的值。 在某些情况下,即使该方法返回失败的 HRESULT,扩展错误信息也可能在此参数中传出。

metadata

与此密钥关联的元数据存储将在此参数中选择性地返回。

返回值

此方法返回指示成功或失败的 HRESULT。 返回值E_BOUNDS(或在某些情况下E_NOT_SET)表示找不到键。

言论

代码示例

ComPtr<IModelObject> spProcess; /* get a process object */

ComPtr<IModelObject> spIdKey;
if (SUCCEEDED(spProcess->GetKey(L"Id", &spIdKey, nullptr)))
{
    // Unlike GetKeyValue(), spIdKey may contain a value or it may be a 
    // *property* that needs to be fetched.  Check!
    ModelObjectKind kind;
    if (SUCCEEDED(spIdKey->GetKind(&kind)))
    {
        if (kind == ObjectPropertyAccessor)
        {
            VARIANT vtProp; 
            if (SUCCEEDED(spIdKey->GetIntrinsicValue(&vtProp)))
            {
                // There is an *in-process* guarantee because of 
                // ObjectPropertyAccessor that the IUnknown is an IModelPropertyAccessor
                IModelPropertyAccessor *pPropertyAccessor = 
                    static_cast<IModelPropertyAccessor *>(vtProp.punkVal);

                // Fetch the value underneath the property accessor.  GetKeyValue 
                // would have done this for us.
                ComPtr<IModelObject> spId;
                if (SUCCEEDED(pPropertyAccessor->GetValue(L"Id", spProcess.Get(), &spId)))
                {
                    // spId now contains the value of the id.  Unbox with GetIntrinsicValueAs.
                }

                VariantClear(&vtProp);
            }
        }
        else
        {
            // spIdKey contains the value.  Unbox with GetIntrinsicValueAs.
        }
    }
}

要求

要求 价值
标头 dbgmodel.h

另请参阅

IModelObject 接口