typeresolution.vb
来自「大名鼎鼎的mono是.NET平台的跨平台(支持linux」· VB 代码 · 共 1,025 行 · 第 1/4 页
VB
1,025 行
End Select Case TypeCode.UInt64 Select Case tpValue Case TypeCode.Single, TypeCode.Double Dim tmp As Double = CDbl(value) If tmp <= UInt64.MaxValue AndAlso tmp >= UInt64.MinValue Then result = CULng(value) Return True Else Return False End If Case TypeCode.Decimal Dim tmp As Decimal = CDec(value) If tmp <= UInt64.MaxValue AndAlso tmp >= UInt64.MinValue Then result = CULng(value) Return True Else Return False End If End Select End Select Select Case tpValue Case TypeCode.Byte, TypeCode.UInt16, TypeCode.UInt32, TypeCode.UInt64 Dim tmpValue As ULong = CULng(value) Helper.Stop() Case TypeCode.SByte, TypeCode.Int16, TypeCode.Int32, TypeCode.Int64 Dim tmpValue As Long = CLng(value) 'Dim t As Type Helper.Stop() Case TypeCode.Char Helper.Stop() Case TypeCode.Boolean Helper.Stop() Case TypeCode.DateTime Helper.Stop() Case TypeCode.Decimal Helper.Stop() Case TypeCode.Double Helper.Stop() Case TypeCode.Single Helper.Stop() Case TypeCode.String Helper.Stop() Case Else Helper.Stop() End Select Helper.Stop() End If End Function ''' <summary> ''' Tries to convert the value into the desired type. Returns true if successful, ''' returns false otherwise. Can only convert if value is already an integral type ''' (does only do range-checking, not type conversion) ''' </summary> ''' <param name="value"></param> ''' <param name="result"></param> ''' <param name="desiredType"></param> ''' <returns></returns> ''' <remarks></remarks> Public Function CheckIntegralRange(ByVal value As Object, ByRef result As Object, ByVal desiredType As BuiltInDataTypes) As Boolean Helper.Assert(value IsNot Nothing) Helper.Assert(IsIntegralType(desiredType)) Dim tpValue As TypeCode = Helper.GetTypeCode(Compiler, value.GetType) Helper.Assert(IsIntegralType(tpValue)) Select Case tpValue Case TypeCode.Byte, TypeCode.UInt16, TypeCode.UInt32, TypeCode.UInt64 Dim tmpValue As ULong = CULng(value) Select Case desiredType Case BuiltInDataTypes.Byte If tmpValue <= Byte.MaxValue Then result = CByte(tmpValue) Return True Else Return False End If Case BuiltInDataTypes.UShort If tmpValue <= UShort.MaxValue Then result = CUShort(tmpValue) Return True Else Return False End If Case BuiltInDataTypes.UInteger If tmpValue <= UInteger.MaxValue Then result = CUInt(tmpValue) Return True Else Return False End If Case BuiltInDataTypes.ULong result = tmpValue Return True Case BuiltInDataTypes.SByte If tmpValue <= SByte.MaxValue Then result = CSByte(tmpValue) Return True Else Return False End If Case BuiltInDataTypes.Short If tmpValue <= Short.MaxValue Then result = CShort(tmpValue) Return True Else Return False End If Case BuiltInDataTypes.Integer If tmpValue <= Integer.MaxValue Then result = CInt(tmpValue) Return True Else Return False End If Case BuiltInDataTypes.Long If tmpValue <= Long.MaxValue Then result = CLng(tmpValue) Return True Else Return False End If Case Else Throw New InternalException("") End Select Case TypeCode.SByte, TypeCode.Int16, TypeCode.Int32, TypeCode.Int64 Dim tmpValue As Long = CLng(value) Select Case desiredType Case BuiltInDataTypes.Byte If tmpValue >= Byte.MinValue AndAlso tmpValue <= Byte.MaxValue Then result = CByte(tmpValue) Return True Else Return False End If Case BuiltInDataTypes.UShort If tmpValue >= UShort.MinValue AndAlso tmpValue <= UShort.MaxValue Then result = CUShort(tmpValue) Return True Else Return False End If Case BuiltInDataTypes.UInteger If tmpValue >= UInteger.MinValue AndAlso tmpValue <= UInteger.MaxValue Then result = CUInt(tmpValue) Return True Else Return False End If Case BuiltInDataTypes.ULong If tmpValue >= ULong.MinValue AndAlso tmpValue <= ULong.MaxValue Then result = CULng(tmpValue) Return True Else Return False End If Case BuiltInDataTypes.SByte If tmpValue >= SByte.MinValue AndAlso tmpValue <= SByte.MaxValue Then result = CSByte(tmpValue) Return True Else Return False End If Case BuiltInDataTypes.Short If tmpValue >= Short.MinValue AndAlso tmpValue <= Short.MaxValue Then result = CShort(tmpValue) Return True Else Return False End If Case BuiltInDataTypes.Integer If tmpValue >= Integer.MinValue AndAlso tmpValue <= Integer.MaxValue Then result = CInt(tmpValue) Return True Else Return False End If Case BuiltInDataTypes.Long If tmpValue >= Long.MinValue AndAlso tmpValue <= Long.MaxValue Then result = CLng(tmpValue) Return True Else Return False End If Case Else Throw New InternalException("") End Select Case Else Throw New InternalException("") End Select End Function Function TypeResolver(ByVal sender As Object, ByVal args As ResolveEventArgs) As System.Reflection.Assembly 'Compiler.Report.WriteLine(vbnc.Report.ReportLevels.Debug, "Resolve event fired for type '" & args.Name & "'")#If EXTENDEDDEBUG Then Compiler.Report.WriteLine("Looking for type: " & args.Name)#End If Dim types As Generic.List(Of Type) types = Me.Compiler.TypeManager.GetType(args.Name, True) If types.Count = 0 Then Throw New InternalException("Type '" & args.Name & "' was been referenced while compiling, but it wasn't found...") ElseIf types.Count = 1 Then If TypeOf types(0) Is TypeDescriptor Then Dim result As Boolean = True result = DirectCast(types(0), TypeDescriptor).Declaration.CreateType() AndAlso result If result = False Then Helper.NotImplemented() End If Else Throw New InternalException("Found type " & args.Name & ", but it isn't a TypeDescriptor!") End If Else Helper.AddError("Compiler cannot decide between several types with the name " & args.Name) End If Return Compiler.AssemblyBuilder End Function ''' <summary> ''' Looks up the specified type in the current compiling assembly. ''' This function looks up the name in the code / referenced assemblies ''' and returns an arraylist of found objects. ''' Can be anything: ''' If found in the parsing code, any Type* Object ''' If not found in the parsing code, but found in referenced assemblies, could be any of the following Reflection classes: ''' MemberInfo ''' Module ''' If a namespace was found, a string variable is returned with the name of the namespace. ''' </summary> ''' <param name="Name"></param> ''' <returns></returns> ''' <remarks></remarks> Function LookupType(ByVal Name As String) As ArrayList Dim result As New ArrayList result.Add(Compiler.AssemblyBuilder.GetType(Name)) Return result End FunctionEnd ClassPublic Enum ConversionType Implicit Explicit NoneEnd EnumPublic Class TypeConversionInfo Public Conversion As ConversionType Public hasPrecisionLoss As Boolean Public BinaryAddResult As TypeCodeEnd Class
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?