If I do this:
Type type = typeof(Nullable<int>);
The type
variable holds the correct Nullable'1
type.
However, If I try to do this:
Nullable<int> n = 2;
Type type = n.GetType();
type
ends up holding System.Int32
which is the underlying type, but not what I am expecting.
How can I get the type of a Nullable<>
instance?
Keep in mind that I don't know the underlying type of the nullable instance, so I cannot call typeof
.
Not elegant, but you can wrap it in a generic function:
public static void Main()
{
int? i = 2;
Console.WriteLine( type(i) );
}
public static Type type<T>( T x ) { return typeof(T); }
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments