gpt4 book ai didi

c# - 使用 T4 生成 WMAppManifest.xml

转载 作者:太空宇宙 更新时间:2023-11-03 16:32:43 25 4
gpt4 key购买 nike

我有一个类只返回 AssemblyInfo.cs 中的值(此代码适用于 Windows Phone):

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;
}
#>

...最后这是我的 list 模板:

<#@ 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) 来填充 list 模板?我不想重复已在 AssemblyInfo.cs 中定义的常量,即我的想法是从那里查看发布我的 WP 应用程序所需的所有信息。

任何想法都非常受欢迎:-)

j3d

最佳答案

好吧,我的应用程序有两种不同的部署配置(精简版或专业版),对于每种配置我都需要特定的 Guid、标题名称、版本等。我能够通过修改 WMAppManifest 来解决我的问题。像这样:

<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ output extension=".xml" #>
<#@ assembly name="$(TargetPath)" #>
<?xml version="1.0" encoding="utf-8"?>
<Deployment xmlns="http://schemas.microsoft.com/windowsphone/2009/deployment" AppPlatformVersion="7.0">
<App xmlns="" ProductID="<#= AppInfo.Id #>" Title="<#= AppInfo.Product + " " + AppInfo.Edition #>" RuntimeType="XNA" Version="<#= AppInfo.Version #>" Genre="apps.games" Author="Gonzo" Description="<#= AppInfo.Description #>" Publisher="<#= AppInfo.Company #>">
...
</App>
</Deployment>

在上面的代码片段中,我在程序集指令中指定了 $(TargetPath)(这指向我在 bin 目录中编译的程序集),最终我能够从 AssemblyInfo 中查看自定义属性。这样,我只定义了一次 Guid 和其他需要的数据——这是我的 AssemblyInfo 的一部分:

...
#if !LITE_EDITION
[assembly: Guid("716ab9de-f88e-9a14-8d81-65sn67dbf99e")]
[assembly: AssemblyEdition("Pro")]
#else
[assembly: Guid("91e6742f-6273-1a8b-55a69-e70ad5644827")]
[assembly: AssemblyEdition("Lite")]
#endif
...

根据当前配置(例如 Debug、Debug Lite、Release、Release Lite),我生成了包含正确信息的应用程序 list 。希望对您有所帮助。

j3d

关于c# - 使用 T4 生成 WMAppManifest.xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10323563/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com