CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    May 2002
    Location
    Hyderabad
    Posts
    76

    Type.GetType doesn't work

    Hi all,

    I want to display property of the controls in the listbox.
    But the following code which I got from an example in MSDN doesn't work properly.
    On line 1 if I use "System.String" it is working but for
    "System.Windows.Forms.TextBox" it doesn't return any type. So MyType is having an undefined value. On line 2 it gives error like "Object reference not set to Object" because MyType has undefined value.

    1 > Type MyType = Type.GetType( "System.Windows.Forms.TextBox") ;
    2 > MemberInfo[] Mymemberinfoarray = MyType.GetMembers();

    why this??

    Thanks,
    Ashish Sheth

  2. #2
    Join Date
    Jul 2003
    Posts
    17
    You know you can use GetType() on any object.

    Like this:

    TextBox tb = ...
    Type type = tb.GetType();
    MemberInfo[] memberArray = type.GetMembers();

  3. #3
    Join Date
    May 2002
    Location
    Hyderabad
    Posts
    76
    No,
    My user will give the type name and based on the type name I have to show the Members of that type. So until runtime I will not know the type. That's why this solution will not work.
    Ashish Sheth

  4. #4
    Join Date
    Jul 2003
    Posts
    17
    Ok.. here is a workaround I think will work. The type you receive must be accessible in the application domain.

    Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();

    foreach (Assembly a in assemblies) {
    object o = a.CreateInstance("System.Windows.Forms.TextBox");

    if (o != null) {
    Type type = o.GetType();
    MemberInfo[] mIArray = type.GetMembers();
    }
    }

  5. #5
    Join Date
    Sep 1999
    Location
    Madurai , TamilNadu , INDIA
    Posts
    1,024
    Type.GetType() searches for a particular type in mscorlib assembly(CLR Library) and the current assembly only. Even if the System.Windows.Forms assembly is loaded in the memory, it will not look into it.

    Al'Kimiya has posted one workaround to solve this problem. Another work around is, if you know the assembly name (not the namespace) then you can use the following code.

    Code:
    Type mytype = Assembly.LoadWithPartialName("System.Windows.Forms").GetType("System.Windows.Forms.TextBox");
    Last edited by poochi; July 16th, 2003 at 04:27 PM.

  6. #6
    Join Date
    May 2002
    Location
    Hyderabad
    Posts
    76
    thanks,
    but how can I create the TextBox control and place it on the form with the Type object I am having?

    Here I will not know the type of the control to be created until runtime.

    regards,
    Ashish Sheth

  7. #7
    Join Date
    Jul 2003
    Posts
    17
    Originally posted by shethashish_a
    thanks,
    but how can I create the TextBox control and place it on the form with the Type object I am having?

    Here I will not know the type of the control to be created until runtime.

    regards,
    You have the object and since it is a TextBox (which you can check with GetType()) you can add it to form by setting it's parent to the Form. That's why you enumerate methods and properties with reflection. You could also convert your object to TextBox:


    Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();

    foreach (Assembly a in assemblies) {
    object o = a.CreateInstance("System.Windows.Forms.TextBox");

    if (o != null) {

    if (o is TextBox) {
    TextBox tb = (TextBox) o;
    tb.Parent = Form1;
    }

    }

    or rather as a Control (more general)

    if (o is Control) {
    Control o = (Control) o;
    o.Parent = Form1;
    }

  8. #8
    Join Date
    May 2002
    Location
    Hyderabad
    Posts
    76
    that's fine
    but what if you don't even know that u r creating a textbox control or any other control.
    here is the solution which I found and it is working. Here I am sure that i will be creating any of the windows control that is derived from the System.Windows.Forms.Control class. So here is my solution.

    Code:
    private void CreateControl(string controlType)
    		{
    			System.Windows.Forms.Control ctrlDyn ;
    
    			Type t = myAssem.GetType(controlType) ;
    			System.Reflection.ConstructorInfo[] ctorInfo = t.GetConstructors() ;
    			pnlDynControl.Controls.Clear() ;
    			if(ctorInfo.Length >=0)
    			{
    				ctrlDyn = (System.Windows.Forms.Control)ctorInfo[0].Invoke(null) ;
    
    				ctrlDyn.Location = new Point(20,80) ;
    				ctrlDyn.Visible = true ;
    				ctrlDyn.Text = "Hi all" ;
    				ctrlDyn.Width = 100  ;
    				ctrlDyn.Height = 50 ;
    				pnlDynControl.Controls.Add(ctrlDyn) ;
    			}
    			
    
    		}
    here myAssem is and Assembly object which has System.Windows.Forms.Dll already loaded in it.

    Now I have new problem:
    How I will set the property of the control created this way?
    If it is a simple string type property I can set it using PropertyInfo.SetValue property, but what if it is a user defined class or collection??

    regards,
    Ashish Sheth

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured