键引用上的 SetKey 方法的行为与 IModelObject 上的 SetKey 方法一样。 它将分配密钥的值。 如果原始键是属性访问器,则会替换属性访问器。 它不会对属性访问器调用 SetValue 方法。
语法
HRESULT SetKey(
  IModelObject *object,
  IKeyStore    *metadata
);
参数
object
要分配给密钥的值。
metadata
要与密钥关联的可选元数据。
返回值
此方法返回指示成功或失败的 HRESULT。
言论
代码示例
ComPtr<IDataModelManager> spManager; /* get the data model manager */
ComPtr<IModelObject> spObject;       /* get an object */
ComPtr<IModelKeyReference> spKeyRef;
if (SUCCEEDED(spObject->GetKeyReference(L"Id", &spKeyRef, nullptr)))
{
    VARIANT vtValue;
    vtValue.vt = VT_UI8;
    vtValue.ullVal = 42;
    ComPtr<IModelObject> sp42;
    if (SUCCEEDED(spManager->CreateIntrinsicObject(ObjectIntrinsic, &vtValue, &sp42)))
    {
        if (SUCCEEDED(spKeyRef->SetKey(sp42.Get(), nullptr)))
        {
            // The "Id" key has been overwritten with the value 42.  
            // If the "Id" key originally was a property accessor, it isn't anymore.
            // This would not have called SetValue() on the property accessor.  
            // It replaced the property accessor with a static "42".
        }
    }
}
要求
| 要求 | 价值 | 
|---|---|
| 标头 | dbgmodel.h |