Finding datatype for Nullable values

Posts   
 
    
staleb
User
Posts: 19
Joined: 30-Jan-2007
# Posted on: 15-Apr-2008 16:31:05   

Hi

How can I find the "real" DataType for Nullable EntityField2 field. Lest say its int32, how can I get this Datatype? Instead of: "System.Nullable`1[[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]"

goose avatar
goose
User
Posts: 392
Joined: 06-Aug-2007
# Posted on: 15-Apr-2008 19:33:39   

how are you obtaining this value?

"System.Nullable`1[[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]"

sorry if i misread your post, i believe this is what your are looking for?

Nullable<int> i = 0;
Type t = i.GetType().UnderlyingSystemType;

staleb
User
Posts: 19
Joined: 30-Jan-2007
# Posted on: 15-Apr-2008 22:05:51   

Hi here is the start of my code. I loop trhough every field int the entity and store the values acording to datatype (At least thats what I want)

One of the columns in my table is an int but also nullable.

foreach (EntityField2 field in (EntityFields2)entDeviation.Fields) { Type type = field.DataType.UnderlyingSystemType;

more code.......

type is still Nullable, "System.Nullable`1[[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]"


Nullable<int> i = 0;
Type t = i.GetType().UnderlyingSystemType;

Seems to work if i has value, but in this case, field is null (Since its a new Enity)

Any Ideas?

This is driving me crazy. I thought my code was working,but.....

staleb
User
Posts: 19
Joined: 30-Jan-2007
# Posted on: 16-Apr-2008 13:37:22   

Solved it:

Type type = field.DataType.UnderlyingSystemType; Nullable.GetUnderlyingType(type).Name

Max avatar
Max
User
Posts: 221
Joined: 14-Jul-2006
# Posted on: 16-Apr-2008 14:35:48   

staleb wrote:

Hi here is the start of my code. I loop trhough every field int the entity and store the values acording to datatype (At least thats what I want)

One of the columns in my table is an int but also nullable.

foreach (EntityField2 field in (EntityFields2)entDeviation.Fields) { Type type = field.DataType.UnderlyingSystemType;

more code.......

type is still Nullable, "System.Nullable`1[[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]"


Nullable<int> i = 0;
Type t = i.GetType().UnderlyingSystemType;

Seems to work if i has value, but in this case, field is null (Since its a new Enity)

Any Ideas?

This is driving me crazy. I thought my code was working,but.....

Nullable type are generic type, so you should be able to retrieve the underlying type by doing

Type type = field.DataType.GetGenericArguments(0);

The GetGenericArguments method return an array of type used to parametrize the generic type.

I use this technique for retrieving the entityType from generic entityCollection or entityView

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39588
Joined: 17-Aug-2003
# Posted on: 16-Apr-2008 16:34:01   

Be aware to check if the type is generic first, otherwise getgenericargument throws an exception.


public static bool IsNullableType(Type toCheck)
{
    if((toCheck == null) || !toCheck.IsValueType)
    {
        return false;
    }
    return (toCheck.IsGenericType && toCheck.GetGenericTypeDefinition() == (typeof(Nullable<>)));
}

Frans Bouma | Lead developer LLBLGen Pro