📄 typeconverter.vb
字号:
Throw New NotImplementedException End Select End Function Shared Function GetConversionResultType(ByVal tp1 As TypeCode, ByVal tp2 As TypeCode) As Char Return GetArrayChar(tp1, tp2, ConversionResultType) End Function Shared Function GetExpOperandType(ByVal op1 As TypeCode, ByVal op2 As TypeCode) As TypeCode Return GetExpResultType(op1, op2) End Function Shared Function GetEqualsOperandType(ByVal op1 As TypeCode, ByVal op2 As TypeCode) As TypeCode Return GetResultType(op1, op2, BinaryOperandTypes) End Function Shared Function GetLTOperandType(ByVal op1 As TypeCode, ByVal op2 As TypeCode) As TypeCode Return GetResultType(op1, op2, BinaryOperandTypes) End Function Shared Function GetGTOperandType(ByVal op1 As TypeCode, ByVal op2 As TypeCode) As TypeCode Return GetResultType(op1, op2, BinaryOperandTypes) End Function Shared Function GetLEOperandType(ByVal op1 As TypeCode, ByVal op2 As TypeCode) As TypeCode Return GetResultType(op1, op2, BinaryOperandTypes) End Function Shared Function GetGEOperandType(ByVal op1 As TypeCode, ByVal op2 As TypeCode) As TypeCode Return GetResultType(op1, op2, BinaryOperandTypes) End Function Shared Function GetNotEqualsOperandType(ByVal op1 As TypeCode, ByVal op2 As TypeCode) As TypeCode Return GetResultType(op1, op2, BinaryOperandTypes) End Function Shared Function GetShiftResultType(ByVal op1 As TypeCode, ByVal op2 As TypeCode) As TypeCode Return GetResultType(op1, op2, ShiftResultType) End Function Shared Function GetModResultType(ByVal op1 As TypeCode, ByVal op2 As TypeCode) As TypeCode Return GetResultType(op1, op2, ModResultType) End Function Shared Function GetLikeResultType(ByVal op1 As TypeCode, ByVal op2 As TypeCode) As TypeCode Return GetResultType(op1, op2, LikeResultType) End Function Shared Function GetLikeOperandType(ByVal op1 As TypeCode, ByVal op2 As TypeCode) As TypeCode Return GetResultType(op1, op2, LikeOperandType) End Function Shared Function GetConcatResultType(ByVal op1 As TypeCode, ByVal op2 As TypeCode) As TypeCode Return GetResultType(op1, op2, ConcatResultType) End Function Shared Function GetConcatOperandType(ByVal op1 As TypeCode, ByVal op2 As TypeCode) As TypeCode Return GetResultType(op1, op2, ConcatOperandType) End Function Shared Function GetRealDivResultType(ByVal op1 As TypeCode, ByVal op2 As TypeCode) As TypeCode Return GetResultType(op1, op2, RealDivResultTypes) End Function Shared Function GetIntDivResultType(ByVal op1 As TypeCode, ByVal op2 As TypeCode) As TypeCode Return GetResultType(op1, op2, IntDivResultTypes) End Function Shared Function GetExpResultType(ByVal op1 As TypeCode, ByVal op2 As TypeCode) As TypeCode Return GetResultType(op1, op2, ExponentResultTypes) End Function Shared Function GetEqualsResultType(ByVal op1 As TypeCode, ByVal op2 As TypeCode) As TypeCode If GetEqualsOperandType(op1, op2) = TypeCode.Empty Then Return TypeCode.Empty Return TypeCode.Boolean End Function Shared Function GetLTResultType(ByVal op1 As TypeCode, ByVal op2 As TypeCode) As TypeCode If GetLTOperandType(op1, op2) = TypeCode.Empty Then Return TypeCode.Empty Return TypeCode.Boolean End Function Shared Function GetGTResultType(ByVal op1 As TypeCode, ByVal op2 As TypeCode) As TypeCode If GetGTOperandType(op1, op2) = TypeCode.Empty Then Return TypeCode.Empty Return TypeCode.Boolean End Function Shared Function GetLEResultType(ByVal op1 As TypeCode, ByVal op2 As TypeCode) As TypeCode If GetLEOperandType(op1, op2) = TypeCode.Empty Then Return TypeCode.Empty Return TypeCode.Boolean End Function Shared Function GetGEResultType(ByVal op1 As TypeCode, ByVal op2 As TypeCode) As TypeCode If GetGEOperandType(op1, op2) = TypeCode.Empty Then Return TypeCode.Empty Return TypeCode.Boolean End Function Shared Function GetNotEqualsResultType(ByVal op1 As TypeCode, ByVal op2 As TypeCode) As TypeCode If GetNotEqualsOperandType(op1, op2) = TypeCode.Empty Then Return TypeCode.Empty Return TypeCode.Boolean End Function Shared Function GetUnaryMinusResultType(ByVal op1 As TypeCode) As TypeCode Return GetResultType(op1, UnaryMinusResultType) End Function Shared Function GetUnaryPlusResultType(ByVal op1 As TypeCode) As TypeCode Return GetResultType(op1, UnaryPlusResultType) End Function Shared Function GetAndResultType(ByVal op1 As TypeCode, ByVal op2 As TypeCode) As TypeCode Return GetResultType(op1, op2, LogicalOperatorResultType) End Function Shared Function GetOrResultType(ByVal op1 As TypeCode, ByVal op2 As TypeCode) As TypeCode Return GetResultType(op1, op2, LogicalOperatorResultType) End Function Shared Function GetXorResultType(ByVal op1 As TypeCode, ByVal op2 As TypeCode) As TypeCode Return GetResultType(op1, op2, LogicalOperatorResultType) End Function Shared Function GetUnaryNotResultType(ByVal op1 As TypeCode) As TypeCode Return GetResultType(op1, NotOperatorResultType) End Function Shared Function GetOrElseResultType(ByVal op1 As TypeCode, ByVal op2 As TypeCode) As TypeCode Return GetResultType(op1, op2, ShortcircuitResultType) End Function Shared Function GetAndAlsoResultType(ByVal op1 As TypeCode, ByVal op2 As TypeCode) As TypeCode Return GetResultType(op1, op2, ShortcircuitResultType) End Function Shared Function GetMultResultType(ByVal op1 As TypeCode, ByVal op2 As TypeCode) As TypeCode Return GetResultType(op1, op2, MultResultType) End Function Shared Function GetBinaryAddResultType(ByVal op1 As TypeCode, ByVal op2 As TypeCode) As TypeCode Return GetResultType(op1, op2, AddResultType) End Function Shared Function GetBinarySubResultType(ByVal op1 As TypeCode, ByVal op2 As TypeCode) As TypeCode Return GetResultType(op1, op2, SubResultType) End Function Shared Function GetIsIsNotOperandType(ByVal op1 As TypeCode, ByVal op2 As TypeCode) As TypeCode Return TypeCode.Object End Function Shared Function GetIsIsNotResultType(ByVal op1 As TypeCode, ByVal op2 As TypeCode) As TypeCode Return TypeCode.Boolean End Function Shared Function GetUnaryResultType(ByVal op As KS, ByVal op1 As TypeCode) As TypeCode Select Case op Case KS.Add Return GetUnaryPlusResultType(op1) Case KS.Minus Return GetUnaryMinusResultType(op1) Case KS.Not Return GetUnaryNotResultType(op1) Case Else Throw New NotImplementedException() End Select End Function Shared Function GetBinaryResultType(ByVal op As KS, ByVal op1 As TypeCode, ByVal op2 As TypeCode) As TypeCode Select Case op Case KS.And Return GetAndResultType(op1, op2) Case KS.AndAlso Return GetAndAlsoResultType(op1, op2) Case KS.Or Return GetOrResultType(op1, op2) Case KS.OrElse Return GetOrElseResultType(op1, op2) Case KS.Xor Return GetXorResultType(op1, op2) Case KS.Add Return GetBinaryAddResultType(op1, op2) Case KS.Minus Return GetBinarySubResultType(op1, op2) Case KS.Mult Return GetMultResultType(op1, op2) Case KS.RealDivision Return GetRealDivResultType(op1, op2) Case KS.IntDivision Return GetIntDivResultType(op1, op2) Case KS.Power Return GetExpOperandType(op1, op2) Case KS.Concat Return GetConcatResultType(op1, op2) Case KS.GE Return GetGEResultType(op1, op2) Case KS.GT Return GetGTResultType(op1, op2) Case KS.LE Return GetLEResultType(op1, op2) Case KS.LT Return GetLTResultType(op1, op2) Case KS.Equals Return GetEqualsResultType(op1, op2) Case KS.NotEqual Return GetNotEqualsResultType(op1, op2) Case KS.ShiftLeft, KS.ShiftRight Return GetShiftResultType(op1, op2) Case KS.Mod Return GetModResultType(op1, op2) Case KS.Like Return GetLikeResultType(op1, op2) Case KS.Is, KS.IsNot Return GetIsIsNotResultType(op1, op2) Case Else Throw New NotImplementedException() End Select End Function Shared Function GetUnaryOperandType(ByVal op As KS, ByVal operand As TypeCode) As TypeCode Select Case op Case KS.Add Return GetUnaryPlusResultType(operand) Case KS.Minus Return GetUnaryMinusResultType(operand) Case KS.Not Return GetUnaryNotResultType(operand) Case Else Throw New NotImplementedException End Select End Function Shared Function GetBinaryOperandType(ByVal op As KS, ByVal op1 As TypeCode, ByVal op2 As TypeCode) As TypeCode Select Case op Case KS.And Return GetAndResultType(op1, op2) Case KS.AndAlso Return GetAndAlsoResultType(op1, op2) Case KS.Or Return GetOrResultType(op1, op2) Case KS.OrElse Return GetOrElseResultType(op1, op2) Case KS.Xor Return GetXorResultType(op1, op2) Case KS.Add Return GetBinaryAddResultType(op1, op2) Case KS.Minus Return GetBinarySubResultType(op1, op2) Case KS.Mult Return GetMultResultType(op1, op2) Case KS.RealDivision Return GetRealDivResultType(op1, op2) Case KS.IntDivision Return GetIntDivResultType(op1, op2) Case KS.Power Return GetExpOperandType(op1, op2) Case KS.Concat Return GetConcatOperandType(op1, op2) Case KS.GE Return GetGEOperandType(op1, op2) Case KS.GT Return GetGTOperandType(op1, op2) Case KS.LE Return GetLEOperandType(op1, op2) Case KS.LT Return GetLTOperandType(op1, op2) Case KS.Equals Return GetEqualsOperandType(op1, op2) Case KS.NotEqual Return GetNotEqualsOperandType(op1, op2) Case KS.ShiftLeft, KS.ShiftRight Return GetShiftResultType(op1, op2) Case KS.Mod Return GetModResultType(op1, op2) Case KS.Like Return GetLikeOperandType(op1, op2) Case KS.Is, KS.IsNot Return GetIsIsNotOperandType(op1, op2) Case Else Throw New NotImplementedException() End Select End Function Private Shared Function GetArrayChar(ByVal op1 As TypeCode, ByVal op2 As TypeCode, ByVal array As String) As Char Return array.Chars(op1 + op2 * 19) End Function Private Shared Function GetResultType(ByVal op1 As TypeCode, ByVal array As String) As TypeCode Dim chr As Char chr = array.Chars(op1) If chr = "X"c Then Return Nothing Else Return CType(Microsoft.VisualBasic.Asc(chr) - Microsoft.VisualBasic.Asc("A"), TypeCode) End If End Function Private Shared Function GetResultType(ByVal op1 As TypeCode, ByVal op2 As TypeCode, ByVal array As String) As TypeCode Dim chr As Char chr = array.Chars(op1 + op2 * 19) If chr = "X"c Then Return Nothing Else Return CType(Microsoft.VisualBasic.Asc(chr) - Microsoft.VisualBasic.Asc("A"), TypeCode) End If End Function#If DEVGENERATOR OrElse GENERATOR = False Then ''' <summary> ''' Converts the source to the destination type. Compiletime conversions are the only ones that succeeds. ''' Returns nothing if no conversion possible. ''' </summary> ''' <param name="Source"></param> ''' <param name="Destination"></param> ''' <returns></returns> ''' <remarks></remarks> Public Shared Function ConvertTo(ByVal Source As Object, ByVal Destination As Type) As Object Dim result As Object Helper.Assert(Source IsNot Nothing) Helper.Assert(Destination IsNot Nothing) Dim dtc As TypeCode = Helper.GetTypeCode(Nothing, Destination) Dim stc As TypeCode = Helper.GetTypeCode(Nothing, Source.GetType) 'Console.WriteLine("ConvertTo: from " & stc.ToString() & " to " & dtc.ToString) If dtc = stc Then Return Source Select Case dtc Case TypeCode.Boolean result = ConvertToBoolean(Source, stc) Case TypeCode.Byte result = ConvertToByte(Source, stc) Case TypeCode.Char result = ConvertToChar(Source, stc) Case TypeCode.DateTime result = ConvertToDateTime(Source, stc) Case TypeCode.DBNull result = ConvertToDBNull(Source, stc) Case TypeCode.Decimal result = ConvertToDecimal(Source, stc) Case TypeCode.Double result = ConvertToDouble(Source, stc) Case TypeCode.Empty result = ConvertToEmpty(Source, stc) Case TypeCode.Int16 result = ConvertToInt16(Source, stc) Case TypeCode.Int32 result = ConvertToInt32(Source, stc) Case TypeCode.Int64 result = ConvertToInt64(Source, stc) Case TypeCode.Object result = ConvertToObject(Source, stc) Case TypeCode.SByte result = ConvertToSByte(Source, stc) Case TypeCode.Single result = ConvertToSingle(Source, stc) Case TypeCode.String result = ConvertToString(Source, stc) Case TypeCode.UInt16 result = ConvertToUInt16(Source, stc) Case TypeCode.UInt32 result = ConvertToUInt32(Source, stc) Case TypeCode.UInt64 result = ConvertToUInt64(Source, stc) Case Else Throw New NotImplementedException() End Select Return result End Function#End If Public Shared Function ConvertToBoolean(ByVal Source As Object, ByVal SourceTypeCode As TypeCode) As Boolean Select Case SourceTypeCode Case TypeCode.Boolean Throw New NotImplementedException Case TypeCode.Byte Throw New NotImplementedException Case TypeCode.Char Throw New NotImplementedException Case TypeCode.DateTime
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -