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