Create an object from an object.

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!)
 
R

Robert Jordan

James said:
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?

Activator.CreateInstance(obj.GetType)

bye
Rob
 
R

Richard Blewett [DevelopMentor]

A little better, but to be honest your pretty much right on the button:

public object CreateNew(object obj)
{
Type t = obj.GetType();
ConstructorInfo ctorInfo = t.GetConstructor(type.EmptyTypes);

if( ctorInfo != null ) // better make sure there is a default ctor
return ctorInfo.Invoke(null);
else
return null;
}

Regards

Richard Blewett - DevelopMentor

http://staff.develop.com/richardb/weblog

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<[email protected]>

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!)



---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.766 / Virus Database: 513 - Release Date: 17/09/2004



[microsoft.public.dotnet.languages.csharp]
 
R

Richard Blewett [DevelopMentor]

LOL - fantastic!

I was so tied up in the implementation I didn't really think through the problem :) Much better solution.

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<[email protected]>

James said:
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?

Activator.CreateInstance(obj.GetType)

bye
Rob

---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.766 / Virus Database: 513 - Release Date: 17/09/2004



[microsoft.public.dotnet.languages.csharp]
 
J

James Curran

Richard Blewett said:
LOL - fantastic!

I was so tied up in the implementation I didn't really think through the
problem :) Much better solution.

Although, as it turns out, in the project I planned (which has been
abandoned for other reasons), I'd want to create a number of these object,
so I'd probably want to use yours, separating the first & second halves, and
caching ctorInfo.

--
Truth,
James Curran
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
(note new day job!)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top