get Type from string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ruben

    #1

    get Type from string

    Hi!

    I am looking for an easy way to get a Type from a arbitrary string.
    What I found looks like:

    ----
    string TypeString = "System.String" ; // only as example...
    Type TypeIAmLookingF or = null;

    Assembly[] appAssemblies =
    System.AppDomai n.CurrentDomain .GetAssemblies( );

    foreach (Assembly assembly in appAssemblies)
    {
    foreach (Type type in assembly.GetTyp es())
    {
    if (t.ToString().E quals(TypeStrin g))
    TypeIAmLookingF or = type;
    }
    }
    ---

    This is kind of overloaded; is there are more easy way to do this?

    Thank you!
    Ruben

  • Carl Daniel [VC++ MVP]

    #2
    Re: get Type from string

    "Ruben" <MrPugh@gmx.dew rote in message
    news:1163437588 .904083.302930@ m73g2000cwd.goo glegroups.com.. .
    Hi!
    >
    I am looking for an easy way to get a Type from a arbitrary string.
    What I found looks like:
    >
    ----
    string TypeString = "System.String" ; // only as example...
    Type TypeIAmLookingF or = null;
    >
    Assembly[] appAssemblies =
    System.AppDomai n.CurrentDomain .GetAssemblies( );
    >
    foreach (Assembly assembly in appAssemblies)
    {
    foreach (Type type in assembly.GetTyp es())
    {
    if (t.ToString().E quals(TypeStrin g))
    TypeIAmLookingF or = type;
    }
    }
    ---
    >
    This is kind of overloaded; is there are more easy way to do this?
    See System.Type.Get Type(string typeName)

    -cd


    Comment

    • Ruben

      #3
      Re: get Type from string

      Carl Daniel [VC++ MVP] schrieb:
      See System.Type.Get Type(string typeName)
      Thank you!

      Sometimes it is hard to find the right place in the documentation.. . :-)

      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: get Type from string

        Ruben <MrPugh@gmx.dew rote:
        Carl Daniel [VC++ MVP] schrieb:
        See System.Type.Get Type(string typeName)
        >
        Thank you!
        >
        Sometimes it is hard to find the right place in the documentation.. . :-)
        Note, however, that Type.GetType with just the "plain" type name (i.e.
        without full assembly information) will only search in mscorlib and the
        currently executing assembly. If you need to look in other assemblies,
        either call GetType on the appropriate Assembly reference, or specify
        the assembly details in the argument to Type.GetType.

        --
        Jon Skeet - <skeet@pobox.co m>
        http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
        If replying to the group, please do not mail me too

        Comment

        Working...