J
James Curran
Given an object on unknown type, I'd like to create a new
(default-constructed) object of the same type. After some playing with
Reflection (my first foray into that area), I came up with the following:
public object CreateNew(object obj)
{
Type t = obj.GetType();
ConstructorInfo ctorInfo = t.GetConstructor(new Type[0]);
return ctorInfo.Invoke(new object[0]);
}
Is there a better way?
--
Truth,
James Curran
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
(note new day job!)
(default-constructed) object of the same type. After some playing with
Reflection (my first foray into that area), I came up with the following:
public object CreateNew(object obj)
{
Type t = obj.GetType();
ConstructorInfo ctorInfo = t.GetConstructor(new Type[0]);
return ctorInfo.Invoke(new object[0]);
}
Is there a better way?
--
Truth,
James Curran
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
(note new day job!)