IModelObject::GetKey 方法 (dbgmodel.h)
GetKey 方法會依名稱取得指定索引鍵的值(和相關聯的元數據)。 大部分的客戶端都應該改用 GetKeyValue 方法。 如果索引鍵是屬性存取子,則呼叫這個方法會將屬性存取子(IModelPropertyAccessor 介面)傳回 IModelObject中。 不同於 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 |