Nullable Of What?
VB.NET
12/06/2009 por
Jar Jar Binks
Hello, I've been facing a problem when I was working with reflections. I was transforming a List (Of a Type) In a DataTable, but the columns created by nullable property should have their datatype equal the underlying type of the nullable. How to do to know the type of the underlying type of the nullable type? Simple: ' This code example demonstrates the ' Nullable.GetUnderlyingType() method. Imports System Imports System.Reflection Class Sample ' Declare a type named Example. ' The MyMethod member of Example returns a Nullable of Int32. Public Class Example Public Function MyMethod() As Nullable(Of Integer) Return 0 End Function End Class 'Example ' ' Use reflection to obtain a Type object for the Example type. ' Use the Type object to obtain a MethodInfo object for the MyMethod method. ' Use the MethodInfo object to obtain the type of the return value of ' MyMethod, which is Nullable of Int32. ' Use the GetUnderlyingType method to obtain the type argument of the ' return value type, which is Int32. ' Public Shared Sub Main() Dim t As Type = GetType(Example) Dim mi As MethodInfo = t.GetMethod("MyMethod") Dim retval As Type = mi.ReturnType Console.WriteLine("Return value type ... {0}", retval) Dim answer As Type = Nullable.GetUnderlyingType(retval) Console.WriteLine("Underlying type ..... {0}", answer) End Sub 'Main End Class 'Sample ' 'This code example produces the following results: ' 'Return value type ... System.Nullable`1[System.Int32] 'Underlying type ..... System.Int32 ' We get in the basis type of the nullable, which is Int32 (Integer) ------------------- IN PORTUGUESE NOW Olá, eu me deparei com um problema quando eu estava trabalhando com reflections. Eu estava transformando uma lista de objetos [List(Of Type)] num datatable, mas as colunas criadas que eram baseadas em propriedades nullable deveriam ter sua propriedade DataType igual ao tipo base da propriedade Nullable. Como fazer para saber o tipo base do tipo nullable? É Simples: ' This code example demonstrates the ' Nullable.GetUnderlyingType() method. Imports System Imports System.Reflection Class Sample ' Declare uma classe Exemplo. ' O MeuMetodo retorna um Nullable of Int32. Public Class Exemplo Public Function MeuMetodo () As Nullable(Of Integer) Return 0 End Function End Class 'Example ' ' Use reflections para obter um objeto do Tipo "Tipo" ( confuso, não?!) para a classe Exemplo ' Use o objeto "Tipo" para obter um objeto MethodInfo para termos a informações do "MeuMetodo" ' Com o Objeto MethodInfo, vamos obtero o tipo de retorno do MeuMetodo, que é Nullable of Int32 (Integer) ' Use o método GetUnderlyingType para obter o tipo base do tipo Nullable , que é Int32 (Integer) simplesmente ' Public Shared Sub Main() Dim t As Type = GetType(Exemplo) Dim mi As MethodInfo = t.GetMethod("MeuMetodo") Dim retval As Type = mi.ReturnType Console.WriteLine("Tipo do retorno da função ... {0}", retval) Dim answer As Type = Nullable.GetUnderlyingType(retval) Console.WriteLine("Tipo base ..... {0}", answer) End Sub 'Main End Class 'Sample 'O código produz no console o seguinte resultado: ' 'Tipo do retorno da função ... System.Nullable`1[System.Int32] 'Tipo base ..... System.Int32 ' See more Technical details in (mais detalhes técnicos em):http://msdn.microsoft.com/en-us/library/system.nullable.getunderlyingtype.aspx Arrivecerci people! [tags] nulllable ,underlying type of , base type, basis type *** Dica Postada por POG MASTERS DESKTOP - v1.0
Comentários sobre o artigo
Poste um comentário >>
|