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

參數

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  

另請參閱

適用於