Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

Opposite of typeof /GetType()

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ade - 10 Mar 2008 14:01 GMT
Whereas typeof gets a Type from a class, I need to get a class from a
Type so that I may create the type at runtime.

Imagine if you wil

class foo
{
   public foo bar()
   {
       foo newFoo = new GetType();
       // Some magic goes here
       return newFoo;
   }
}

What would be the syntactically-correct way to do this? I want to
create, from the base class, a new instance of whatever type the
instance of foo is. eg calling bar() on

class superfoo : foo

will give me something unboxable to a superfoo.
Marc Gravell - 10 Mar 2008 14:12 GMT
I'm assuming that this is an extension of the ArrayList converstaion,
so generics etc are out of the question...

How about:

foo newFoo = (foo) Activator.CreateInstance(GetType())?

Another option is a virtual method for creating a new instanc, but
this has a maintenance cost (i.e. you need to remember to override
it). But might be an option if you can't guarantee a default ctor.

Alternatively - I already posted an example that used
MemberwiseClone() to do this, changing the single property afterwards.
Since this is essentially a blit, it should out-perform any reflection-
based implementations.

Marc
Jon Skeet [C# MVP] - 10 Mar 2008 14:13 GMT
> Whereas typeof gets a Type from a class I need to get a class from a
> Type so that I may create the type at runtime.
[quoted text clipped - 18 lines]
>
> will give me something unboxable to a superfoo.

Well, I don't think boxing/unboxing actually comes into it, but I
suspect what you're after is:

Activator.CreateInstance(GetType());

Note that it will only work if there's a parameterless constructor - if
you don't have a parameterless constructor, you'll need to specify
constructor parameters too.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

pipo - 10 Mar 2008 17:03 GMT
Sorry, if I misunderstood your question.

public class Foo<T>
where T : new()
{
protected T bar()
{
return new T();
}
}
public class SuperFoo : Foo<SuperFoo>
{
//Class code
}

SuperFoo  super = new Foo<SuperFoo>().bar();

> Whereas typeof gets a Type from a class, I need to get a class from a
> Type so that I may create the type at runtime.
[quoted text clipped - 18 lines]
>
> will give me something unboxable to a superfoo.

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.