static class ...err.. pointer

  • Thread starter Thread starter evaristobo
  • Start date Start date
E

evaristobo

i'm a bit confused here, a pointer is something aiming to an object,
but a static class is not an object, nevertheless i would like to have
the possibility of doing something like handling an HashTable and
getting from it a pointer to a static class :
( (classStatic)myHash("classStatic") ).staticMethod()

....is this possible, or i'm i short on reading?
 
evaristobo said:
i'm a bit confused here, a pointer is something aiming to an object,
but a static class is not an object, nevertheless i would like to have
the possibility of doing something like handling an HashTable and
getting from it a pointer to a static class :
( (classStatic)myHash("classStatic") ).staticMethod()

Since static methods apply to the class as a whole rather than specific
instances of it, then you could achieve the above more simply with:

classStatic.staticMethod();

There's no need to have an instance of it.
 
i'm a bit confused here, a pointer is something aiming to an object,
but a static class is not an object, nevertheless i would like to have
the possibility of doing something like handling an HashTable and
getting from it a pointer to a static class :
( (classStatic)myHash("classStatic") ).staticMethod()

...is this possible, or i'm i short on reading?

So you want to index different types in order to access one
identically named static method on one of them?

It's not as easy as storing multiple objects, but it's certainly
possible using a mechanism known as "reflection".

Variant 1. Store the class name (e.g. "classStatic") in your
hashtable, and then use reflection to get the TypeInfo object for the
class, then the MethodInfo object for your method, then invoke the
method. That works but it's cumbersome and slow to execute.

Variant 2. Same as 1, but when adding a new element to your hashtable
you immediately determine the TypeInfo and store that instead of the
class name. Even better, if you always call just one method you
determine its MethodInfo object and store that.

Variant 3. Same as 2, but you declare a delegate that matches your
staticMethod, and when adding a new hashtable element you store a
delegate object for the new type's method, not a MethodInfo object.

Variant 4. Same as 3, but you avoid reflection altogether by having
the static classes add themselves to the hashtable. They can
immediately create a delegate for one of their own methods, without
having to use reflection and run-time name lookups. Big disadvantage
here: each of the static classes must have an "adder" method, or else
some central "adder" method has to have all static classes hardcoded.
 
....thank you very much Chris and Bobbo, but i was trying to escape
Reflection here, you see, i'm trying to design a registry object, which
is nothing more than an hashtable for the client code to query it for
objects who will follow a strict naming convention, this convention is
drawn from what i want the application framework to be, or better, to
supply. As an example, all application will have to supply a 'logger'
object, a 'dbConnection' object, a 'threadManager' object, and so on.
And then i want to get the 'extension.functionalityX' plugin which is
loaded from the plugins folder and implements MyFramework.IPlugin
interface...what do you think about this, how do you design your app
frame?
 
evaristobo said:
i'm a bit confused here, a pointer is something aiming to an object,
but a static class is not an object, nevertheless i would like to have
the possibility of doing something like handling an HashTable and
getting from it a pointer to a static class :
( (classStatic)myHash("classStatic") ).staticMethod()

...is this possible, or i'm i short on reading?

Whenever this happens to me, I resort to singletons. Or, possibly,
sort-of-singletons that can participate in inheritance.

The other situation that causes me to think "singleton" is wanting a
static class that I can pass to a method as an argument, again because
I want polymorphism.

If you make your static class a singleton that implements an interface,
say IMyInterface, then you can create a hash table of IMyInterface
objects. The fact that the class is a singleton means that there can
only ever be one of them, and it's accessible from the static space,
viz:

ClassSingleton.Instance.InstanceMethod();
 
If you make your static class a singleton that implements an interface,
say IMyInterface, then you can create a hash table of IMyInterface
objects. The fact that the class is a singleton means that there can
only ever be one of them, and it's accessible from the static space,
viz:

ClassSingleton.Instance.InstanceMethod();

.....that's it! that's my solution for this problem, i have an
application registry as a Registry class, itself a singleton too,
implementing an interface IRegistry, for example. Then i have a
Singleton singleton class accessible at the startup procedure when the
core framework is putting its act together, then i register my
singleton like
Registry.register("mysingleton",Singleton.getInstance());
then all i have to do is supply a reference to Registry for whichever
object needing to use it by the IRegistry interface, which must be
located in a public assembly, well, but that's another story...

I can thank you enough
best regards
joao viegas
 
Back
Top