none
How to use Type.GetType(string)? Doesnt seem to work. RRS feed

  • Question

  • I'm trying to write a function to genericly return a new instances of a Windows form control based on its typename. It would seem that I should be able to do something like.

    Activator.CreateInstance(Type.GetType("System.Windows.Forms.Label"))

    to accomplish this. I can make Type.GetType work for simple types such as "System.Int32" or "System.String" but it does not work when I ask for a label or textbox. Obviously I'm not getting the typename right, but it seems right to me and I really dont know where to look next. Please help!

    Bill
    Thursday, September 22, 2005 2:59 PM

Answers

  • Try:-
       Type t = Type.GetType("System.Windows.Forms.Label, System.Windows.Forms");

       object o = AppDomain.CurrentDomain.CreateInstanceAndUnwrap(t.Assembly.FullName, t.FullName);

    I wish the documentation for this was clearer. Documentation always uses the most trival, meaningless examples. Why would you want to get an int like this?
    Thursday, September 22, 2005 8:03 PM

All replies

  • I would assume this is hapenning cause the object is in an other .dll

    Console.WriteLine(System.Reflection.Assembly.LoadFrom("C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\System.Windows.Forms.dll").GetType("System.Windows.Forms.Label"))

    returns the right thing, but a simple GetType returns nothing for me too

    Thursday, September 22, 2005 6:26 PM
  • Try:-
       Type t = Type.GetType("System.Windows.Forms.Label, System.Windows.Forms");

       object o = AppDomain.CurrentDomain.CreateInstanceAndUnwrap(t.Assembly.FullName, t.FullName);

    I wish the documentation for this was clearer. Documentation always uses the most trival, meaningless examples. Why would you want to get an int like this?
    Thursday, September 22, 2005 8:03 PM
  • I want to go through the controls collection in a form and take action based on the type of each control.  The example given

    Type t = Type.GetType("System.Windows.Forms.Label, System.Windows.Forms");

    t is null after the execution.

    I have also tried

    Type t = Type.GetType("System.Windows.Forms.Label");

    with same result.  The following works but is CLEARLY wasteful

    Type GroupBoxType = new System.Windows.Forms.GroupBox().GetType();

    It would really be nice if the documentation explained something simple like how to use Type.GetType(string)  This should be so simple why is it so hard.  What were they thinking?

    BTW I am using VS2005 and .Net 2

    Tuesday, May 2, 2006 3:43 AM
  • "System.Windows.Forms" is different then "tyepof(Label).Assembly.FullName" !
    Tuesday, April 3, 2007 7:41 AM
  • If the typeof() operator or Type.GetType() method won't work, try using this function instead:

    public

     

    static Type GetTypeEx(string

    fullTypeName)

    {

     

    Type type = Type

    .GetType(fullTypeName);

     

    if (type != null

    )

     

    return

    type;

    System.Reflection.

    Assembly assembly = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(a => a.GetType(fullTypeName) != null

    );

     

    if(assembly != null

    )

     

    return

    assembly.GetType(fullTypeName);

     

    return null

    ;

    }

    It worked for me when Type.GetType() failed.


    dm
    Friday, September 2, 2011 2:40 PM