Daniel Cazzulino's Blog : How to get a System.Type from an EnvDTE.CodeTypeRef or EnvDTE.CodeClass

Subscriptions

News

Source code published in this blog is public domain unless otherwise specified.

 

kzu in LinkedIn

  Microsoft MVP Profile

 Contact

Post Categories

How to get a System.Type from an EnvDTE.CodeTypeRef or EnvDTE.CodeClass

I have already hinted at this in a previous post, but this time it's a bit different. Sometimes it's useful to switch to a full System.Type when you're doing code-generation or general CodeModel navigation within VS in an addin or VSSDK package.

The trick is to retrieve the ITypeResolutionService from the DynamicType associated with the current project:

private ITypeResolutionService GetResolutionService()
{
    DynamicTypeService typeService = GetService<DynamicTypeService>();
    Debug.Assert(typeService != null, "No dynamic type service registered.");

    IVsSolution sln = GetService<IVsSolution>();
    IVsHierarchy hier;
    sln.GetProjectOfUniqueName(CurrentProject.Project.UniqueName, out hier);

    Debug.Assert(hier != null, "No active hierarchy is selected.");

    return typeService.GetTypeResolutionService(hier);
}

[GetService<T> is just a shorthand to an IServiceProvider.GetService call. The service provider should be the one associated with the current project]

With the resolver at hand, you can simply do:

CodeTypeRef type = // some type you got
Type
t = resolver.GetType(type.AsFullName, true);

or

CodeClass cc = // some class you got
Type
t = resolver.GetType(cc.FullName, true);

 

One caveat:  this is very reliable if the type you're loading is external (i.e. it's CodeClass.InfoLocation == vsCMInfoLocation.vsCMInfoLocationExternal) but for classes on the current project, you'll get a type that contains the code from the last successful compilation. Alternatively, you can force a compilation before going this route, although I'd advise against it: it's not a very good user experience IMO.

posted on Thursday, October 04, 2007 1:20 PM by kzu

# How to get a System.Type from an EnvDTE.CodeTypeRef or EnvDTE.CodeClass @ Thursday, October 04, 2007 1:35 PM

I have already hinted at this in a previous post , but this time it's a bit different. Sometimes it's

Anonymous

# How do I get a System.Type from a type name? @ Thursday, March 06, 2008 4:49 AM

This another question that appears from time to time in the forums, and it is one extremely complicated

Anonymous