Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Nullable & reflection

2 views
Skip to first unread message

Lloyd Dupont

unread,
Nov 18, 2005, 10:43:02 PM11/18/05
to
I have written a class which automatically fills up an object's field from a
DbDataReader.
I'm trying to do some data type conversion before assigning the fields.

Unfortunately Convert.ChangetType((long) x, typeof(Nullable<long>)); throw
an InvalidCastException.

So I wonder, how could I, from the type information such as
Type aType = typeof(Nullable<long>);

and an object
object obj = 42L;

convert the object obj to the type of aType?


Lloyd Dupont

unread,
Nov 18, 2005, 11:07:46 PM11/18/05
to
found a fix:
if (ft.IsGenericType && ft.GetGenericTypeDefinition() == typeof(Nullable<>))
{
Type tval = ft.GetGenericArguments()[0];
object val = Convert.ChangeType(o, tval);
return Activator.CreateInstance(ft, val);
}

"Lloyd Dupont" <net.galador@ld> wrote in message
news:eKG3%23sL7F...@TK2MSFTNGP12.phx.gbl...

0 new messages