|
ms
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Reflection and NullablesCan someone please kindly show me how to determine if a type (read value type) is Nullable. MSDN has this KB: How to: Identify a Nullable Type (C# Programming Guide) http://msdn2.microsoft.com/en-us/library/ms366789.aspx however, using their code snippet, I couldn't get it to work: public class MyClass { public static void Main() { Nullable<int> j = new Nullable<int>(20); Type type = j.GetType(); if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>)) { Console.WriteLine("j is Nullable"); } else Console.WriteLine("j is NOT Nullable"); Console.WriteLine("Type of j is {0}", j.GetType()); } } The Output I got is: j is NOT Nullable Type of j is System.Int32 thus, is there no way to determine a nullable or an non-nullable value types? Thanks for your help in advanced. Joe Bloggs wrote:
> Hi, That very page includes the following text:> > Can someone please kindly show me how to determine if a type (read > value > type) is Nullable. > > MSDN has this KB: > How to: Identify a Nullable Type (C# Programming Guide) > http://msdn2.microsoft.com/en-us/library/ms366789.aspx > > however, using their code snippet, I couldn't get it to work: >> if you attempt to obtain type information from Nullable variables at.... runtime using the GetType method or the is operator, the result is a Type object that represents the underlying type, not the Nullable type itself. .... GetType always returns a Type object that represents the underlying type, not the Nullable type. .... int? i = 5; Type t = i.GetType(); Console.WriteLine(t.FullName); //"System.Int32" .... Remember that this code [the test you reproduce below] ****always returns false if the Type object was returned from a call to GetType**** >> So this will be the underlying type.> > public class MyClass > { > public static void Main() > { > Nullable<int> j = new Nullable<int>(20); > Type type = j.GetType(); Show quoteHide quote > if (type.IsGenericType && type.GetGenericTypeDefinition() == There is, and you have it, but it doesn't work if you get those types> typeof(Nullable<>)) > { > Console.WriteLine("j is Nullable"); > } > else > Console.WriteLine("j is NOT Nullable"); > > Console.WriteLine("Type of j is {0}", j.GetType()); > } > } > > The Output I got is: > > j is NOT Nullable > Type of j is System.Int32 > > thus, is there no way to determine a nullable or an non-nullable value > types? "from Nullable variables at runtime using the GetType method or the is operator". -- Larry Lard Replies to group please Ok,
Does that mean that there is no way to use reflection to introspect a variable if it is a Nullable type? I don't remember any other methods other than GetType() from retriving the Type from a variable. Any suggestions? Larry Lard wrote: Show quoteHide quote > Joe Bloggs wrote: > > Hi, > > > > Can someone please kindly show me how to determine if a type (read > > value > > type) is Nullable. > > > > MSDN has this KB: > > How to: Identify a Nullable Type (C# Programming Guide) > > http://msdn2.microsoft.com/en-us/library/ms366789.aspx > > > > however, using their code snippet, I couldn't get it to work: > > That very page includes the following text: > >> > ... > if you attempt to obtain type information from Nullable variables at > runtime using the GetType method or the is operator, the result is a > Type object that represents the underlying type, not the Nullable type > itself. > ... > GetType always returns a Type object that represents the underlying > type, not the Nullable type. > ... > > int? i = 5; > Type t = i.GetType(); > Console.WriteLine(t.FullName); //"System.Int32" > ... > Remember that this code [the test you reproduce below] ****always > returns false if the Type object was returned from a call to > GetType**** > >> > > > > > public class MyClass > > { > > public static void Main() > > { > > Nullable<int> j = new Nullable<int>(20); > > Type type = j.GetType(); > > So this will be the underlying type. > > > if (type.IsGenericType && type.GetGenericTypeDefinition() == > > typeof(Nullable<>)) > > { > > Console.WriteLine("j is Nullable"); > > } > > else > > Console.WriteLine("j is NOT Nullable"); > > > > Console.WriteLine("Type of j is {0}", j.GetType()); > > } > > } > > > > The Output I got is: > > > > j is NOT Nullable > > Type of j is System.Int32 > > > > thus, is there no way to determine a nullable or an non-nullable value > > types? > > There is, and you have it, but it doesn't work if you get those types > "from Nullable variables at runtime using the GetType method or the is > operator". > > -- > Larry Lard > Replies to group please Joe Bloggs wrote:
> Does that mean that there is no way to use reflection to introspect a For a local variable, maybe not. (Though why you would need to use> variable if it is a Nullable type? I don't remember any other methods > other than GetType() from retriving the Type from a variable. Reflection to tell if a local - that you declared! - is a nullable type is beyond me.) However, in the much more understandable case where you need to tell if a particular field (or property, or method result) in a particular type is nullable, you CAN use Reflection. For example, this prints True twice: using System; using System.Collections.Generic; using System.Text; namespace DetectNullableType { class Program { static void Main(string[] args) { Type Class = typeof(Demo); IsNullable(Class.GetField("Field").FieldType); IsNullable(Class.GetMethod("Method").ReturnType); Console.ReadLine(); } static void IsNullable(Type T) { Console.WriteLine(T.IsGenericType && T.GetGenericTypeDefinition() == typeof(Nullable<>)); } } class Demo { public int? Field; public bool? Method() { return null; } } } -- ..NET 2.0 for Delphi Programmers www.midnightbeach.com/.net Delphi skills make .NET easy to learn In print, in stores. "Joe Bloggs" <es***@rocketmail.com> wrote: If the variable is the value of a field, you can use> Does that mean that there is no way to use reflection to introspect a > variable if it is a Nullable type? I don't remember any other methods > other than GetType() from retriving the Type from a variable. FieldInfo.FieldType. If it's a local, you can use typeof(int?) or whatever the declared type is. You could use generic type parameter inference to get what you want: ---8<--- using System; class App { static void Main() { int? x = 0; Console.WriteLine(GetType(x).FullName); } static Type GetType<T>(T value) { return typeof(T); } } --->8--- This will print: System.Nullable`1[[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] -- Barry Barry Kelly wrote:
> You could use generic type parameter Very clever!> inference to get what you want: > static Type GetType<T>(T value) -- ..NET 2.0 for Delphi Programmers www.midnightbeach.com/.net Delphi skills make .NET easy to learn In print, in stores. Jon Shemitz wrote:
> Barry Kelly wrote: This is it.... this is what I am looking for... > > > You could use generic type parameter > > inference to get what you want: > > > static Type GetType<T>(T value) > > Very clever! Thanks Barry.
Other interesting topics
Convert HTML to Image
Visual studio 2005 Julian dates question Interface Question Parsing a complicated date time connect to Oracle server by using csharp WinForm Communications in .NET What's a practical use of .Net remoting? Reporting Mirror / rotate image ( System.Drawing.Graphics / System.Drawing.Drawing2D.Matrix) |
|||||||||||||||||||||||