Skip to content

Commit

Permalink
Reduces reflection code in ExtensionManagerShim by referencing IInsta…
Browse files Browse the repository at this point in the history
  • Loading branch information
Fernando Aguilar authored May 26, 2022
1 parent fc79d1b commit dbb62f4
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@
using System;
using System.Linq;
using System.Reflection;
using Microsoft.VisualStudio.ExtensionManager;
using Microsoft.VisualStudio.Shell;
using NuGet.VisualStudio.Implementation.Resources;

namespace NuGet.VisualStudio
{
internal class ExtensionManagerShim
{
private static Type _iInstalledExtensionType;
private static Type _iVsExtensionManagerType;
private static PropertyInfo _installPathProperty;
private static Type _sVsExtensionManagerType;
private static MethodInfo _tryGetInstalledExtensionMethod;
private static bool _typesInitialized;
Expand All @@ -23,7 +22,7 @@ internal class ExtensionManagerShim
public ExtensionManagerShim(object extensionManager, Action<string> errorHandler)
{
InitializeTypes(errorHandler);
_extensionManager = extensionManager ?? Package.GetGlobalService(_sVsExtensionManagerType);
_extensionManager = extensionManager ?? AsyncPackage.GetGlobalService(_sVsExtensionManagerType);
}

private static void InitializeTypes(Action<string> errorHandler)
Expand All @@ -36,20 +35,14 @@ private static void InitializeTypes(Action<string> errorHandler)
try
{
Assembly extensionManagerAssembly = AppDomain.CurrentDomain.GetAssemblies()
.First(a => a.FullName.StartsWith("Microsoft.VisualStudio.ExtensionManager,"));
.First(a => a.FullName.StartsWith("Microsoft.VisualStudio.ExtensionManager,", StringComparison.InvariantCultureIgnoreCase));
_sVsExtensionManagerType =
extensionManagerAssembly.GetType("Microsoft.VisualStudio.ExtensionManager.SVsExtensionManager");
_iVsExtensionManagerType =
extensionManagerAssembly.GetType("Microsoft.VisualStudio.ExtensionManager.IVsExtensionManager");
_iInstalledExtensionType =
extensionManagerAssembly.GetType("Microsoft.VisualStudio.ExtensionManager.IInstalledExtension");
_tryGetInstalledExtensionMethod = _iVsExtensionManagerType.GetMethod("TryGetInstalledExtension",
new[] { typeof(string), _iInstalledExtensionType.MakeByRefType() });
_installPathProperty = _iInstalledExtensionType.GetProperty("InstallPath", typeof(string));
if (_installPathProperty == null
|| _tryGetInstalledExtensionMethod == null
||
_sVsExtensionManagerType == null)
new[] { typeof(string), typeof(IInstalledExtension).MakeByRefType() });
if (_tryGetInstalledExtensionMethod == null || _sVsExtensionManagerType == null)
{
throw new Exception();
}
Expand All @@ -73,8 +66,8 @@ public bool TryGetExtensionInstallPath(string extensionId, out string installPat
{
return false;
}
object extension = parameters[1];
installPath = _installPathProperty.GetValue(extension, index: null) as string;
var extension = parameters[1] as IInstalledExtension;
installPath = extension?.InstallPath;
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
using System.Diagnostics.CodeAnalysis;

[assembly: SuppressMessage("Build", "CA1031:Modify 'InitializeTypes' to catch a more specific allowed exception type, or rethrow the exception.", Justification = "<Pending>", Scope = "member", Target = "~M:NuGet.VisualStudio.ExtensionManagerShim.InitializeTypes(System.Action{System.String})")]
[assembly: SuppressMessage("Build", "CA1307:The behavior of 'string.StartsWith(string)' could vary based on the current user's locale settings. Replace this call in 'NuGet.VisualStudio.ExtensionManagerShim.InitializeTypes(System.Action<string>)' with a call to 'string.StartsWith(string, System.StringComparison)'.", Justification = "<Pending>", Scope = "member", Target = "~M:NuGet.VisualStudio.ExtensionManagerShim.InitializeTypes(System.Action{System.String})")]
[assembly: SuppressMessage("Build", "CA1031:Modify 'CreateMetadata' to catch a more specific allowed exception type, or rethrow the exception.", Justification = "<Pending>", Scope = "member", Target = "~M:NuGet.VisualStudio.PackageManagementHelpers.CreateMetadata(System.String,NuGet.Packaging.Core.PackageIdentity)~NuGet.VisualStudio.IVsPackageMetadata")]
[assembly: SuppressMessage("Build", "CA1822:Member CopyNativeBinaries does not access instance data and can be marked as static (Shared in VisualBasic)", Justification = "<Pending>", Scope = "member", Target = "~M:NuGet.VisualStudio.PreinstalledPackageInstaller.CopyNativeBinaries(NuGet.PackageManagement.VisualStudio.VsMSBuildProjectSystem,System.String,System.String)")]
[assembly: SuppressMessage("Build", "CA1305:The behavior of 'string.Format(string, object)' could vary based on the current user's locale settings. Replace this call in 'PreinstalledPackageInstaller.GetExtensionRepositoryPath(string, object, Action<string>)' with a call to 'string.Format(IFormatProvider, string, params object[])'.", Justification = "<Pending>", Scope = "member", Target = "~M:NuGet.VisualStudio.PreinstalledPackageInstaller.GetExtensionRepositoryPath(System.String,System.Object,System.Action{System.String})~System.String")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
<PackageDownload Include="Microsoft.VisualStudio.ProjectSystem.Managed" />
<PackageDownload Include="Microsoft.VisualStudio.ProjectSystem.Managed.VS" />
<PackageReference Include="Microsoft.VisualStudio.Sdk" />
<PackageReference Include="Microsoft.VSSDK.BuildTools" GeneratePathProperty="true" PrivateAssets="all" />
</ItemGroup>
<ItemGroup>
<Reference Include="Microsoft.VisualStudio.ProjectSystem.VS">
Expand All @@ -45,6 +46,9 @@
<Reference Include="Microsoft.VisualStudio.ProjectSystem.Managed.VS">
<HintPath>$(NuGetPackageRoot)microsoft.visualstudio.projectsystem.managed.vs\$(ProjectSystemManagedVersion)\lib\net472\Microsoft.VisualStudio.ProjectSystem.Managed.VS.dll</HintPath>
</Reference>
<Reference Include="Microsoft.VisualStudio.ExtensionEngine">
<HintPath>$(PkgMicrosoft_VSSDK_BuildTools)\tools\vssdk\Microsoft.VisualStudio.ExtensionEngine.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Update="CommonResources.resx">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public FileOpener([Import(typeof(SVsServiceProvider))] IServiceProvider serviceP

public void OpenFile(string filePath, bool isReadOnly)
{
ThreadHelper.ThrowIfNotOnUIThread();

var rdt = new RunningDocumentTable(_serviceProvider);

IVsWindowFrame? windowFrame = null;
Expand Down

0 comments on commit dbb62f4

Please sign in to comment.