I am attempting to create shapes dynamically, and I have hit a snag regarding the use of Reflection to get this done.
I am tying to do something like this, where an object (objClass) has a string property (chartShape) with the name of the GoShape that is to be used when displaying that object.
GoBasicNode node = new GoBasicNode();
Type s = Type.GetType(“Northwoods.GoWeb.” + objClass.chartShape);
node.Shape = Activator.CreateInstance(s);
The trouble is that Activator.CreateInstance returns an object, which I will need to cast to the appropriate shape. But, how do I do that?
Attempts like this:
node.Shape = (s.GetType())Activator.CreateInstance(s);
are not syntacticly correct but, I can’t figure out how to code it.
Any suggestions from anyone?
Thanks very much.
Can't you just do
node.Shape = (GoShape)Activator.CreateInstance(s);
or
GoShape shape = Activator.CreateInstance(s) as GoShape;
yes, of course I can!
"node.Shape = (GoShape)Activator.CreateInstance(s);" is actually the first thing I tried, but the type pointed to by "s" was not being formed correctly, and was throwing the error. I mistook the error to mean that the cast was not functioning properly.
D'uh.
Thanks for your help.