2011-06-21 9 views
13

Powiel możliwe:
Determine if a generic param is a Nullable typeJak sprawdzić, czy ogólny parametr typu jest zerowalny?

Próbuję ustalić, czy parametr typ jest pustych.

public T Get<T>(int index) 
    { 
     var none=default(T); 
     var t = typeof(T); 
     BaseVariable v = this[index].Var; 
     if (T is Nullable) //compiler error 
     { 
      if (v == ...) 
      { 
       return none; 
      } 
     } 
     //.... 
    } 

Jak to zrobić? Próbowałem już robić t == typeof(Nullable), ale zawsze skutkowało to fałszem.

To, co chcę zrobić, to dla foo.Get<bool?>(1) czasami wartość null.

+4

To dup http://stackoverflow.com/questions/5181494/determine-if-a-generic-param-is-a-nullable-type – Nix

+0

@ Nix, który jest w VB, więc może granica nie jest dupkiem. Zobaczymy – Earlz

+3

Rozwiązania są w C# i VB – Nix

Odpowiedz