0

I'm trying to write my first ReSharper extension and I'm stuck at the following problem:

How do you get a resolved Type from an IType descriptor of it?

For background, I'm trying to write an analyzer that uses a tool to test the compatibility of two type arguments.

So far, I've got this

[ElementProblemAnalyzer(new[] { typeof(IInvocationExpression) })]
public class MyAnalyzer : IElementProblemAnalyzer`
{
   public void Run(ITreeNode element, ElementProblemAnalyzerData analyzerData, IHighlightingConsumer consumer)
   {
        ...

        var typeArgs = meaningfulChildren.FirstOrDefault(o => o is ITypeArgumentList) as ITypeArgumentList;

        IType psiType = typeArgs.TypeArguments[0];
        Type actualType = psiType.ResolvedType; // No such property
   }
}
Andre Luus
  • 3,692
  • 3
  • 33
  • 46

1 Answers1

0

You can't do what you're asking for - ReSharper's type system is a view of the source of a project, not an assembly loaded into an AppDomain. In other words, you can get the IType for a class defined in Foo.cs, even if Foo.cs hasn't been loaded. It still needs to be able to return type information if the project can't compile, because the code in Bar.cs is just plain invalid.

So you can't convert an IType into a System.Type without compiling the project and loading the assembly into an AppDomain. But ReSharper has a lot of tools to work with types in its own view of the type system. What exactly are you trying to do?

citizenmatt
  • 18,085
  • 5
  • 55
  • 60
  • I was afraid this might be the case. I am trying to build a ReSharper extension for AutoMapper with the aim that it provides a highlight if it determines that the types you are mapping aren't fully 'mappable'. In order to do this, I need to use calls to `Mapper.CreateMap()` with actual types. – Andre Luus Nov 24 '15 at 13:41
  • If you can replicate the logic easily enough, you'll be able to write it using ReSharper - there are various utility methods that will check whether two types (based on source code) are compatible. – citizenmatt Nov 24 '15 at 17:27
  • Sadly compatible isn't trivial here - this isn't just assignable in the regular sense, it is "will all properties on the destination type be 'hydratable' from the source type or any of its child properties if you flatten them". I checked and unfortunately they also didn't add an abstraction layer on top of the .Net type system, so I couldn't easily fake it. It may end up being more trouble than it is worth. I appreciate your advice though. – Andre Luus Nov 24 '15 at 17:48