XObject.Annotations 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
多載
Annotations(Type) |
取得此 XObject 之指定類型註釋的集合。 |
Annotations<T>() |
取得此 XObject 之指定類型註釋的集合。 |
Annotations(Type)
- 來源:
- XObject.cs
- 來源:
- XObject.cs
- 來源:
- XObject.cs
取得此 XObject 之指定類型註釋的集合。
public:
System::Collections::Generic::IEnumerable<System::Object ^> ^ Annotations(Type ^ type);
public System.Collections.Generic.IEnumerable<object> Annotations (Type type);
member this.Annotations : Type -> seq<obj>
Public Function Annotations (type As Type) As IEnumerable(Of Object)
參數
- type
- Type
要擷取的註釋類型。
傳回
IEnumerable<T> 的 Object,包含符合此 XObject 指定類型的註釋。
範例
下列範例會將一些注釋新增至 XElement ,然後使用這個方法擷取批註集合。
public class MyAnnotation
{
private string tag;
public string Tag { get { return tag; } set { tag = value; } }
public MyAnnotation(string tag)
{
this.tag = tag;
}
}
class Program
{
static void Main(string[] args)
{
XElement root = new XElement("Root", "content");
root.AddAnnotation(new MyAnnotation("T1"));
root.AddAnnotation(new MyAnnotation("T2"));
root.AddAnnotation("abc");
root.AddAnnotation("def");
IEnumerable<object> annotationList;
annotationList = root.Annotations(typeof(MyAnnotation));
foreach (object ma in annotationList)
Console.WriteLine(((MyAnnotation)ma).Tag);
Console.WriteLine("----");
IEnumerable<object> stringAnnotationList;
stringAnnotationList = root.Annotations(typeof(string));
foreach (object str in stringAnnotationList)
Console.WriteLine((string)str);
}
}
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 root As XElement = <Root>content</Root>
root.AddAnnotation(New MyAnnotation("T1"))
root.AddAnnotation(New MyAnnotation("T2"))
root.AddAnnotation("abc")
root.AddAnnotation("def")
Dim annotationList As IEnumerable(Of Object)
annotationList = root.Annotations(GetType(MyAnnotation))
For Each ma As MyAnnotation In annotationList
Console.WriteLine(ma.Tag)
Next
Console.WriteLine("----")
Dim stringAnnotationList As IEnumerable(Of Object)
stringAnnotationList = root.Annotations(GetType(String))
For Each str As String In stringAnnotationList
Console.WriteLine(str)
Next
End Sub
End Module
這個範例會產生下列輸出:
T1
T2
----
abc
def
另請參閱
適用於
Annotations<T>()
- 來源:
- XObject.cs
- 來源:
- XObject.cs
- 來源:
- XObject.cs
取得此 XObject 之指定類型註釋的集合。
public:
generic <typename T>
where T : class System::Collections::Generic::IEnumerable<T> ^ Annotations();
public System.Collections.Generic.IEnumerable<T> Annotations<T> () where T : class;
member this.Annotations : unit -> seq<'T (requires 'T : null)> (requires 'T : null)
Public Function Annotations(Of T As Class) () As IEnumerable(Of T)
Public Iterator Function Annotations(Of T As Class) () As IEnumerable(Of T)
類型參數
- T
要擷取的註釋類型。
傳回
IEnumerable<T>,包含此 XObject 的註釋。
範例
下列範例會使用這個方法來擷取元素上的注釋。
public class MyAnnotation {
private string tag;
public string Tag {get{return tag;} set{tag=value;}}
public MyAnnotation(string tag) {
this.tag = tag;
}
}
class Program {
static void Main(string[] args) {
XElement root = new XElement("Root", "content");
root.AddAnnotation(new MyAnnotation("T1"));
root.AddAnnotation(new MyAnnotation("T2"));
root.AddAnnotation("abc");
root.AddAnnotation("def");
IEnumerable<MyAnnotation> annotationList;
annotationList = root.Annotations<MyAnnotation>();
foreach (MyAnnotation ma in annotationList)
Console.WriteLine(ma.Tag);
Console.WriteLine("----");
IEnumerable<string> stringAnnotationList;
stringAnnotationList = root.Annotations<string>();
foreach (string str in stringAnnotationList)
Console.WriteLine(str);
}
}
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 root As XElement = <Root>content</Root>
root.AddAnnotation(New MyAnnotation("T1"))
root.AddAnnotation(New MyAnnotation("T2"))
root.AddAnnotation("abc")
root.AddAnnotation("def")
Dim annotationList As IEnumerable(Of MyAnnotation)
annotationList = root.Annotations(Of MyAnnotation)()
For Each ma As MyAnnotation In annotationList
Console.WriteLine(ma.Tag)
Next
Console.WriteLine("----")
Dim stringAnnotationList As IEnumerable(Of String)
stringAnnotationList = root.Annotations(Of String)()
For Each str As String In stringAnnotationList
Console.WriteLine(str)
Next
End Sub
End Module
這個範例會產生下列輸出:
T1
T2
----
abc
def