如何使用 IVsPackageSourceProvider 提供程序

How to use IVsPackageSourceProvider provider

如何使用 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 文本:

Cannot cast the underlying exported value of type 'NuGet.VisualStudio.VsPackageSourceProvider (ContractName="NuGet.VisualStudio.IVsPackageSourceProvider")' to type 'NuGet.VisualStudio.IVsPackageSourceProvider'

请告诉我如何使用此提供程序。

抱歉英语不好

您可以通过 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)
            {
            }

编辑:

代码: https://1drv.ms/u/s!AlvaNEnglADDe3a-mSTWubSix-w