Dictionary of classes

H

Holger

I have built some custom classes that are built on the same base
class, but the have all a different method called Calc.
I would like to put them in one dictionary and use the calc method.

Is there a way to add them in one dictionary without using a generic
object and boxing/unboxing them.
I'm looking for something more elegant.

Holger
 
M

Marcel Müller

Holger said:
I have built some custom classes that are built on the same base
class, but the have all a different method called Calc.
I would like to put them in one dictionary and use the calc method.

Is there a way to add them in one dictionary without using a generic
object and boxing/unboxing them.
I'm looking for something more elegant.

Look for the multiton pattern. It's like a singleton, but it adds some
key to the type and for each key no more than one object may exist in
memory.

Technically you can add a static repository in the base class.
public static readonly IList<CalcBase> AllInstances;
With an abstract function
public abstract SomeResult Calc(Parameter);
you will be able to invoke Calc without ugly downcasts.

You may update the list by the constructor of Base.
protected CalcBase()
{ AllInstances.Add(this);
}
Note that mutitons like singletons are objects with infinite lifetime.

If your instances are not equivalent you might want to use a
IDictionary<Key,CalcBase> instead of IList<CalcBase> with some
reasonable key to identify your calculators.

So far so good. But how get your instances build? Whatever you do, you
need to initialize your active CalcBase implementations. Be aware to do
this early enough in your program, to build the repository before it is
accessed.

If all of your implementations of CalcBase are default constructable and
singletons, i.e. there are no two instances in your repository that
share the same type, then you might build your repository in the static
constructor of CalcBase by using reflections in the way that you seek
for all classes that derive from CalcBase. For this to be a reasonable
solution, all classes should be defined in the same assembly and the
same namespace.


Marcel
 
J

Jeff Johnson

I have built some custom classes that are built on the same base
class, but the have all a different method called Calc.
I would like to put them in one dictionary and use the calc method.

Is there a way to add them in one dictionary without using a generic
object and boxing/unboxing them.
I'm looking for something more elegant.

Pardon me if this is a n00B assumption, but couldn't you just put the Calc
method in an interface and then make a Dictionary<string, ICalculator>?
 
H

Holger

Pardon me if this is a n00B assumption, but couldn't you just put the Calc
method in an interface and then make a Dictionary<string, ICalculator>?

Marcel/Jeff, thank you for your help.

Jeff, that is what I was looking for. I just didn't know that you
could do that. Excellent!
 

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