<dm********@openspan.comwrote:
I have scenario where I need to create an object of an unknown type.
Reference types work fine. I call Type.GetConstructors(), let the
user pick a ConstructorInfo and then call ConstructorInfo.Invoke().
However, for the value types I have tested (like Int32)
GetConstructors doesn't return anything.
Use Activator.CreateInstance to create a value, not passing in any
constructor parameters. Although at the CLR level value types don't
have parameterless constructors, Activator.CreateInstance treats them
as if they do. (At the C# level they do, according to the spec. It's a
bit confusing in terminology terms, to be honest - but the behaviour is
simple.)
Here's an example:
using System;
class Test
{
static void Main()
{
int i = (int) Activator.CreateInstance(typeof(int));
Console.WriteLine (i);
}
}
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog:
http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too