2011-06-21 26 views
13

Duplicar posible:
Determine if a generic param is a Nullable type¿Cómo verificar si un parámetro de tipo genérico es anulable?

Estoy tratando de determinar si un parámetro de tipo es anulable.

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; 
      } 
     } 
     //.... 
    } 

¿Cómo puedo hacer esto? Lo intenté haciendo t == typeof(Nullable) pero eso siempre resultó en falso.

Lo que quiero que ocurra es que foo.Get<bool?>(1) veces a null.

+4

Este es un DUP http://stackoverflow.com/questions/5181494/determine-if-a-generic-param-is-a-nullable-type – Nix

+0

@ Nix está en VB, así que tal vez no sea un tonto. Veremos – Earlz

+3

Las soluciones están en C# y VB – Nix

Respuesta

Cuestiones relacionadas