N
not_a_commie
It seems that the only way to construct a struct from a type is to use
Activator.CreateInstance. Is that true? Can anyone improve
(performance-wise) upon this function below:
/// <summary>
/// Create an object of the given type
/// A default constructor is required to be successful.
/// A failure will return null.
/// </summary>
/// <param name="type">The type of the object to create.</param>
/// <param name="constructorParams">Arguments for the desired
constructor.</param>
public static object CreateObject(Type type, params object[]
constructorParams)
{
if (type == null)
throw new ArgumentNullException("Type");
try
{
if (type.IsArray)
{
int[] dimensions = new int[constructorParams.Length];
for (int i = 0; i < constructorParams.Length; i++)
dimensions = (int)constructorParams;
return Array.CreateInstance(type.GetElementType(), dimensions);
}
else if (type == typeof(string))
{
return "";
}
else
{
// the easy and slow way:
//return Activator.CreateInstance(type, constructorParams);
if (constructorParams == null || constructorParams.Length <= 0)
{
ConstructorInfo ci = type.GetConstructor(Type.EmptyTypes);
if (ci == null) // stupid structs and their default
constructors....
return Activator.CreateInstance(type); // it does cache the most
recent 16 types
DynamicMethod dm = new DynamicMethod("MyCtor", type,
Type.EmptyTypes, typeof(ClassFactory).Module, true);
ILGenerator ilgen = dm.GetILGenerator();
ilgen.Emit(OpCodes.Nop);
ilgen.Emit(OpCodes.Newobj, ci);
ilgen.Emit(OpCodes.Ret);
return ((CtorDelegate)dm.CreateDelegate(typeof(CtorDelegate)))();
// we could cache these delegates with a type lookup
// but preparation time is really very minimal
// see here: http://blogs.msdn.com/haibo_luo/archive/2005/11/17/494009.aspx
// and here: http://blogs.msdn.com/haibo_luo/articles/494008.aspx
}
else
{
Type[] constructorTypes = new Type[constructorParams.Length];
for (int i = 0; i < constructorParams.Length; i++)
constructorTypes = constructorParams.GetType();
// DynamicMethod is not quite as fast in this case
// where we don't have a delegate matching the function
// This should be really rare in deserialization, though
ConstructorInfo ci = type.GetConstructor(constructorTypes);
if (ci == null) // stupid structs and their default
constructors....
return Activator.CreateInstance(type); // it does cache the most
recent 16 types
return ci.Invoke(constructorParams);
}
// another thing we could do is cache IClonables and just return
cloned copies in that case
}
}
catch (Exception e)
{
if (e.InnerException is LicenseException)
throw e.InnerException;
throw new ClassFactoryException("Error constructing object " +
type.Name, e);
}
}
private delegate object CtorDelegate();
Activator.CreateInstance. Is that true? Can anyone improve
(performance-wise) upon this function below:
/// <summary>
/// Create an object of the given type
/// A default constructor is required to be successful.
/// A failure will return null.
/// </summary>
/// <param name="type">The type of the object to create.</param>
/// <param name="constructorParams">Arguments for the desired
constructor.</param>
public static object CreateObject(Type type, params object[]
constructorParams)
{
if (type == null)
throw new ArgumentNullException("Type");
try
{
if (type.IsArray)
{
int[] dimensions = new int[constructorParams.Length];
for (int i = 0; i < constructorParams.Length; i++)
dimensions = (int)constructorParams;
return Array.CreateInstance(type.GetElementType(), dimensions);
}
else if (type == typeof(string))
{
return "";
}
else
{
// the easy and slow way:
//return Activator.CreateInstance(type, constructorParams);
if (constructorParams == null || constructorParams.Length <= 0)
{
ConstructorInfo ci = type.GetConstructor(Type.EmptyTypes);
if (ci == null) // stupid structs and their default
constructors....
return Activator.CreateInstance(type); // it does cache the most
recent 16 types
DynamicMethod dm = new DynamicMethod("MyCtor", type,
Type.EmptyTypes, typeof(ClassFactory).Module, true);
ILGenerator ilgen = dm.GetILGenerator();
ilgen.Emit(OpCodes.Nop);
ilgen.Emit(OpCodes.Newobj, ci);
ilgen.Emit(OpCodes.Ret);
return ((CtorDelegate)dm.CreateDelegate(typeof(CtorDelegate)))();
// we could cache these delegates with a type lookup
// but preparation time is really very minimal
// see here: http://blogs.msdn.com/haibo_luo/archive/2005/11/17/494009.aspx
// and here: http://blogs.msdn.com/haibo_luo/articles/494008.aspx
}
else
{
Type[] constructorTypes = new Type[constructorParams.Length];
for (int i = 0; i < constructorParams.Length; i++)
constructorTypes = constructorParams.GetType();
// DynamicMethod is not quite as fast in this case
// where we don't have a delegate matching the function
// This should be really rare in deserialization, though
ConstructorInfo ci = type.GetConstructor(constructorTypes);
if (ci == null) // stupid structs and their default
constructors....
return Activator.CreateInstance(type); // it does cache the most
recent 16 types
return ci.Invoke(constructorParams);
}
// another thing we could do is cache IClonables and just return
cloned copies in that case
}
}
catch (Exception e)
{
if (e.InnerException is LicenseException)
throw e.InnerException;
throw new ClassFactoryException("Error constructing object " +
type.Name, e);
}
}
private delegate object CtorDelegate();