2016-08-03 101 views
0

我为要设置用户提供的某些属性的VS项目创建了一个模板。所以,我实现IWizard - 接口:使用IWizard更改项目属性

public void RunStarted(object automationObject, Dictionary<string, string> replacementsDictionary, WizardRunKind runKind, object[] customParams) 
{ 
    string projectPath = replacementsDictionary["$destinationdirectory$"]; 
    string projectName = replacementsDictionary["$projectname$"]; 

    SchemaWizardForm frm = new SchemaWizardForm(projectPath, projectName); 
    frm.ShowDialog(); 
    this.m_assemblyInfo = frm.AssemblyInfo; 

    replacementsDictionary["$assemblyTitle$"] = frm.AssemblyInfo.AssemblyName; 
    replacementsDictionary["$assemblyName$"] = frm.AssemblyInfo.AssemblyName; 
    replacementsDictionary["$copyrightYear$"] = DateTime.Now.Month + "/" + DateTime.Now.Year; 
    replacementsDictionary["$defaultNamespace$"] = frm.AssemblyInfo.RootNamespace; 
} 

我template's项目文件中现在,我有这样的事情:

<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> 
    <PropertyGroup> 
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> 
    <Platform Condition=" '$(Platform)' == '' ">x86</Platform> 
    <ProductVersion>8.0.30703</ProductVersion> 
    <SchemaVersion>2.0</SchemaVersion> 
    <OutputType>Library</OutputType> 
    <AppDesignerFolder>Properties</AppDesignerFolder> 
    <RootNamespace>$defaultNamespace$</RootNamespace> 
    <AssemblyName>$assemblyName$</AssemblyName> 
    <TargetFrameworkVersion>v3.5</TargetFrameworkVersion> 
    <FileAlignment>512</FileAlignment> 
    </PropertyGroup> 
</Project> 

而且在AssemblyInfo.cs

[assembly: AssemblyTitle("$assemblyTitle$")] 
[assembly: AssemblyCopyright("(C) $copyrightYear$ MyCompany")] 
// ... 

我能成功地使用向导创建我的项目。它的版权日期从AssemblyInfo.cs后生成是[assembly: AssemblyCopyright("(C) 8/2016 MyCompany")] - 这很好。但RootNamespace的值与projectsname类似。我已经调试了上面的代码,其中replacementsDictionary为命名空间提供了正确的条目(请参见下图)。

although the map has the correct values the result-project does not have those properties set appropriately

下面是结果项目

enter image description here

正如你可以看到两个Assembly NameDefault namespace含有projectsname什么,而不是我在用户窗体中输入(值在我的情况下为asdas)。然而,版权日期可通过Assembly Information-按钮项目 - >产品 - >应用程序正确设置(图中未显示)。

是不是可以使用replacementDictionary更改项目属性自己?

回答

0

显然我们不能通过这样的字典来改变项目的属性。我们可以做的是修改此地图中的projectname-入口,这为我解决了问题。现在Assembly title,Assembly nameDefault namespace都指向相同的变量。这有点烦人(特别是我们不能独立更改程序集名称和名称空间),但这对我很有用。

replacementsDictionary["$projectname$"] = frm.AssemblyInfo.ProjectName; 
replacementsDictionary["$safeprojectname$"] = frm.AssemblyInfo.ProjectName;