Create new Form of the same Type as an existing Instance

T

tarnap

Hello,

I have an instance of a form, lets say formA.
This form is passed to a method that must create a new form, lets say
formB of the same type.
How can this be accomplished except by using reflection eg;
Type FormType = (formA.GetType());
Form formB = (Form)
Activator.CreateInstance(FormType);

Thank you.
 
M

Marc Gravell

Well, if you own the forms, you could always define a factory interface -
similar to ICloneable, but returning a vanilla one of the same? Not ideal
though (it would need constant override methods to work with inheritance,
and doesn't work for existing Form (w/o the interface) instances).

Perhaps reflection is the lesser evil in this scenario, and (lets face it)
in WinForms there's more reflection going on than you would believe, so a
little more isn't going to hurt. And it is a fairly innocent use of
reflection anyway (you aren't hacking around private fields, etc)

Marc
 
G

gerhard.stephan

Hi,

in your scneario I would recommend to use a factory method. In order to
implement one, you have to write an interface with a single
CreateInstance Method. Let FormA implement that interface (which
schould return an instance of itself).

Cheers
Gerhard

PS: Creating an instance with reflection cost time and is more hard to
implement and to maintain.

Always have a look at: http://jachman.wordpress.com
 
M

Marc Gravell

Creating an instance with reflection cost time and
is more hard to implement and to maintain.

Harder to implement and maintain?

With the OP's suggested Activator.CreateInstance approach, he needs to do,
erm, nothing. Except keep a default ctor, which most Form types are likely
to have anyway... with the factory approach you have an additional (albeit
trivial) interface, which you need to keep implementing. And every time you
subclass you need to override the implementation, otherwise if "class A :
IFactory", and "class B : A", then "B : IFactory" automatically, but unless
you *always* remember to override, calling b.CreatInstance() will return a
new A, not a new B. It also can't be used on Form types that you don't own.

Generics /might/ give a workable solution in this scenario via the "where T
: new()" clause, but I doubt the type is fixed at runtime (although maybe a
little redesign using generics and interfaces might yield a successful
conclusion).

Finally, in forms performance, I don't think anybody is going to really
notice the tiny performance hit for using reflection. You don't generally
create forms in a tight loop, and there is enough going on anyway (PInvoke
to get OS handles and set Win32 flags, reflection internals inside WinForms
itself, etc) that it is *absolutely* a premature optimisation to worry about
this.

So: the premature optimisation *introduces* a maintenance overhead
(subclassing etc), and a potential cause of bugs (forgetting to override
etc), and can't be applied as widely... personally I'd stick with reflection
here.

Marc
 

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