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
Printable View
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
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.
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?Quote:
Originally Posted by loftty
Do you mean to say that you are saving the properties in the XML file?
Yes I am saving the properties to xml plus the control type.
Thanks
Loftty
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.
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