in

ASP.NET Weblogs

uber1024's WebLog

It's not hot wings and beer, but it's still okay

Late binding

Okay, this isn't a big deal but I ran into a situation where it was convenient to be able to create a class without knowing it's type until runtime.  All the classes in this namespace derive from a base class so certain functionality it guaranteed in each one.  This is how we did it:

tType = Type.GetType("XmlRouter." + sCurrentNodeName);

if( tType != null )

{

object o = Activator.CreateInstance(tType);

}

// the function we want to use has XML parameters

XmlNode[] args = new XmlNode[2];

args[0] = oXmlHeaderNode;

args[1] = oNode;

// invoke method

object oOut = tType.InvokeMember("ProcessRequest", BindingFlags.Default | BindingFlags.InvokeMethod, null, o, args);

Comments

 

Stuart Ballard said:

If you have a known common base class it's easier to do something like this:

tType = Type.GetType("XmlRouter." + sCurrentNodeName);
if (tType != null)
{
MyBaseClass obj = (MyBaseClass) Activator.CreateInstance(tType);
}

object oOut = obj.ProcessRequest(oXmlHeaderNode, oNode);
December 29, 2004 1:23 PM
 

Ron Buckton said:

I would recommend using something like a Factory to create your types as well if possible, that way you can reduce the cost of reflection with Type.GetType and Activator.CreateInstance.

e.g.

public class XmlRouterFactory {
private XmlRouterFactory() {}
public static XmlRouterBase CreateRouter(string name) {
switch(name) {
case "TypeA": return new TypeA();
case "TypeB": return new TypeB();
default: throw new InvalidOperationException();
}
}
}

public class TypeA : XmlRouterBase { ... }
public class TypeB : XmlRouterBase { ... }

...
XmlRouterBase router = XmlRouterFactory.CreateRouter(sCurrentNodeName);
object oOut = router.ProcessRequest(oXmlHeaderNode, oNode);
December 29, 2004 1:30 PM
 

Stuart Ballard said:

Except that won't compile, because obj is out of scope by the time we come to use it. If I were writing this I'd either not bother with the tType != null test at all and let the fatal exception propagate quickly, or do error handling properly and check everything including testing whether "obj is MyBaseClass" before doing the cast (or making sure that it has a ProcessRequest method with the right arguments, in your version).
December 29, 2004 1:33 PM
 

Stuart Ballard said:

Sorry, I was referring to my own code when I said it wouldn't compile, not yours Ron! :)
December 29, 2004 1:33 PM
 

uber1024 said:

> Except that won't compile, because obj is out of scope by the time we come to use it.

Right, but the point is there. I like the idea of strongly typing it to the base class.

We didn't use a factory because we're going to expand this in the future (starting today, that's my next project) and this will minimize the number of places that the code needs to change. A factory was my first idea, but this was written before I got here.
December 29, 2004 2:11 PM
 

Ron Buckton said:

I still recommend the Factory approach because even if you are forced to use Type.GetType you can increase performance by caching the types.
December 29, 2004 3:51 PM
 

TrackBack said:

^_^,Pretty Good!
April 10, 2005 7:17 AM

Leave a Comment

(required)  
(optional)
(required)  
Add