XObject.Annotation 方法

定义

重载

Annotation(Type)

从此 XObject 获取指定类型的第一个批注对象。Gets the first annotation object of the specified type from this XObject.

Annotation<T>()

从此 XObject 获取指定类型的第一个批注对象。Gets the first annotation object of the specified type from this XObject.

Annotation(Type)

从此 XObject 获取指定类型的第一个批注对象。Gets the first annotation object of the specified type from this XObject.

public:
 System::Object ^ Annotation(Type ^ type);
public object Annotation (Type type);
member this.Annotation : Type -> obj
Public Function Annotation (type As Type) As Object

参数

type
Type

要检索的批注类型。The type of the annotation to retrieve.

返回

包含与指定类型匹配的第一个批注对象的 Object;如果没有指定类型的批注,则为 nullThe Object that contains the first annotation object that matches the specified type, or null if no annotation is of the specified type.

示例

下面的示例将批注添加到 XElementThe following example adds an annotation to an XElement. 然后,它将检索批注,并指定要检索的类型。It then retrieves the annotation, specifying the type to retrieve.

public class MyAnnotation {  
    private string tag;  
    public string Tag {get{return tag;} set{tag=value;}}  
    public MyAnnotation(string tag) {  
        this.tag = tag;  
    }  
}  
  
public class Program {  
    public static void Main(string[] args) {     
        MyAnnotation ma = new MyAnnotation("T1");  
        XElement root = new XElement("Root", "content");  
        root.AddAnnotation(ma);  
  
        MyAnnotation ma2 = (MyAnnotation)root.Annotation(typeof(MyAnnotation));  
        Console.WriteLine(ma2.Tag);  
    }  
}  
Public Class MyAnnotation  
    Private _tag As String  
  
    Property Tag() As String  
        Get  
            Return Me._tag  
        End Get  
        Set(ByVal Value As String)  
            Me._tag = Value  
        End Set  
    End Property  
  
    Public Sub New(ByVal tag As String)  
        Me._tag = tag  
    End Sub  
End Class  
  
Module Module1  
    Sub Main()  
        Dim ma As MyAnnotation = New MyAnnotation("T1")  
        Dim root As XElement = <Root>content</Root>  
        root.AddAnnotation(ma)  
  
        Dim ma2 As MyAnnotation = DirectCast(root.Annotation(GetType(MyAnnotation)), MyAnnotation)  
        Console.WriteLine(ma2.Tag)  
    End Sub  
  
End Module  

该示例产生下面的输出:This example produces the following output:

T1  

另请参阅

Annotation<T>()

从此 XObject 获取指定类型的第一个批注对象。Gets the first annotation object of the specified type from this XObject.

public:
generic <typename T>
 where T : class T Annotation();
public T Annotation<T> () where T : class;
member this.Annotation : unit -> 'T (requires 'T : null)
Public Function Annotation(Of T As Class) () As T

类型参数

T

要检索的批注类型。The type of the annotation to retrieve.

返回

T

与指定类型匹配的第一个批注对象;如果没有指定类型的批注,则为 nullThe first annotation object that matches the specified type, or null if no annotation is of the specified type.

示例

下面的示例将一个批注添加到元素,然后通过此方法检索它。The following example adds an annotation to an element, and then retrieves it through this method.

public class MyAnnotation {  
    private string tag;  
    public string Tag {get{return tag;} set{tag=value;}}  
    public MyAnnotation(string tag) {  
        this.tag = tag;  
    }  
}  
  
public class Program {  
    public static void Main(string[] args) {     
        MyAnnotation ma = new MyAnnotation("T1");  
        XElement root = new XElement("Root", "content");  
        root.AddAnnotation(ma);  
  
        MyAnnotation ma2 = root.Annotation<MyAnnotation>();  
        Console.WriteLine(ma2.Tag);  
    }  
}  
Public Class MyAnnotation  
    Private _tag As String  
  
    Property Tag() As String  
        Get  
            Return Me._tag  
        End Get  
        Set(ByVal Value As String)  
            Me._tag = Value  
        End Set  
    End Property  
  
    Public Sub New(ByVal tag As String)  
        Me._tag = tag  
    End Sub  
End Class  
  
Module Module1  
    Sub Main()  
        Dim ma As MyAnnotation = New MyAnnotation("T1")  
        Dim root As XElement = <Root>content</Root>  
        root.AddAnnotation(ma)  
  
        Dim ma2 As MyAnnotation = root.Annotation(Of MyAnnotation)()  
        Console.WriteLine(ma2.Tag)  
    End Sub  
End Module  

该示例产生下面的输出:This example produces the following output:

T1  

另请参阅

适用于