Home All Groups Group Topic Archive Search About

Reflection and Nullables

Author
13 Jul 2006 4:01 PM
Joe Bloggs
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:

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.

Author
13 Jul 2006 4:09 PM
Larry Lard
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.

Show quoteHide quote
>   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
Are all your drivers up to date? click for free checkup

Author
13 Jul 2006 4:55 PM
Joe Bloggs
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
Author
13 Jul 2006 5:27 PM
Jon Shemitz
Joe Bloggs wrote:

> 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.

For a local variable, maybe not. (Though why you would need to use
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.
Author
13 Jul 2006 6:16 PM
Barry Kelly
"Joe Bloggs" <es***@rocketmail.com> wrote:

> 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.

If the variable is the value of a field, you can use
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

Author
13 Jul 2006 6:23 PM
Jon Shemitz
Barry Kelly wrote:

> You could use generic type parameter
> inference to get what you want:

>     static Type GetType<T>(T value)

Very clever!

--

..NET 2.0 for Delphi Programmers        www.midnightbeach.com/.net
Delphi skills make .NET easy to learn  In print, in stores.
Author
14 Jul 2006 2:35 AM
Joe Bloggs
Jon Shemitz wrote:

> Barry Kelly wrote:
>
> > You could use generic type parameter
> > inference to get what you want:
>
> >     static Type GetType<T>(T value)
>
> Very clever!

This is it.... this is what I am looking for...

Thanks Barry.
Author
13 Jul 2006 6:14 PM
Barry Kelly
"Joe Bloggs" <es***@rocketmail.com> wrote:

> Can someone please kindly show me how to determine if a type (read
> value
> type) is Nullable.

I also answered in your multipost in dotnet.general

-- Barry


Bookmark and Share