參考 (WMI)

MOF ref 關鍵詞描述物件路徑,並對應至VT_BSTR自動化類型。 對象路徑可以是伺服器和命名空間的完整路徑,或相同命名空間中另一個對象的相對路徑。 您可以使用 ref 關鍵詞將兩個或多個類別連結在一起。 WMI 支援兩種類型的物件路徑,可用來定義 WMI 內的一般或特定路徑。

ref 關鍵詞的主要目的是減少存在於 WMI 存放庫中的對象之間的傳輸時間和編碼。 您也可以使用 ref 關鍵詞來建立兩個類別之間的關聯。 如需詳細資訊,請參閱 宣告關聯類別。 如果參考的項目位於相同的MOF檔案內,請使用別名來初始化 ref 值。 如需詳細資訊,請參閱 建立別名。

注意

當 ref 關鍵詞套用至索引鍵屬性時,您可以區分物件字串值的物件參考,而不是取值。

 

MOF 支援弱型別和強型別物件路徑的概念。 弱型別的物件路徑會指向未指定類別的物件,並使用具有 OBJECT 關鍵詞的 ref 關鍵詞。 強型別物件指向特定類別的物件,並使用具有類別名稱的 ref ref。 下列範例描述可指向任何類別或類別實例的弱型別 RefToAnyClass 參考,以及只能指向 ClassX 類別或實例的 RefToClassX 參考:

class MyClass
{
    object ref RefToAnyClass;       // Weakly typed
    ClassX ref RefToClassX;         // Strongly typed
};

下列範例描述兩個實例和參考先前實例的關聯物件:

#pragma namespace("\\\\.\\root")

instance of __Namespace
{
    Name = "WMI" ;
} ;

#pragma namespace("\\\\.\\root\\WMI")

Class A{
    [key] string aKey;
};

Class C{
    [key] string cKey;
};

// The following class creates an association 
// between the "A" class and the "C" class
    [Association] Class B{
    [key] A ref aRef;
    [Key, Min(1)] C ref cRef;
};

instance of a
{
    aKey = "This is the key for the A class";
};

instance of c
{
    cKey = "This is the key for the c class";
};