ManagementReferenceAttribute Class
The ManagementReferenceAttribute marks a class member, property or method parameter as a reference to another management object or class.
Assembly: System.Core (in System.Core.dll)
'Declaration <AttributeUsageAttribute(AttributeTargets.Property Or AttributeTargets.Field Or AttributeTargets.Parameter, AllowMultiple := False)> _ <HostProtectionAttribute(SecurityAction.LinkDemand, MayLeakOnAbort := True)> _ Public NotInheritable Class ManagementReferenceAttribute _ Inherits Attribute 'Usage Dim instance As ManagementReferenceAttribute
Note: |
|---|
The HostProtectionAttribute attribute applied to this type or member has the following Resources property value: MayLeakOnAbort. The HostProtectionAttribute does not affect desktop applications (which are typically started by double-clicking an icon, typing a command, or entering a URL in a browser). For more information, see the HostProtectionAttribute class or SQL Server Programming and Host Protection Attributes. |
You can use this attribute to create association classes as demonstrated in the following example.
This example demonstrates how to use the ManagementReferenceAttribute attribute together with the ManagementQualifierAttribute to create an association WMI class that links two other WMI classes. The example is a decoupled provider that exposes three WMI classes in the root/assoc namespace. The first two classes, NumberPhonetic and NumberLetter, are linked by the last class, LetterPhonetic.
To compile the example, you will need to include references to both System.Management.Instrumentation and System.Configuration.Install. You must run installutil.exe against the resulting executable and ensure that the program is running in order to use the implemented WMI classes.
using System;
using System.Collections;
using System.Management.Instrumentation;
[assembly: WmiConfiguration("root/assoc", HostingModel = ManagementHostingModel.Decoupled)]
[System.ComponentModel.RunInstaller(true)]
public class TheInstaller : DefaultManagementInstaller
{ }
namespace AssocExample
{
class Program
{
static void Main(string[] args)
{
InstrumentationManager.RegisterType(typeof(NumberPhonetic));
InstrumentationManager.RegisterType(typeof(NumberLetter));
InstrumentationManager.RegisterType(typeof(LetterPhonetic));
Console.WriteLine("Press enter to exit");
Console.ReadLine();
InstrumentationManager.UnregisterType(typeof(NumberPhonetic));
InstrumentationManager.UnregisterType(typeof(NumberLetter));
InstrumentationManager.UnregisterType(typeof(LetterPhonetic));
}
}
[ManagementEntity]
public class NumberPhonetic
{
[ManagementKey]
public int Number;
[ManagementProbe]
public string Name;
[ManagementBind]
public NumberPhonetic(int Number)
{
this.Number = Number;
if(Number == 1)
{
Name = "alpha";
}
else if(Number == 2)
{
Name = "bravo";
}
else
{
throw new InstanceNotFoundException();
}
}
[ManagementEnumerator]
static public IEnumerable EnumerateInstances()
{
for (int i = 1; i < 3; i++)
{
yield return new NumberPhonetic(i);
}
}
}
[ManagementEntity]
public class NumberLetter
{
[ManagementKey]
public int Number;
[ManagementProbe]
public string Letter;
[ManagementBind]
public NumberLetter(int Number)
{
this.Number = Number;
if(Number == 1)
{
Letter = "A";
}
else if(Number == 2)
{
Letter = "B";
}
else
{
throw new InstanceNotFoundException();
}
}
[ManagementEnumerator]
static public IEnumerable EnumerateInstances()
{
for (int i = 1; i < 3; i++)
{
yield return new NumberLetter(i);
}
}
}
[ManagementEntity]
[ManagementQualifier("Association", Flavor = ManagementQualifierFlavors.DisableOverride)]
public class LetterPhonetic
{
[ManagementReference(Type = "NumberLetter")]
[ManagementKey]
public string LetterNumber;
[ManagementReference(Type = "NumberPhonetic")]
[ManagementKey]
public string PhoneticNumber;
[ManagementEnumerator]
static public IEnumerable EnumerateInstances()
{
ArrayList insts = new ArrayList();
for (int i = 1; i < 3; i++)
{
LetterPhonetic inst = new LetterPhonetic();
inst.LetterNumber = "Letter = " + i;
inst.PhoneticNumber = "Phonetic = " + i;
insts.Add(inst);
}
return insts;
}
}
}
Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Note: