Home All Groups Group Topic Archive Search About

Nullable types problerm

Author
15 Dec 2006 2:05 PM
oscar.acostamontesde@googlemail.com
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

Author
15 Dec 2006 2:19 PM
oscar.acostamontesde@googlemail.com
oscar.acostamonte***@googlemail.com ha escrito:

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

Well, I found this:
http://msdn2.microsoft.com/en-us/library/ms366789(VS.80).aspx

How to get the underlying type of a variable without calling GetType()??
Author
15 Dec 2006 3:13 PM
Yves. L.
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
>
Author
16 Dec 2006 12:56 AM
RobinS
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
>>

AddThis Social Bookmark Button