private Assembly ResolveAssembly(ProjectId projectId, string assemblyName) { this.AssertIsForeground(); if (_workspace == null) { return(null); } IVsHierarchy hierarchy; string targetMoniker; if (!_workspace.TryGetHierarchy(projectId, out hierarchy) || !hierarchy.TryGetProperty((__VSHPROPID)__VSHPROPID4.VSHPROPID_TargetFrameworkMoniker, out targetMoniker) || targetMoniker == null) { return(null); } try { // Below we use the DesignTimeAssemblyResolver functionality of VS to // determine if we can resolve the specified assembly name in the context // of this project. However, this service does not do the right thing // in UWP apps. Specifically, it *will* resolve the assembly to a // reference assembly, even though that's never what we want. In order // to deal with that, we put in this little check where we do not allow // reference assembly resolution if the projects TargetFrameworkMoniker // is ".NETCore, Version=5.0" or greater. var frameworkName = new FrameworkName(targetMoniker); if (StringComparer.OrdinalIgnoreCase.Equals(frameworkName.Identifier, ".NETCore") && frameworkName.Version >= new Version(major: 5, minor: 0)) { return(null); } } catch (ArgumentException) { // Something wrong with our TFM. We don't have enough information to // properly resolve this assembly name. return(null); } try { var frameworkProvider = new VsTargetFrameworkProvider( _workspace.GetVsService <SVsFrameworkMultiTargeting, IVsFrameworkMultiTargeting>(), targetMoniker, _workspace.GetVsService <SVsSmartOpenScope, IVsSmartOpenScope>()); return(frameworkProvider.GetReflectionAssembly(new AssemblyName(assemblyName))); } catch (InvalidOperationException) { // VsTargetFrameworkProvider throws InvalidOperationException in the // some cases (like when targetting packs are missing). In that case // we can't resolve this path. return(null); } }
public string ResolveAssemblyPath(ProjectId projectId, string assemblyName) { this.AssertIsForeground(); if (_workspace != null) { IVsHierarchy hierarchy; string targetMoniker; if (_workspace.TryGetHierarchy(projectId, out hierarchy) && hierarchy.TryGetProperty((__VSHPROPID)__VSHPROPID4.VSHPROPID_TargetFrameworkMoniker, out targetMoniker) && targetMoniker != null) { try { var frameworkProvider = new VsTargetFrameworkProvider( _workspace.GetVsService <SVsFrameworkMultiTargeting, IVsFrameworkMultiTargeting>(), targetMoniker, _workspace.GetVsService <SVsSmartOpenScope, IVsSmartOpenScope>()); var assembly = frameworkProvider.GetReflectionAssembly(new AssemblyName(assemblyName)); // Codebase specifies where the assembly is on disk. However, it's in // full URI format (i.e. file://c:/...). This will allow us to get the // actual local in the normal path format. Uri uri; if (Uri.TryCreate(assembly.CodeBase, UriKind.RelativeOrAbsolute, out uri)) { return(uri.LocalPath); } } catch (InvalidOperationException) { // VsTargetFrameworkProvider throws InvalidOperationException in the // some cases (like when targetting packs are missing). In that case // we can't resolve this path. } } } return(null); }