|
-
Mar 13th, 2006, 08:33 AM
#1
Thread Starter
Fanatic Member
Cast a string to the control type
Hi,
I dont know if this is possible or not but if it is could someone show me how to do it please?
I have a string which is "System.Windows.Forms.Button".
How can I turn that string into a button?
Thanks
Loftty
-
Mar 13th, 2006, 08:46 AM
#2
Re: Cast a string to the control type
You could use Activator.CreateInstance, but you will get an ObjectHandle reference returned, so you'd still have to unwrap and cast it to access members of the Button class. There seem to have been a lot of questions lately on using strings to specify types. More often than not if you find yourself in that situation it's through bad design rather than necessity. Where has this string come from in the first place? It may be that you don't need to do it that way.
-
Mar 13th, 2006, 09:06 AM
#3
Thread Starter
Fanatic Member
Re: Cast a string to the control type
Hi,
The string came from a xml file, as I am saving all the controls on a form to xml but when I read the file back in I dont want a switch statement for each diffrent control type.
Thanks
Loftty
-
Mar 13th, 2006, 09:11 AM
#4
Re: Cast a string to the control type
 Originally Posted by loftty
Hi,
The string came from a xml file, as I am saving all the controls on a form to xml but when I read the file back in I dont want a switch statement for each diffrent control type.
Thanks
Loftty
Why would save them in the XML File?
Do you mean to say that you are saving the properties in the XML file?
Use [code] source code here[/code] tags when you post source code.
My Articles
-
Mar 13th, 2006, 09:16 AM
#5
Thread Starter
Fanatic Member
Re: Cast a string to the control type
Yes I am saving the properties to xml plus the control type.
Thanks
Loftty
-
Mar 13th, 2006, 04:53 PM
#6
Re: Cast a string to the control type
You cannot just turn a String into a Type. Like I said, even if you use CreateInstance you will still only get an ObjectHandle from it. You can then Unwrap that to get an Object. You could confidently cast that as a Control, but you'd still need to use reflection to set any properties specific to the actual type. It sounds to me like you should investigate serialization. That's the in-built mechanism for saving objects to XML.
-
Mar 13th, 2006, 11:44 PM
#7
Fanatic Member
Re: Cast a string to the control type
I believe there is a XUL parser for Mono (I could be wrong here as I've never done anything with Mono). It's somewhat creating a component base from the xml file from somewhere. And oh, I don't know if there are any third party stuff for .Net on this one.
http://xul.sourceforge.net/post/2003...l_for_uis.html
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|