J
Jeff Brown
Hi,
I'm trying to implement the functionality of the generic expression,
"default(T)", but for a given instance of the Type class.
The ideal would be if the Type class had a method like "object
Type.MakeDefaultInstance()" -- but it doesn't.
Here's my attempt, but it seems rather kludgy. Is there a better way?
static public object MakeDefaultInstance(Type type)
{
if (type.IsValueType)
return Activator.CreateInstance(type);
else
return null;
}
Thanks,
Jeff Brown
I'm trying to implement the functionality of the generic expression,
"default(T)", but for a given instance of the Type class.
The ideal would be if the Type class had a method like "object
Type.MakeDefaultInstance()" -- but it doesn't.
Here's my attempt, but it seems rather kludgy. Is there a better way?
static public object MakeDefaultInstance(Type type)
{
if (type.IsValueType)
return Activator.CreateInstance(type);
else
return null;
}
Thanks,
Jeff Brown