Nullable types problerm

  • Thread starter oscar.acostamontesde
  • Start date
O

oscar.acostamontesde

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
 
O

oscar.acostamontesde

(e-mail address removed) ha escrito:
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()??
 
Y

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
 
R

RobinS

That's very clever.

Robin S.
----------------------------
Yves. L. said:
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


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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top