microsoft-visualbasic-runtime/Language/Value/Variant.vb

259 lines
8.5 KiB
VB.net
Raw Normal View History

2020-01-02 19:08:37 +08:00
#Region "Microsoft.VisualBasic::838723c2a0d84d1efbcb4a56d1cbea58, Microsoft.VisualBasic.Core\Language\Value\Variant.vb"
2019-05-06 18:27:46 +08:00
' Author:
'
' asuka (amethyst.asuka@gcmodeller.org)
' xie (genetics@smrucc.org)
' xieguigang (xie.guigang@live.com)
'
' Copyright (c) 2018 GPL3 Licensed
'
'
' GNU GENERAL PUBLIC LICENSE (GPL3)
'
'
' This program is free software: you can redistribute it and/or modify
' it under the terms of the GNU General Public License as published by
' the Free Software Foundation, either version 3 of the License, or
' (at your option) any later version.
'
' This program is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
' GNU General Public License for more details.
'
' You should have received a copy of the GNU General Public License
' along with this program. If not, see <http://www.gnu.org/licenses/>.
' /********************************************************************************/
' Summaries:
' Class [Variant]
'
' Properties: VA, VB
'
' Constructor: (+3 Overloads) Sub New
' Function: [TryCast], GetUnderlyingType
' Operators: (+2 Overloads) <>, (+2 Overloads) =, (+2 Overloads) Like
'
' Class [Variant]
'
' Properties: VC
'
' Constructor: (+4 Overloads) Sub New
' Operators: (+2 Overloads) Like
'
'
' /********************************************************************************/
2019-03-22 22:19:09 +08:00
#End Region
Imports System.Runtime.CompilerServices
2019-03-21 19:25:10 +08:00
Namespace Language
2019-03-19 21:03:20 +08:00
2019-05-02 13:28:49 +08:00
''' <summary>
''' Union type of <typeparamref name="A"/> and <typeparamref name="B"/>
''' </summary>
''' <typeparam name="A"></typeparam>
''' <typeparam name="B"></typeparam>
2019-03-22 22:11:28 +08:00
Public Class [Variant](Of A, B) : Inherits Value(Of Object)
2019-03-19 21:03:20 +08:00
Public Overrides Function GetUnderlyingType() As Type
If Value Is Nothing Then
Return GetType(Void)
Else
Return Value.GetType
End If
End Function
2019-11-14 19:53:24 +08:00
''' <summary>
''' TryCast to <typeparamref name="A"/>
''' </summary>
''' <returns></returns>
2019-03-19 21:03:20 +08:00
Public ReadOnly Property VA As A
2019-03-21 19:25:10 +08:00
<MethodImpl(MethodImplOptions.AggressiveInlining)>
2019-03-19 21:03:20 +08:00
Get
If Me Like GetType(A) OrElse GetUnderlyingType.IsInheritsFrom(GetType(A)) Then
2019-11-14 19:53:24 +08:00
Return Value
Else
Return Nothing
End If
2019-03-19 21:03:20 +08:00
End Get
End Property
2019-11-14 19:53:24 +08:00
''' <summary>
''' TryCast to <typeparamref name="B"/>
''' </summary>
''' <returns></returns>
2019-03-19 21:03:20 +08:00
Public ReadOnly Property VB As B
2019-03-21 19:25:10 +08:00
<MethodImpl(MethodImplOptions.AggressiveInlining)>
2019-03-19 21:03:20 +08:00
Get
If Me Like GetType(B) OrElse GetUnderlyingType.IsInheritsFrom(GetType(B)) Then
2019-11-14 19:53:24 +08:00
Return Value
Else
Return Nothing
End If
2019-03-19 21:03:20 +08:00
End Get
End Property
2019-12-26 22:35:19 +08:00
<DebuggerStepThrough>
2019-04-03 22:26:05 +08:00
Sub New()
End Sub
2019-12-26 22:35:19 +08:00
<DebuggerStepThrough>
2019-04-03 22:26:05 +08:00
Sub New(a As A)
Value = a
End Sub
2019-12-26 22:35:19 +08:00
<DebuggerStepThrough>
2019-04-03 22:26:05 +08:00
Sub New(b As B)
Value = b
End Sub
2019-12-26 22:35:19 +08:00
<DebuggerStepThrough>
2019-03-30 14:33:58 +08:00
Public Function [TryCast](Of T)() As T
Return DirectCast(Value, T)
End Function
2019-03-21 19:25:10 +08:00
<MethodImpl(MethodImplOptions.AggressiveInlining)>
2019-12-26 22:35:19 +08:00
<DebuggerStepThrough>
2019-03-22 22:11:28 +08:00
Public Overloads Shared Widening Operator CType(obj As [Variant](Of A, B)) As Type
2019-03-21 19:25:10 +08:00
Return obj.GetUnderlyingType
End Operator
<MethodImpl(MethodImplOptions.AggressiveInlining)>
2019-12-26 22:35:19 +08:00
<DebuggerStepThrough>
2019-03-22 22:11:28 +08:00
Public Overloads Shared Narrowing Operator CType(obj As [Variant](Of A, B)) As A
2019-03-19 21:03:20 +08:00
Return obj.VA
End Operator
2019-03-21 19:25:10 +08:00
<MethodImpl(MethodImplOptions.AggressiveInlining)>
2019-12-26 22:35:19 +08:00
<DebuggerStepThrough>
2019-03-22 22:11:28 +08:00
Public Overloads Shared Narrowing Operator CType(obj As [Variant](Of A, B)) As B
2019-03-19 21:03:20 +08:00
Return obj.VB
End Operator
2019-03-21 19:25:10 +08:00
<MethodImpl(MethodImplOptions.AggressiveInlining)>
2019-12-26 22:35:19 +08:00
<DebuggerStepThrough>
2019-03-22 22:11:28 +08:00
Public Overloads Shared Widening Operator CType(a As A) As [Variant](Of A, B)
Return New [Variant](Of A, B) With {.Value = a}
2019-03-19 21:03:20 +08:00
End Operator
2019-03-21 19:25:10 +08:00
<MethodImpl(MethodImplOptions.AggressiveInlining)>
2019-12-26 22:35:19 +08:00
<DebuggerStepThrough>
2019-03-22 22:11:28 +08:00
Public Overloads Shared Widening Operator CType(b As B) As [Variant](Of A, B)
Return New [Variant](Of A, B) With {.Value = b}
End Operator
<MethodImpl(MethodImplOptions.AggressiveInlining)>
2019-12-26 22:35:19 +08:00
<DebuggerStepThrough>
2019-03-22 22:11:28 +08:00
Public Overloads Shared Operator =(obj As [Variant](Of A, B), a As A) As Boolean
Return obj.VA.Equals(a)
End Operator
<MethodImpl(MethodImplOptions.AggressiveInlining)>
2019-12-26 22:35:19 +08:00
<DebuggerStepThrough>
2019-03-22 22:11:28 +08:00
Public Overloads Shared Operator <>(obj As [Variant](Of A, B), a As A) As Boolean
Return Not obj = a
End Operator
<MethodImpl(MethodImplOptions.AggressiveInlining)>
2019-12-26 22:35:19 +08:00
<DebuggerStepThrough>
2019-03-22 22:11:28 +08:00
Public Overloads Shared Operator =(obj As [Variant](Of A, B), b As B) As Boolean
Return obj.VB.Equals(b)
End Operator
<MethodImpl(MethodImplOptions.AggressiveInlining)>
2019-12-26 22:35:19 +08:00
<DebuggerStepThrough>
2019-03-22 22:11:28 +08:00
Public Overloads Shared Operator <>(obj As [Variant](Of A, B), b As B) As Boolean
Return Not obj = b
2019-03-19 21:03:20 +08:00
End Operator
2019-03-30 17:57:43 +08:00
''' <summary>
''' 请注意Like是直接进行比较不会比较继承关系链的
''' </summary>
''' <param name="var"></param>
''' <param name="type"></param>
''' <returns></returns>
2019-03-30 17:57:43 +08:00
<MethodImpl(MethodImplOptions.AggressiveInlining)>
2019-12-26 22:35:19 +08:00
<DebuggerStepThrough>
2019-03-30 17:57:43 +08:00
Public Overloads Shared Operator Like(var As [Variant](Of A, B), type As Type) As Boolean
Return var.GetUnderlyingType Is type
End Operator
2019-03-19 21:03:20 +08:00
End Class
2019-03-22 22:11:28 +08:00
Public Class [Variant](Of A, B, C) : Inherits [Variant](Of A, B)
2019-03-19 21:03:20 +08:00
Public ReadOnly Property VC As C
2019-03-21 19:25:10 +08:00
<MethodImpl(MethodImplOptions.AggressiveInlining)>
2019-03-19 21:03:20 +08:00
Get
Return Value
End Get
End Property
2019-04-03 22:26:05 +08:00
Sub New()
End Sub
2019-12-26 22:35:19 +08:00
<DebuggerStepThrough>
2019-04-03 22:26:05 +08:00
Sub New(a As A)
Value = a
End Sub
2019-12-26 22:35:19 +08:00
<DebuggerStepThrough>
2019-04-03 22:26:05 +08:00
Sub New(b As B)
Value = b
End Sub
2019-12-26 22:35:19 +08:00
<DebuggerStepThrough>
2019-04-03 22:26:05 +08:00
Sub New(c As C)
Value = c
End Sub
2019-03-21 19:25:10 +08:00
<MethodImpl(MethodImplOptions.AggressiveInlining)>
2019-12-26 22:35:19 +08:00
<DebuggerStepThrough>
2019-03-22 22:11:28 +08:00
Public Overloads Shared Narrowing Operator CType(obj As [Variant](Of A, B, C)) As C
2019-03-19 21:03:20 +08:00
Return obj.VC
End Operator
2019-03-21 19:25:10 +08:00
<MethodImpl(MethodImplOptions.AggressiveInlining)>
2019-12-26 22:35:19 +08:00
<DebuggerStepThrough>
2019-03-22 22:11:28 +08:00
Public Overloads Shared Narrowing Operator CType(obj As [Variant](Of A, B, C)) As A
2019-03-21 19:25:10 +08:00
Return obj.VA
End Operator
<MethodImpl(MethodImplOptions.AggressiveInlining)>
2019-12-26 22:35:19 +08:00
<DebuggerStepThrough>
2019-03-22 22:11:28 +08:00
Public Overloads Shared Narrowing Operator CType(obj As [Variant](Of A, B, C)) As B
2019-03-21 19:25:10 +08:00
Return obj.VB
End Operator
<MethodImpl(MethodImplOptions.AggressiveInlining)>
2019-12-26 22:35:19 +08:00
<DebuggerStepThrough>
2019-03-22 22:11:28 +08:00
Public Overloads Shared Widening Operator CType(c As C) As [Variant](Of A, B, C)
Return New [Variant](Of A, B, C) With {.Value = c}
2019-03-21 19:25:10 +08:00
End Operator
<MethodImpl(MethodImplOptions.AggressiveInlining)>
2019-12-26 22:35:19 +08:00
<DebuggerStepThrough>
2019-03-22 22:11:28 +08:00
Public Overloads Shared Widening Operator CType(a As A) As [Variant](Of A, B, C)
Return New [Variant](Of A, B, C) With {.Value = a}
2019-03-21 19:25:10 +08:00
End Operator
<MethodImpl(MethodImplOptions.AggressiveInlining)>
2019-12-26 22:35:19 +08:00
<DebuggerStepThrough>
2019-03-22 22:11:28 +08:00
Public Overloads Shared Widening Operator CType(b As B) As [Variant](Of A, B, C)
Return New [Variant](Of A, B, C) With {.Value = b}
2019-03-19 21:03:20 +08:00
End Operator
2019-03-30 17:57:43 +08:00
<MethodImpl(MethodImplOptions.AggressiveInlining)>
2019-12-26 22:35:19 +08:00
<DebuggerStepThrough>
2019-03-30 17:57:43 +08:00
Public Overloads Shared Operator Like(var As [Variant](Of A, B, C), type As Type) As Boolean
Return var.GetUnderlyingType Is type
End Operator
2019-03-19 21:03:20 +08:00
End Class
2019-03-22 22:19:09 +08:00
End Namespace