CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jul 2007
    Posts
    9

    Question Reflection: Type.GetType(string) but different Assembly

    Hi!

    I've got a string representing the full name of a class ("Blah.Muh.Thing" for example). For getting it's type I normally would now use Type.GetType(string). But the class isn't in the calling Assembly. It's in a DLL used by my application. So I have to use Assembly.GetType(string) first. BUT that only works when I know the Assembly. Now my question:

    What do I have to do if I want to load the type from a string if I don't know from which Assembly it comes from?

    [C#/.NET2.0/VS 2005]

  2. #2
    Join Date
    Dec 2005
    Location
    Waterloo ON
    Posts
    545

    Re: Reflection: Type.GetType(string) but different Assembly

    I think you need to know what assembly(namespace) where the type definition is.
    The difficulty is that you have no idea how difficult it is.

    .Net 3.5/VS 2008

  3. #3
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: Reflection: Type.GetType(string) but different Assembly

    You could do something like this:
    Code:
    Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
    
    List<Type> types = new List<Type>();
    
    foreach (Assembly assembly in assemblies)
    {
        Type type = assembly.GetType("LibA.MyType");
        if (type != null)
            types.Add(type);
    }
    
    foreach (Type type in types)
    {
        Console.WriteLine("Fonund type {0} in assembly {1}.", type.FullName, type.Assembly.FullName);
    }
    - petter

  4. #4
    Join Date
    Jul 2007
    Posts
    9

    Thumbs up Re: Reflection: Type.GetType(string) but different Assembly

    Ah, thank you - works great! I was not able to see the wood for the trees.

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