Dynamically casing an object

I'm trying to add dictionary of controls that are objects

        public Dictionary<string, object> CodeArray
            set { _CodeArray = value; }
            get { return _CodeArray; }


The problem is that CodeArray[tagChild] returns an object instead for example an Image or LinkButton, etc.
 I cant statically assign it b/c i have a map file and its dynamically loaded.  So my question is how would i fix
so that i can properly cast it to they type that the object is.  I can find out what type it is if i look at the returned object but not sure how to cast it.  I'm sure this has something to do with reflection but not sure what to do.

Who is Participating?
YurichConnect With a Mentor Commented:
You can investigate using of Activator as well:

That will involve reflection:

The basic syntax is:
Button button = new Button();
Object o = Activator.CreateInstance( button.GetType() );
Button b = ( Button )o;

But I think it can get a lot more complex :|

Good luck,
If you don'thave too many controls to cast to (and even if you do), you can probably use a switch operator and cast depending on the type that you said you can find - then just hard-code your casting.

If you're doing Contols.Add( x ) then all the 'x' must derive from Control.

So just cast to Control.....

((LinkButton)CodeArray[tagParent]).Controls.Add( (Control) CodeArray[tagChild]);

(Or, why can't you make the dictionary a Dictionary<string,Control> instead!
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

A nice operator, totally underused in C# is the "is" and "as" operator

System.Text.StringBuilder sb = new System.Text.StringBuilder();
object o = new Object();
Button b = new Button();

Control c;
c = (Control) sb; // InvalidCastException
c = sb as Control; // No exception, result: c = null
c = c as Control; // c now contains the button (casted to a Control)
bool valid = o is Button; // gives false
valid = b is Control; // gives true

Kind regards,
borg48Author Commented:
The problem i have is that the controls will be different at any given time.  
may be

basically im creating a composite control and created a mapping system so i dont have to hard code building of the control, otherwise it would be easy.

So CodeArray contains a varied web.ui.control not just one particulare one.  My initial solutions is just to use a switch statement which works but not happy with b/c it gets big.  So thats is why i'm trying to figure out how to use reflection and cast out the type.  Example i can call gettype to find out out the control but how do i cast that?
... but if CodeArray contains various things ALL of which derived from ui.control, then just cast everything to ui.control, which should have the .controls property that you can .add() a ui.control to...
illusioConnect With a Mentor Commented:
You don't.
Using reflection you can call methods using strings in stead of using the Object.Method notation. But you can never cast dynamically to an unknown type. The switch statement is the only solution that I know of, if you want to use the typed objects. If you want to create objects of a certain type dynamically from a namestring you can use Activator.CreateInstance as Yurich has shown in the first post. The result you receive is an Object but the actual type is the type requested.
What you have to ask is what is the minimal type I need for the methods I'm using. If this type is a common ancestor of the types you are using, cast everything to that base class (e.g. Control as mentioned by andrewjb). If the minimal type needed is not a common ancestor then you already have a different handling in case of certain types. You examine the actual type and then go for the different handling types.

If (CodeArray[tagParent] is Control)
  if (CodeArray[tagChild] is Image && CodeArray[tagParent] is Button)
    (CodeArray[tagParent] as Button).ImageList.Images.Add(CodeArray[tagChild] as Image];
    (CodeArray[tagParent] as Control).Controls.Add(CodeArray[tagChild] as Control);
  // sth else 'cause .Controls.Add is only valid for a "Control"

Kind regards,
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.