Type.GetType

What do I do about when Type.GetType fails for types like System.Xml.Serialization.XmlElement   I have created an addin that I want to be able to see what are the attributes for a given class but it is failing on this one all the time.  If I need to load the assembly first how do I look at references for a ProjectItem so I can load the assembly if needed before calling GetType


Answer this question

Type.GetType

  • Aigarslv

    I did exactly what you specified above I just didn't put it in my example ;)



  • Ted Eiles

    Just so people know what I had to do what use the VSProject2 in order to get to the references of a project to get the full path of the assembly then load it and then call GetType on the assembly.

    Example: VS 2005 ;)

    VSLangProj80.VSProject2 vsProject = project.Object as VSLangProj80.VSProject2;

    foreach(Reference2 reference in vsProject.References)
    {

       Assembly asm = Assembly.LoadFile(reference.Path);

       type = asm.GetType(fullName);

       if (type != null)
          return type;

    }



  • Scott Hodgin

    You should change this to:

    Type type = Type.GetType(fullName);
    if (type == null) {
        VSLangProj80.VSProject2 vsProject = project.Object as VSLangProj80.VSProject2;
        foreach(Reference2 reference in vsProject.References)
        {
           Assembly asm = Assembly.LoadFile(reference.Path);
           type = asm.GetType(fullName);
           if (type != null)
              return type;
        }
    }

    And you could make this search smarter if you did some heuristic matching of the type's full name and the known assembly names - since there's usually some name matching at the namespace level.

  • Michel Mathijssen

    Yes, if GetType returns null you need to then load the assembly.  If you are implementing an MSBuild task you can access references from build engine API's.  If you are outside of MSBUild runtime then you need to process the project file yourself I guess.
  • Type.GetType