Dynamically creating a control on. NET with a string is achieved by reflection.
First, use the System.Type.GetType method to get the type instance of the control specified in the string.
Here you need to pay attention to the syntax of this string, according to the MSDN explanation:
hiding by name and signature takes into account all parts of the signature, including custom modifiers, return types, parameter types, tags, and unmanaged calling conventions. This is binary comparison.
For reflection, properties and events are hidden by name and signature. If there are properties in the base class that have both a get accessor and a set accessor, but only a get accessor in the derived class, the derived class property hides the base class property and you will not be able to access the base class's setup program.
Custom attributes are not part of a common type system.
Do not search the array or COM types unless they have been loaded into the available class tables.
TypeName can be a simple type name, a type name that contains a namespace, or a complex name that contains an assembly name specification.
If TypeName contains only the name of Type, this method searches in the assembly of the calling object first and then in the mscorlib.dll assembly. If TypeName is fully qualified with a partial or full assembly name, this method searches in the specified assembly.
AssemblyQualifiedName can return a fully qualified type name, including nested types and assembly names. All compilers that support the common language runtime emit a simple name for a nested class, and when queried, reflection constructs a mangled name according to the following conventions.
For example, the fully qualified name of a class might resemble the following form:
topnamespace.subnamespace.containingclass+nestedclass,myassembly
But using Type.GetType ("System.Windows.Forms.TextBox") directly to get the type is null. This is because the Windows.Forms assembly is a public assembly that is in the assembly cache, and the assembly has a different version, and in order to determine which version to use, we will not only provide the name of the assembly, but also the version and strong name of the assembly. In this way, on the use of the. NET Framework 1.1, write this sentence as Type.GetType ("System.Windows.Forms.CheckBox, System.Windows.Forms, version= 1.0.5000.0, Culture=neutral, publickeytoken=b77a5c561934e089 "). There is no problem with running now. The question is how do we get the version and strong name of the Windows.Forms assembly we use? You can use GetType (CheckBox). AssemblyQualifiedName Such syntax, once this information is available, we can use that information for any other control, since they all come from the same version of the Windows.Forms assembly.
Using the method mentioned above, you can now use the System.Activator.CreateInstance method to create a TextBox control:
public static void CreateControl (string controltype, form form, int positionx, int positiony)
{
try
{
String assemblyqualifiedname = typeof (System.Windows.Forms.Form). AssemblyQualifiedName;
String assemblyinformation = Assemblyqualifiedname.substring (Assemblyqualifiedname.indexof (","));
Type ty = Type.GetType (ControlType + assemblyinformation);
Control NewControl = (Control) System.Activator.CreateInstance (Ty);
Form. SuspendLayout ();
Newcontrol.location = new System.Drawing.Point (Positionx, positiony);
NewControl.Name = Ty. Name + form. Controls.Count.ToString ();
Form. Controls.Add (NewControl);
Form. ResumeLayout ();
}
catch (Exception ex)
{
throw ex;
}
}
Call: CreateControl ("System.Windows.Forms.TextBox", this, 10, 10);