共用方式為


XObject.Annotation 方法

定義

多載

Annotation(Type)

從此 XObject 取得指定類型的第一個註釋物件。

Annotation<T>()

從此 XObject 取得指定類型的第一個註釋物件。

Annotation(Type)

從此 XObject 取得指定類型的第一個註釋物件。

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

參數

type
Type

要擷取的註釋類型。

傳回

Object

Object,包含符合指定類型的第一個註釋物件,如果沒有指定類型的註釋,則為 null

範例

下列範例會將批註新增至 XElement 。 然後它會擷取批註,並指定要擷取的類型。

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  

這個範例會產生下列輸出:

T1  

另請參閱

適用於

Annotation<T>()

從此 XObject 取得指定類型的第一個註釋物件。

public:
generic <typename T>
 where T : class T Annotation();
public T Annotation<T> () where T : class;
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

要擷取的註釋類型。

傳回

T

符合指定類型的第一個註釋物件,或者如果沒有指定類型的註釋,則為 null

範例

下列範例會將注釋新增至元素,然後透過此方法擷取。

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  

這個範例會產生下列輸出:

T1  

另請參閱

適用於