我正在为我的组织创建一个多项目解决方案模板,该模板需要安装Nuget软件包并在创建时使用Windows窗体设置一些自定义参数。我正在使用VSIX部署它。
我将这两个功能分开运行,但我还没有找到同时使用这两种功能的方法。根据{{3}} Visual Studio应该支持在项目模板中使用多个IWizard,如果我正确理解它的话。
我尝试过直接方法,只是在.vstemplate文件中添加对两个向导的引用,但只执行了第一个。我也尝试使用下面的代码在我的自定义向导中调用Nuget向导,但我想我必须使用packages-info设置WizardData以使其工作...
var asm = Assembly.Load("NuGet.VisualStudio.Interop, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");
var nuget = (IWizard)asm.CreateInstance("NuGet.VisualStudio.TemplateWizard");
nuget.RunStarted(automationObject, replacementsDictionary, runKind, customParams);
是否可以实现多个IWizard实现的使用,或者这是不可能的?
答案 0 :(得分:1)
不,你不能为每个模板注册多个IWizard派生类,在nuget的情况下,你可以通过这样的反射来调用:
private void AddNugetPackage(VSProject VsProj, string PackageName, string Version)
{
try
{
Assembly nugetAssembly = Assembly.Load("nuget.core");
Type packageRepositoryFactoryType = nugetAssembly.GetType("NuGet.PackageRepositoryFactory");
PropertyInfo piDefault = packageRepositoryFactoryType.GetProperty("Default");
MethodInfo miCreateRepository = packageRepositoryFactoryType.GetMethod("CreateRepository");
object repo = miCreateRepository.Invoke(piDefault.GetValue(null, null), new object[] { "https://packages.nuget.org/api/v2" });
Type packageManagerType = nugetAssembly.GetType("NuGet.PackageManager");
ConstructorInfo ciPackageManger = packageManagerType.GetConstructor(new Type[] { System.Reflection.Assembly.Load("nuget.core").GetType("NuGet.IPackageRepository"), typeof(string) });
DirectoryInfo di = new DirectoryInfo(ProjectPath);
string solPath = di.Parent.FullName;
string installPath = di.Parent.CreateSubdirectory("packages").FullName;
object packageManager = ciPackageManger.Invoke(new object[] { repo, installPath });
MethodInfo miInstallPackage = packageManagerType.GetMethod("InstallPackage",
new Type[] { typeof(string), System.Reflection.Assembly.Load("nuget.core").GetType("NuGet.SemanticVersion") });
string packageID = PackageName;
MethodInfo miParse = nugetAssembly.GetType("NuGet.SemanticVersion").GetMethod("Parse");
object semanticVersion = miParse.Invoke(null, new object[] { Version });
miInstallPackage.Invoke(packageManager, new object[] { packageID, semanticVersion });
}
catch(Exception ex)
{
// ...
return;
}
}