我有一個類,只是返回AssemblyInfo.cs中的值(此代碼是Windows Phone的):生成WMAppManifest.xml與T4
using System.Runtime.InteropServices;
using System.Reflection;
namespace Tiletoons
{
class AppInfo
{
public static readonly string Id = string.Empty;
public static readonly string Product = string.Empty;
public static readonly string Company = string.Empty;
public static readonly string Version = string.Empty;
static AppInfo()
{
Assembly assembly = Assembly.GetExecutingAssembly();
foreach (object attribute in assembly.GetCustomAttributes(false)) {
if (attribute.GetType() == typeof(GuidAttribute)) {
Id = (attribute as GuidAttribute).Value;
} else if (attribute.GetType() == typeof(AssemblyProductAttribute)) {
Product = (attribute as AssemblyProductAttribute).Product;
} else if (attribute.GetType() == typeof(AssemblyCompanyAttribute)) {
Company = (attribute as AssemblyCompanyAttribute).Company;
}
}
Version = assembly.FullName.Split('=')[1].Split(',')[0];
}
}
}
...我想用它來通過T4轉換自動生成我的WMAppManifest.xml;這裏是我的ttinclude文件:
<#@ assembly name="System.Core" #>
<#@ assembly name="EnvDTE" #>
<#@ import namespace="EnvDTE" #>
<#@ import namespace="System" #>
<#
IServiceProvider hostServiceProvider = Host as IServiceProvider;
EnvDTE.DTE dte = hostServiceProvider.GetService(typeof(EnvDTE.DTE)) as EnvDTE.DTE;
EnvDTE.ProjectItem containingProjectItem = dte.Solution.FindProjectItem(Host.TemplateFile);
Project project = containingProjectItem.ContainingProject;
var projectName = project.FullName;
ProjectItem deploymentConfiguration = GetProjectItem(project, "AppInfo.cs");
if (deploymentConfiguration == null) {
throw new Exception("Unable to resolve AppInfo.cs");
}
var codeModel = deploymentConfiguration.FileCodeModel;
string id = null;
string product = null;
string company = null;
string version = null;
foreach (CodeElement codeElement in codeModel.CodeElements) {
if (codeElement.Name == "AppInfo") {
CodeClass codeClass = codeElement as CodeClass;
foreach (CodeElement memberElement in codeClass.Members) {
CodeVariable variable = memberElement as CodeVariable;
switch (memberElement.Name) {
case "Id":
id = variable.InitExpression as string;
break;
case "Product":
product = variable.InitExpression as string;
break;
case "Company":
company = variable.InitExpression as string;
break;
case "Version":
version = variable.InitExpression as string;
break;
}
}
}
}
#>
<#+ EnvDTE.ProjectItem GetProjectItem(Project project, string fileName)
{
foreach (ProjectItem projectItem in project.ProjectItems) {
if (projectItem.Name.EndsWith(fileName)) {
return projectItem;
}
var item = GetProjectItem(projectItem, fileName);
if (item != null) {
return item;
}
}
return null;
}
EnvDTE.ProjectItem GetProjectItem(EnvDTE.ProjectItem projectItem, string fileName)
{
if (projectItem.ProjectItems != null && projectItem.ProjectItems.Count > 0) {
foreach (ProjectItem item in projectItem.ProjectItems) {
if (item.Name.EndsWith(fileName)) {
return item;
}
}
}
return null;
}
#>
...終於在這裏是我的清單模板:
<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ output extension=".xml" #>
<#@ include file="AppInfo.ttinclude" #>
<?xml version="1.0" encoding="utf-8"?>
<Deployment xmlns="http://schemas.microsoft.com/windowsphone/2009/deployment" AppPlatformVersion="7.0">
<App xmlns="" ProductID="<#= id #>" Title="<#= product #>" RuntimeType="XNA" Version="<#= version #>" Genre="apps.games" Author="Gonzo" Description="<#= description #>" Publisher="<#= company #>">
<IconPath IsRelative="true" IsResource="false">
...
</App>
</Deployment>
是否有可能使用填充類像我(了AppInfo)清單模板?我不想重複已經在AssemblyInfo.cs中定義的常量,即這個想法是查看從那裏發佈我的WP應用程序所需的所有信息。
任何想法將是真正歡迎:-)
J3D
你想達到什麼目的?清單是由Visual Studio爲您創建的。你用T4生成它會得到什麼? – 2012-04-26 21:41:50