Home All Groups Group Topic Archive Search About

Creating a nullable type using Activator

Author
19 May 2006 4:44 PM
DerekG
Hi

I am trying to create an instance of a nullable type using System.Activator
for example

t is passed in from the caller and is the Type info for a nullable type or
can be a standard type i.e string, int etc.

object value = Activator.CreateInstance(t);

value always returns null

Author
19 May 2006 4:58 PM
Mattias Sjögren
>value always returns null

That makes sense since you haven't specified a value for the instance.


Mattias

--
Mattias Sjögren [C# MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Author
19 May 2006 5:10 PM
Giuseppe Lippolis
Hello DerekG,
            object o = typeof(Nullable<>).MakeGenericType(new Type[] { typeof(int)
});

Show quote
> Hi
>
> I am trying to create an instance of a nullable type using
> System.Activator for example
>
> t is passed in from the caller and is the Type info for a nullable
> type or can be a standard type i.e string, int etc.
>
> object value = Activator.CreateInstance(t);
>
> value always returns null
>
Author
22 May 2006 1:09 PM
DerekG
Hi Giuseppe,

Thanks for the reply, this has helped me solve the problem. Thanks for your
help.

Show quote
"Giuseppe Lippolis" wrote:

> Hello DerekG,
>             object o = typeof(Nullable<>).MakeGenericType(new Type[] { typeof(int)
> });
>
> > Hi
> >
> > I am trying to create an instance of a nullable type using
> > System.Activator for example
> >
> > t is passed in from the caller and is the Type info for a nullable
> > type or can be a standard type i.e string, int etc.
> >
> > object value = Activator.CreateInstance(t);
> >
> > value always returns null
> >
>
>
>

AddThis Social Bookmark Button