XObject.Annotation 方法

定義

多載

Annotation(Type)

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

Annotation<T>()

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

Annotation(Type)

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

C#
public object Annotation (Type type);
C#
public object? Annotation (Type type);

參數

type
Type

要擷取的註釋類型。

傳回

Object

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

範例

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

C#
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);  
    }  
}  

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

T1  

另請參閱

適用於

.NET 7 和其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Annotation<T>()

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

C#
public T Annotation<T> () where T : class;
C#
public T? Annotation<T> () where T : class;

類型參數

T

要擷取的註釋類型。

傳回

T

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

範例

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

C#
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);  
    }  
}  

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

T1  

另請參閱

適用於

.NET 7 和其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0