Dynamic Casting

J

Jeff Poste

Hi,

I'm developing software that requires business rules that
constantly need new versions for different quarters,
years, etc. I created a business rule factory that stores
these different business rules in a hashtable indexed by
the year, ie "3Q2003". Now based upon the key that I give
the factory, it will return a reference to the correct
business object for me to use. This works well as long as
all of the business objects in the factory implement the
same interface exactly.

If I want to extend a business rule by adding a method to
the "4Q2003" object, for example, my code breaks, since my
interface Type has no knowledge of the extra method in the
extended object reference, and hence can not call it.

I can, however, using reflection, get the fully qualified
name of the object, as it knows what it really is,
discover the method names on that object reference, and
actually call the extra method, but this is VERY
cumbersome. (I can send the code for this if necessary.)

It would be great if I could dymanically cast the returned
object reference from my factory to the extended class
represented by the fully qualified name that I can
retrieve from the object reference itself, then I could
just use my extended object.

An example of the problem in a code snippet is:

if (rs == "4Q2003")
{
IBusRule br1;
br1 = BRFactory.getBusRule(rs);
//works: useBusRule in IBusRule intfc
br1.useBusRule(); // OK
//doesn't work: multiplyParams not in IBusRule intfc
double dd = br1.multiplyParams(5.0, 7.0); // ERROR
}

In Java the solution to the problem is simple and
powerful. I can check the fully qulified name of my
returned object reference. I can then dynamically create a
reference of that object type at runtime by using the
Class.forName() method. If it is a valid class it will be
created, and I can assign my factory returned object
reference to it. I can now use the object. This allows
me to store ANY object in my factory, check its type when
its reference is returned, and use it.

Is it in the plans for C# to implement some sort of
dynamic casting functionality like this in the future? I
sure found it useful when I was developing in Java. Or, is
there a way to do this in C#, and I'm just not getting it?

Thanks for your help.
 
J

Jon Skeet [C# MVP]

In Java the solution to the problem is simple and
powerful. I can check the fully qulified name of my
returned object reference. I can then dynamically create a
reference of that object type at runtime by using the
Class.forName() method. If it is a valid class it will be
created, and I can assign my factory returned object
reference to it. I can now use the object. This allows
me to store ANY object in my factory, check its type when
its reference is returned, and use it.

There is no dynamic casting in Java - and to be honest, I don't
understand what you just wrote above. If you could write the Java code
you are talking about, I'm happy to convert it into C#. In both Java
and C#, however, the best way is to use interfaces - and when you know
the actual type in question, just do a normal cast. If you don't know
the actual type in question, you're not going to know that it's got a
multiplyParams method anyway.

It sounds like even your Java solution is more complicated than it
really needs to be, but it won't be clear whether or not that's the
case until you post the code which does what you're talking about.
 
F

Frank Oquendo

Jeff said:
In Java the solution to the problem is simple and
powerful. I can check the fully qulified name of my
returned object reference. I can then dynamically create a
reference of that object type at runtime by using the
Class.forName() method.

Class.forName creates an instance of the specified type. It's still up
to you to declare the variable which will reference it.
Activator.CreateInstance will do the same thing.

However, that doesn't solve the problem of not being able to access the
methods of the returned object via a reference to a type that's unaware
of your new interface.

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
 
J

Jon Skeet [C# MVP]

Frank Oquendo said:
Class.forName creates an instance of the specified type.

No it doesn't - it loads/fetches the type itself. It's the equivalent
of Type.GetType.

Class.newInstance() is the Java equivalent of Activator.CreateInstance.
It's still up to you to declare the variable which will reference it.

Indeed. It's not at all clear what the OP is actually doing in Java.
 

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