Author: Joanna Carter [TeamB]Joanna Carter [TeamB]
Date: Jun 19, 2007 03:57
Borland a écrit :
> Referring to that posting from December last year re "class of"... I
> understand the use of Activator.CreateInstance(ClassType) as a replacement
> for the Delphi "class of" for use in a C# abstract class factory, but how
> would I use that ClassType reference to call a static method (eg public
> static DoIt()) which has been declared in the Class class?
>
> In Delphi I would simply retrieve the "class of" reference, (eg ClassClass -
> class of TClass), and call the function normally - eg
> TClass(ClassClass).DoIt;
First, can I ask you to change your posting name from Borland ?
Now, to the matter in hand.
Don't forget that, in C#, static methods cannot be virtual. Therefore,
you have to redlclare the method in each and every derived class but
simply calling the static method on the base type will only call that
method, not the derived one.
However, here is a simple example fo how to create a factory class that
will call the correct method, depending on the type passed in.
|