2011-08-31 4 views
3
typeof(Nullable<>) 

public static bool IsNullableType(Type t) 
     { 
      return t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>); 
     } 

람다 Expressioin 매개 변수가 nullable인지 확인하기 위해이 방법을 사용했습니다.이 VB는 어떻게 표현합니까?

답변

4

자신이 작업을 수행 할 필요가 없습니다 - 당신이 그것을 통과 유형이 널이 아닌 경우 그냥 Nothing/null을 반환 Nullable.GetUnderlyingType를 사용할 수 있습니다

Public Shared Function IsNullable(t as Type) As Boolean 
    Return Nullable.GetUnderlyingType(t) IsNot Nothing 
End Function 

(하지만 당신은 정말 열린 제네릭 형식을 필요로하는 경우 다음 GetType(Nullable(Of)) 작동합니다. 여러 유형 매개 변수가 필요한 경우, 단지를 쉼표로 구분 예 GetType(Dictionary(Of ,)).)

0
(t.GetGenericTypeDefinition() Is GetType(Nullable(Of))) 
관련 문제