IKeyStore::GetKey 方法(dbgmodel.h)

GetKey 方法类似于 IModelObject上的 GetKey 方法。 如果指定键存在于密钥存储或密钥存储的父存储中,它将返回指定键的值。 请注意,如果键的值是属性访问器,则不会对属性访问器调用 GetValue 方法。 将返回装入 IModelObject 的实际 IModelPropertyAccessor。 通常,客户端会出于此原因调用 GetKeyValue。

语法

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

参数

key

要为其获取值的键的名称

object

将在此参数中返回键的值。

metadata

与此密钥关联的元数据存储将在此参数中选择性地返回。 二级元数据没有使用。 因此,此参数通常应指定为 null。

返回值

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

言论

代码示例

ComPtr<IModelObject> spObject; /* get an object */
ComPtr<IKeyStore> spMetadata;  /* get a key store from spObject (say 
                                  returned from GetKeyValue) */

ComPtr<IModelObject> spRadixKey;
if (SUCCEEDED(spMetadata->GetKey(L"PreferredRadix", &spRadixKey, nullptr)))
{
    // Since this is GetKey and not GetKeyValue, spRadixKey *MAY* be a 
    // property accessor.  Check and fetch.
    ModelObjectKind kind;
    if (SUCCEEDED(spRadixKey->GetKind(&kind)))
    {
        if (kind == ObjectPropertyAccessor)
        {
            VARIANT vtProp;
            if (SUCCEEDED(spRadixKey->GetIntrinsicValue(&vtProp)))
            {
                // There is a guarantee in-process that the IUnknown here 
                // is IModelPropertyAccessor because of the ObjectPropertyAccessor.
                IModelPropertyAccessor *pProperty = 
                    static_cast<IModelPropertyAccessor *>(vtProp.punkVal);
                
                ComPtr<IModelObject> spRadix; 

                // It is important that the context object be the object where 
                // the metadata store CAME FROM.  Hence the second argument
                // of spObject.Get().  Note that if you use GetKeyValue on the store,
                // this is automatically handled for you.
                if (SUCCEEDEDED(pProperty->GetValue(L"PreferredRadix", 
                                                    spObject.Get(), 
                                                    &spRadix)))
                {
                    // spRadix has the radix.  Use GetIntrinsicValueAs to unbox.
                }
                VariantClear(&vtProp);
            }
        }
        else
        {
            // spRadixKey has the radix.  Use GetIntrinsicValueAs to unbox.
        }
    }
}

要求

要求 价值
标头 dbgmodel.h

另请参阅

IKeyStore 接口