/// <summary> /// Retrieves the <see cref="Type"/> corresponding to the <paramref name="codeClass"/>. /// </summary> /// <exception cref="InvalidOperationException">An <see cref="ITypeDiscoveryService"/> cannot be /// retrieved for the project the class lives in.</exception> /// <exception cref="InvalidOperationException">The <paramref name="codeClass"/> has not been /// compiled into the project still.</exception> public static Type ToType(CodeClass codeClass) { Guard.ArgumentNotNull(codeClass, "codeClass"); string typeFullName = codeClass.FullName; Type returnType = null; IServiceProvider provider = ToServiceProvider(codeClass.DTE); DynamicTypeService typeService = VsHelper.GetService <DynamicTypeService>(provider, true); IVsHierarchy hier = ToHierarchy(codeClass.ProjectItem.ContainingProject); ITypeResolutionService typeResolution = typeService.GetTypeResolutionService(hier); if (typeResolution == null) { throw new InvalidOperationException(String.Format( CultureInfo.CurrentCulture, Properties.Resources.ServiceMissing, typeof(ITypeResolutionService))); } returnType = typeResolution.GetType(typeFullName); if (returnType == null) { throw new InvalidOperationException(String.Format( CultureInfo.CurrentCulture, Properties.Resources.ClassNotCompiledYet, typeFullName, codeClass.ProjectItem.get_FileNames(1))); } return(returnType); }
/// <summary> /// Converts the project into a visual studio hierarchy object. /// </summary> public static IVsHierarchy ToHierarchy(Project project) { Guard.ArgumentNotNull(project, "project"); IServiceProvider provider = ToServiceProvider(project.DTE); IVsSolution solution = (IVsSolution)VsHelper.GetService <SVsSolution>(provider, true); IVsHierarchy hierarchy; ErrorHandler.ThrowOnFailure(solution.GetProjectOfUniqueName(project.UniqueName, out hierarchy)); return(hierarchy); }
/// <summary> /// Retrieves the selected hierarchy from the provider. /// </summary> public static IVsHierarchy ToSelectedHierarchy(IServiceProvider provider) { Guard.ArgumentNotNull(provider, "provider"); uint itemId; IVsMonitorSelection selectionMonitor = (IVsMonitorSelection)VsHelper.GetService <SVsShellMonitorSelection>(provider, true); IntPtr hierarchyPointer = IntPtr.Zero; IVsMultiItemSelect multiItemSelect = null; IntPtr ppSC = IntPtr.Zero; ErrorHandler.ThrowOnFailure(selectionMonitor.GetCurrentSelection(out hierarchyPointer, out itemId, out multiItemSelect, out ppSC)); return((IVsHierarchy)Marshal.GetObjectForIUnknown(hierarchyPointer)); }