Fun with generics
-
Here's a good problem. I'm trying to set up a system where text fields in a database define dynamically-generated controls on the form. In other words, when a user makes a selection on the form, the system will pull all the records that match that selection from a database that contains text information that defines all the controls from one table, and text information that defines each of those controls' properties from a related table. Now, I could set up switch statements that would satisfy many of the possible permutations. But that would require hundreds of lines of code, and it wouldn't be feasible to predict all the possibilities. My idea is to use generics (which I'm just learning about) to dynamically define the controls and their properties. In essense, I need to somehow get text from the database into the utilization of the generic. Conceptually, something along these lines: **** public class GenericControl { **Code to construct the control** } class TestGeneric { private void BuildControl() { //In reality, this string would be retrieved from the database. string controltype = "System.Web.UI.WebControls.TextBox"; //Then the part that won't work: GenericControl MyControl = new GenericControl(); } } **** Now, if I understand correctly, generic utilization requires that you use a defined object where I have "controltype" - which is why the above example won't work. Is there any way to take the string "System.Web.UI.WebControls.TextBox" and use it to get a TextBox object type (or the appropriate object type) - without using a switch statement?
-
Here's a good problem. I'm trying to set up a system where text fields in a database define dynamically-generated controls on the form. In other words, when a user makes a selection on the form, the system will pull all the records that match that selection from a database that contains text information that defines all the controls from one table, and text information that defines each of those controls' properties from a related table. Now, I could set up switch statements that would satisfy many of the possible permutations. But that would require hundreds of lines of code, and it wouldn't be feasible to predict all the possibilities. My idea is to use generics (which I'm just learning about) to dynamically define the controls and their properties. In essense, I need to somehow get text from the database into the utilization of the generic. Conceptually, something along these lines: **** public class GenericControl { **Code to construct the control** } class TestGeneric { private void BuildControl() { //In reality, this string would be retrieved from the database. string controltype = "System.Web.UI.WebControls.TextBox"; //Then the part that won't work: GenericControl MyControl = new GenericControl(); } } **** Now, if I understand correctly, generic utilization requires that you use a defined object where I have "controltype" - which is why the above example won't work. Is there any way to take the string "System.Web.UI.WebControls.TextBox" and use it to get a TextBox object type (or the appropriate object type) - without using a switch statement?
-
Here's a good problem. I'm trying to set up a system where text fields in a database define dynamically-generated controls on the form. In other words, when a user makes a selection on the form, the system will pull all the records that match that selection from a database that contains text information that defines all the controls from one table, and text information that defines each of those controls' properties from a related table. Now, I could set up switch statements that would satisfy many of the possible permutations. But that would require hundreds of lines of code, and it wouldn't be feasible to predict all the possibilities. My idea is to use generics (which I'm just learning about) to dynamically define the controls and their properties. In essense, I need to somehow get text from the database into the utilization of the generic. Conceptually, something along these lines: **** public class GenericControl { **Code to construct the control** } class TestGeneric { private void BuildControl() { //In reality, this string would be retrieved from the database. string controltype = "System.Web.UI.WebControls.TextBox"; //Then the part that won't work: GenericControl MyControl = new GenericControl(); } } **** Now, if I understand correctly, generic utilization requires that you use a defined object where I have "controltype" - which is why the above example won't work. Is there any way to take the string "System.Web.UI.WebControls.TextBox" and use it to get a TextBox object type (or the appropriate object type) - without using a switch statement?
In Generics, types must be known compile-time, not run-time, and do not instance parameters (controltype is an instance of the System.String class). The suggestion from Guffa is very good, but you could still avoid having tons of switches in your code. Start from the fact that all your controls inherit from the
System.Windows.Forms.Control
class, which, I would say, contains about 95% of the properties of the built-in controls (Size, Font, Text, Position, Name, Visible, etc.). So, using reflection, you could, as suggested by Guffa, instanciate your controls in a way similar to the one below:List<Control> controls = new List<Control>();
foreach (....)
{
Type myControlType = Type.ReflectionOnlyGetType(controltype, true, false);
ConstructorInfo myControlContructor = myControlType.GetConstructor(Type.EmtpyTypes); // Default
Control myControl = myControlContructor.Invoke(null);controls.Add(myControl);
}
// It is more efficient to declare the variables outside the loop, though.
Then, set all the properties common to all controls, I think this would cover much of your cases. You will need some mechanism (maybe a flag from your database) to indicate properties that are not defined in the
System.Windows.Forms.Control
class. Hope this helps, Michel -------- "I say no to drugs, but they don't listen." - Marilyn Manson -- modified at 7:39 Friday 27th January, 2006