Need help with casting

  • Thread starter Thread starter Keith Selbee
  • Start date Start date
K

Keith Selbee

I have a file that provides details on which user control to populate a web
page with at runtime. I'm able to create and load the control successfully,
but now I need to cast it from Control to its specific type so I can access
it's specific members. I have the type as a string ("MyUserControl" for
example). So, having the name, how do I cast it from Control to this type?
Thanks...


Keith
 
Keith said:
I have a file that provides details on which user control to populate a web
page with at runtime. I'm able to create and load the control successfully,
but now I need to cast it from Control to its specific type so I can access
it's specific members. I have the type as a string ("MyUserControl" for
example). So, having the name, how do I cast it from Control to this type?
Thanks...

If you know up front what types the control could be an instance of, you
could use something along the lines of:

if (control is MyUserControl) {
MyUserControl myUserControl = (MyUserControl)control;
// Do MyUserControl specific stuff
}

if (control is AnotherControl) {
AnotherControl anotherControl = (AnotherControl)control;
// Do AnotherControl specific stuff
}
 
Check out the Assembly.GetType method. If you are already in the
corresponding assembly, use

Assembly.GetExecutingAssembly method to get instance of Assembly and then
call GetType method by passing type name. You may need full type name (with
namespace and all).
 
Keith Selbee said:
I have a file that provides details on which user control to populate a web
page with at runtime. I'm able to create and load the control successfully,
but now I need to cast it from Control to its specific type so I can access
it's specific members. I have the type as a string ("MyUserControl" for
example). So, having the name, how do I cast it from Control to this type?

If you only have the type as a string, how do you know at compile time
what its specific members are? If you don't know at compile time what
its specific members are, what good do you believe casting would give
you? If you know its specific members at compile time, you'd be best
off encapsulating them in an interface which your control implements,
and then casting to that interface.
 
I'm not 100% sure what you're trying to do, but I think you can cast your
control to whatever the type you want to cast to as long as you inherit the
control from the type you want to cast to. The object hierarchy would be
like...

Control
|---- Your abstract class
|-------------Your control
 
My hope is that I can have an xml file specifying the type of the user
control and a list of properties specific to that type and then populate
them at run time. I just asked for the first part though.

Keith
 
Keith Selbee said:
My hope is that I can have an xml file specifying the type of the user
control and a list of properties specific to that type and then populate
them at run time. I just asked for the first part though.

But if you've got the list of properties as strings as well, rather
than hard-coded, you'll be using reflection to set them - so you don't
need to cast anyway.
 

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

Back
Top