1 #Region "Microsoft.VisualBasic::53e7c6597d5aef0c229055d7d29eadc6, Microsoft.VisualBasic.Core\Text\Xml\Serialization\XmlComment.vb"
2
3     ' Author:
4     
5     '       asuka (amethyst.asuka@gcmodeller.org)
6     '       xie (genetics@smrucc.org)
7     '       xieguigang (xie.guigang@live.com)
8     
9     ' Copyright (c) 2018 GPL3 Licensed
10     
11     
12     ' GNU GENERAL PUBLIC LICENSE (GPL3)
13     
14     
15     ' This program is free software: you can redistribute it and/or modify
16     ' it under the terms of the GNU General Public License as published by
17     ' the Free Software Foundation, either version 3 of the License, or
18     ' (at your option) any later version.
19     
20     ' This program is distributed in the hope that it will be useful,
21     ' but WITHOUT ANY WARRANTY; without even the implied warranty of
22     ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23     ' GNU General Public License for more details.
24     
25     ' You should have received a copy of the GNU General Public License
26     ' along with this program. If not, see <http://www.gnu.org/licenses/>.
27
28
29
30     ' /********************************************************************************/
31
32     ' Summaries:
33
34     '     Class XmlCommentAttribute
35     
36     '         Properties: Value
37     
38     '         Constructor: (+1 OverloadsSub New
39     '         FunctionToString
40     
41     '     Module XmlCommentExtensions
42     
43     '         FunctionGetXmlComment, GetXmlCommentAttribute
44     
45     
46     ' /********************************************************************************/
47
48 #End Region
49
50 Imports System.Reflection
51 Imports System.Runtime.CompilerServices
52 Imports System.Xml
53
54 Namespace Text.Xml.Serialization
55
56     <AttributeUsage(AttributeTargets.Property, AllowMultiple:=False)>
57     Public Class XmlCommentAttribute : Inherits Attribute
58
59         Public Property Value As String
60
61         Sub New(value As String)
62             _Value = value
63         End Sub
64
65         Public Overrides Function ToString() As String
66             Return Value
67         End Function
68     End Class
69
70     Public Module XmlCommentExtensions
71
72         Const XmlCommentPropertyPostfix$ = "XmlComment"
73
74         <Extension>
75         Public Function GetXmlCommentAttribute(type As Type, <CallerMemberName> Optional memberName$ = NothingAs XmlCommentAttribute
76             Dim member = type.GetProperty(memberName)
77
78             If (member Is NothingThen
79                 Return Nothing
80             Else
81                 Dim attr = member.GetCustomAttribute(Of XmlCommentAttribute)()
82                 Return attr
83             End If
84         End Function
85
86         <Extension>
87         Public Function GetXmlComment(type As Type, <CallerMemberName> Optional memberName$ = ""As XmlComment
88             Dim attr = GetXmlCommentAttribute(type, memberName)
89
90             If (attr Is NothingThen
91                 If (memberName.EndsWith(XmlCommentPropertyPostfix)) Then
92                     attr = type.GetXmlCommentAttribute(memberName.Substring(0, memberName.Length - XmlCommentPropertyPostfix.Length))
93                 End If
94             End If
95
96             If (attr Is Nothing OrElse String.IsNullOrEmpty(attr.Value)) Then
97                 Return Nothing
98             Else
99                 Return New XmlDocument().CreateComment(attr.Value)
100             End If
101         End Function
102     End Module
103 End Namespace