如何使用IVsPackageSourceProvider
我可以获得一个NuGet源列表,例如https://www.nuget.org/api/v2/
吗?我尝试使用这样的结构:
using (Microsoft.VisualStudio.Shell.ServiceProvider provider = new Microsoft.VisualStudio.Shell.ServiceProvider((Microsoft.VisualStudio.OLE.Interop.IServiceProvider)base._dteObject))
{
IComponentModel service = (IComponentModel)provider.GetService(typeof(SComponentModel));
try
{
var sourceProvider = service.GetService<IVsPackageSourceProvider>();
}
catch(Exception ex)
{
}
}
但是Try
阻止了Exception
的文字:
无法转换类型的基础导出值 &#39; NuGet.VisualStudio.VsPackageSourceProvider (ContractName =&#34; NuGet.VisualStudio.IVsPackageSourceProvider&#34)&#39;输入 &#39; NuGet.VisualStudio.IVsPackageSourceProvider&#39;
请告诉我如何使用此提供商。
Sry for bad english
答案 0 :(得分:0)
您可以通过IVsPackageInstallerServices获取NuGet包列表,如下所示:
try
{
//Using the IComponentModel service
var componentModel = (IComponentModel)this.ServiceProvider.GetService(typeof(SComponentModel));
//var sourceProvider = componentModel.GetService<IVsPackageSourceProvider>();
IVsPackageInstallerServices installerServices =
componentModel.GetService<IVsPackageInstallerServices>();
var installedPackages = installerServices.GetInstalledPackages();
}
catch (Exception ex)
{
}
编辑: