|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Nullable types problermI'm tryn to detect if a type is a nullable type. I'm using the following code: using System; using System.Reflection; using System.Collections.Generic; public class Test { public static void Main() { Nullable<int> n = 0; Console.WriteLine(IsNullableType(n.GetType())); Console.ReadLine(); } static bool IsNullableType(Type theType){ return (theType.GetType().IsGenericType && theType.GetGenericTypeDefinition().Equals(typeof(Nullable<>))); } } First call in IsNullableType returns false, where i think should return true. And if i remove this first test (theType.GetType().IsGenericType) it launch a System.InvalidOperationException. Anyone knows why is it hapening, and/or how to detect if a type is a nullable type? Thanks!! Oscar Acosta This code launch an exception, and oscar.acostamonte***@googlemail.com ha escrito:
Show quote > Hello: Well, I found this:> I'm tryn to detect if a type is a nullable type. I'm using the > following code: > > using System; > using System.Reflection; > using System.Collections.Generic; > > public class Test > { > > > public static void Main() > { > Nullable<int> n = 0; > Console.WriteLine(IsNullableType(n.GetType())); > Console.ReadLine(); > } > > static bool IsNullableType(Type theType){ > return (theType.GetType().IsGenericType && > theType.GetGenericTypeDefinition().Equals(typeof(Nullable<>))); > } > > } > > First call in IsNullableType returns false, where i think should return > true. And if i remove this first test (theType.GetType().IsGenericType) > it launch a System.InvalidOperationException. Anyone knows why is it > hapening, and/or how to detect if a type is a nullable type? Thanks!! > > Oscar Acosta http://msdn2.microsoft.com/en-us/library/ms366789(VS.80).aspx How to get the underlying type of a variable without calling GetType()?? Method overloading to the rescue....
This Works static void Main(string[] args) { Nullable<int> n = 0; int i = 0; Console.WriteLine(IsGeneric(n)); Console.WriteLine(IsGeneric(i)); Console.ReadLine(); } public static bool IsGeneric (Nullable<int> n) { return true; } public static bool IsGeneric (int i) { return false; } Yvesl oscar.acostamonte***@googlemail.com wrote: Show quote > Hello: > I'm tryn to detect if a type is a nullable type. I'm using the > following code: > > using System; > using System.Reflection; > using System.Collections.Generic; > > public class Test > { > > > public static void Main() > { > Nullable<int> n = 0; > Console.WriteLine(IsNullableType(n.GetType())); > Console.ReadLine(); > } > > static bool IsNullableType(Type theType){ > return (theType.GetType().IsGenericType && > theType.GetGenericTypeDefinition().Equals(typeof(Nullable<>))); > } > > } > > First call in IsNullableType returns false, where i think should return > true. And if i remove this first test (theType.GetType().IsGenericType) > it launch a System.InvalidOperationException. Anyone knows why is it > hapening, and/or how to detect if a type is a nullable type? Thanks!! > > Oscar Acosta > This code launch an exception, and > That's very clever.
Robin S. ---------------------------- Show quote "Yves. L." <Yv***@discussions.microsoft.com> wrote in message news:%237amHuFIHHA.1064@TK2MSFTNGP04.phx.gbl... > Method overloading to the rescue.... > This Works > > > static void Main(string[] args) > { > Nullable<int> n = 0; > int i = 0; > > Console.WriteLine(IsGeneric(n)); > Console.WriteLine(IsGeneric(i)); > > Console.ReadLine(); > } > > public static bool IsGeneric (Nullable<int> n) > { > return true; > } > public static bool IsGeneric (int i) > { > return false; > } > Yvesl > > > oscar.acostamonte***@googlemail.com wrote: >> Hello: >> I'm tryn to detect if a type is a nullable type. I'm using the >> following code: >> >> using System; >> using System.Reflection; >> using System.Collections.Generic; >> >> public class Test >> { >> >> >> public static void Main() >> { >> Nullable<int> n = 0; >> Console.WriteLine(IsNullableType(n.GetType())); >> Console.ReadLine(); >> } >> >> static bool IsNullableType(Type theType){ >> return (theType.GetType().IsGenericType && >> theType.GetGenericTypeDefinition().Equals(typeof(Nullable<>))); >> } >> >> } >> >> First call in IsNullableType returns false, where i think should >> return >> true. And if i remove this first test >> (theType.GetType().IsGenericType) >> it launch a System.InvalidOperationException. Anyone knows why is it >> hapening, and/or how to detect if a type is a nullable type? Thanks!! >> >> Oscar Acosta >> This code launch an exception, and >> |
|||||||||||||||||||||||