Get type in referenced assembly by supplying class name as string?

25,807

Solution 1

Maybe the referenced assembly isn't loaded at the time. Also, I understand from your question that you do not have the full type name, only the class name.
You should try something along this line then:

Type type = Assembly.Load("YourAssemblyName").GetTypes().First(t => t.Name == "ShortTypeName");

Hope I understood you correctly.

Solution 2

For the first question, you could do something like

Type t = AppDomain.CurrentDomain.GetAssemblies()
                                .Where(a => a.FullName == "MyFramework")
                                .SelectMany(a => a.GetTypes())
                                .FirstOrDefault(t => t.Name == "Car");

I am not sure what you mean by the second question.

Share:
25,807
SventoryMang
Author by

SventoryMang

Started out Web Designer, moved into programming. Still fairly new at me but I am learning! So far I have experience in .NET C# and VB and dabbled in a little in php.

Updated on February 08, 2021

Comments

  • SventoryMang
    SventoryMang about 3 years

    These are similar questions: How-to: Load a type from a referenced assembly at runtime using a string in Silverlight, GetType on a class in a referenced assembly fails but neither answer works.

    I've got an MVC project that pulls data from a database that includes the plain types as strings. These types are in a referenced assembly, not in the MVC project.

    So for example let's say my Referenced Assembly Name is MyFramework and the plain type name Car, the full type name could be MyFramework.Cars.Car or MyFramework.Vehicles.Cars.Car or some other variation. All I have are the referenced assembly name and plain class name as strings. How can I get the type regardless of the full type name?

    Finally, could I write a function in the referenced assembly that calls GetType() and use that in the MvC project so I could forego including the assembly name? I want to remove knowing the assembly name so I thought I could write a Util IN the referenced assembly like:

    namespace MyFramework //the referenced assembly
    {
      public static class TypeUtil
      {
        public static Type GetFrameworkType(string typeName)
        {
            return Type.GetType(typeName);
        }
      }
    }
    

    And then in my MVC project I could call it without needing the assembly as a string name. Is that possible or will I always need the assembly name?

  • SventoryMang
    SventoryMang over 11 years
    If I write a Util in that assembly but still only have the class name, how would it look? I'd like to remove the need for the assembly name by writing a TypeUtil class that can have a GetTypeFromFramework(string typeName) function. I thought if I call GetType in that assembly, I don't need to use the assembly at all, but it still returns null.
  • Jony Adamit
    Jony Adamit over 11 years
    Please post a sample of what you're trying to do, so we could better understand your situation.
  • SventoryMang
    SventoryMang over 11 years
    I edited the question. Your answer works when using it in my Util. I am just curious if I can remove the dependency on the assembly name as a string. I was under the impression that if I called GetType() in that assembly, I wouldn't need to supply the assembly name, but simply supplying Type.GetType(class name) kept returning null, even when this code was in the referenced assembly.
  • Jony Adamit
    Jony Adamit over 11 years
    Well, this method would only work if you supply it with the namespace.
  • Jony Adamit
    Jony Adamit over 11 years
    You don't have to have the assembly.. but atleast you need the full type name.
  • SventoryMang
    SventoryMang over 11 years
    Okay, then I guess I'll stick to knowing the assembly instead. Because the full type name definitely won't be supplied.