1 #Region "Microsoft.VisualBasic::d8bc1d00cad0b167ad1d2d6d37f66594, Microsoft.VisualBasic.Core\Scripting\MetaData\Type.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 TypeInfo
35     
36     '         Properties: assm, FullIdentity, SystemKnownType
37     
38     '         Constructor: (+2 OverloadsSub New
39     
40     '         Function: [GetType], LoadAssembly, ToString
41     
42     '         Sub: __infoParser
43     
44     '         Operators: <>, =
45     
46     
47     ' /********************************************************************************/
48
49 #End Region
50
51 Imports System.Reflection
52 Imports System.Xml.Serialization
53
54 Namespace Scripting.MetaData
55
56     ''' <summary>
57     ''' The type reference information.(类型信息)
58     ''' </summary>
59     Public Class TypeInfo
60
61         ''' <summary>
62         ''' The assembly file which contains this type definition.(模块文件)
63         ''' </summary>
64         ''' <returns></returns>
65         <XmlAttribute> Public Property assm As String
66         ''' <summary>
67         ''' <see cref="Type.FullName"/>.(类型源)
68         ''' </summary>
69         ''' <returns></returns>
70         <XmlAttribute> Public Property FullIdentity As String
71
72         ''' <summary>
73         ''' Is this type object is a known system type?(是否是已知的类型?)
74         ''' </summary>
75         ''' <returns></returns>
76         Public ReadOnly Property SystemKnownType As Boolean
77             Get
78                 Return Not Scripting.GetType(FullIdentity) Is Nothing
79             End Get
80         End Property
81
82         Sub New()
83         End Sub
84
85         ''' <summary>
86         ''' Creates type reference from the definition.
87         ''' </summary>
88         ''' <param name="info"></param>
89         Sub New(info As Type)
90             Call __infoParser(info, assm, FullIdentity)
91         End Sub
92
93         Private Shared Sub __infoParser(info As Type, ByRef assm As StringByRef id As String)
94             assm = FileIO.FileSystem.GetFileInfo(info.Assembly.Location).Name
95             id = info.FullName
96         End Sub
97
98         Public Overrides Function ToString() As String
99             Return $"{assm}!{FullIdentity}"
100         End Function
101
102         ''' <summary>
103         ''' Loads the assembly file which contains this type. If the <param name="DIR"></param> is not a valid directory location, 
104         ''' then using the location <see cref="App.HOME"/> as default.
105         ''' </summary>
106         ''' <returns></returns>
107         Public Function LoadAssembly(Optional DIR As String = NothingAs Assembly
108             Dim path As String = If(Not DIR.DirectoryExists, App.HOME, DIR) & "/" & Me.assm
109             Dim assm As Assembly = Assembly.LoadFile(path)
110             Return assm
111         End Function
112
113         ''' <summary>
114         ''' Get mapping type information.
115         ''' </summary>
116         ''' <returns></returns>
117         Public Overloads Function [GetType](Optional knownFirst As Boolean = FalseAs Type
118             Dim type As Type
119
120             If knownFirst Then
121                 type = Scripting.GetType(FullIdentity)
122                 If Not type Is Nothing Then
123                     Return type
124                 End If
125             End If
126
127             Dim assm As Assembly = LoadAssembly()
128             type = assm.GetType(Me.FullIdentity)
129             Return type
130         End Function
131
132         ''' <summary>
133         ''' 检查a是否是指向b的类型引用的
134         ''' </summary>
135         ''' <param name="a"></param>
136         ''' <param name="b"></param>
137         ''' <returns></returns>
138         Public Overloads Shared Operator =(a As TypeInfo, b As Type) As Boolean
139             Dim assm As String = Nothing, type As String = Nothing
140             Call __infoParser(b, assm, type)
141             Return String.Equals(a.assm, assm, StringComparison.OrdinalIgnoreCase) AndAlso
142                 String.Equals(a.FullIdentity, type, StringComparison.Ordinal)
143         End Operator
144
145         Public Overloads Shared Operator <>(a As TypeInfo, b As Type) As Boolean
146             Return Not a = b
147         End Operator
148     End Class
149 End Namespace