DotNet · 2022年2月28日

vb.net中Nullable的类型分析

dim p as Nullable(of double) = 10.0
     p.GetType()   返回的类型并不是Nullable本身的类型,而是Nullable的RuntimeType,为double

,要返回正确的类型,需要使用GetType(Nullable(of double))

直接输出Nullable的类型的话如下:

{Name = “Nullable`1” FullName = “System.Nullable`1[[System.Double, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]”}
显然,这个在部份应用下是不能满足需要的,在部份应用场合下,Nullable是需要隐形的。

所以需要判断IsGenericType属性,如果是的话,就要通过GetGenericArguments()方法来返回包含的具体类型


        Dim t As Type = GetType(Nullable(Of Double))


        If t.IsGenericType Then
            ' If this is a generic type, display the type arguments.
            '
            Dim typeArguments As Type() = t.GetGenericArguments()


            Console.WriteLine(vbTab & "List type arguments (" _
                & typeArguments.Length & "):")


            For Each tParam As Type In typeArguments
                ' If this is a type parameter, display its position.
                '
                If tParam.IsGenericParameter Then
                    Console.WriteLine(vbTab & vbTab & tParam.ToString() _
                        & vbTab & "(unassigned - parameter position " _
                        & tParam.GenericParameterPosition & ")")
                Else
                    Console.WriteLine(vbTab & vbTab & tParam.ToString())
                End If
            Next tParam
        End If
        Console.ReadLine()
 

最新电影,电视剧,尽在午夜剧场

电影电视剧午夜不寂寞