. NET, on the basis of string to create dynamic controls

Author:Anonymous    Updated:2008-10-26 21:07:49
First of all, System.Type.GetType use of the method was specified in the string of examples of the type of control.

     It should be pay attention to the string of grammar, according to the msdn explanation:

           1. Hidden by name and signature will take into account all the signatures, including custom qualifier, return type, the type of parameters, and non trusteeship tag calling convention. This is a binary comparison.

            2. For reflection, properties and events by name and signature to hide. If the base class in the same time zone and get access device set to visit the property, but the derived class only get to visit, the hidden attribute is derived class base class attributes, and you will not be able to visit the base class set of procedures.

            3. Custom features are not common types of system.

     COM or not the array type of search, unless they have been loaded into the available categories in the table.

     typeName can be a simple type, the type of space that contains names, or include the name of a standardized set of procedures for the name of the complex.

     If typeName only Type the name, this method calls in the first object of the search focused on the procedure, and then mscorlib.dll focus on the search process. If typeName part of a complete or fully qualified name of the assembly, this approach focused on the procedures specified in the search.

     AssemblyQualifiedName can return to the fully qualified name of the type (including nested set of procedures and the type of name). All support the common language runtime compiler will issue a simple name of the nested class, and when the inquiry, reflection, in accordance with the agreed structure following a mangled name.

     For example, the fully qualified class name may be similar to the form are as follows:

     TopNamespace.SubNameSpace.ContainingClass + NestedClass, MyAssembly

     However, the use of direct Type.GetType ( "System.Windows.Forms.TextBox") is a Type was Null. This is because, Windows.Forms assembly is the public assembly, is located in the assembly cache, and this assembly There are different versions, in order to determine the version used, we must not only provide the name of the assembly, but also to provide assembly and the strong version of the name. According to this thinking, the use of. net Framework 1.1, this sentence written in Type.GetType ( "System.Windows.Forms.CheckBox, System.Windows.Forms, Version = 1.0.5000.0, Culture = neutral, PublicKeyToken = b77a5c561934e089"). Now there will be no problem to run. The question is how do we get Windows.Forms used by the assembly's version and strong name? Can GetType (CheckBox). AssemblyQualifiedName such a grammar, once this information, we will be able to use this information to any other control, because they all come from the same version of Windows.Forms assembly.

     Use of the above mentioned methods can now use System.Activator.CreateInstance way 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);
Previous:. NET members of the special types
Next:JS objects to the object c #
User Reviews
Site Search
Recommended article
AD