2

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

...そして最後に、これが私のマニフェストテンプレートです:

<#@ 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

4

1 に答える 1

0

さて、アプリには 2 つの異なる展開構成 (ライト エディションまたはプロ エディション) があり、構成ごとに特定の Guid、タイトル名、バージョンなどが必要です。このように WMAppManifest.tt を変更することで問題を解決できました。 :

<#@ 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 とその他の必要なデータを 1 回だけ定義するだけです。これが私の 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 など) に応じて、正しい情報を含むアプリ マニフェストを生成します。それが役立つことを願っています。

j3d

于 2012-04-27T07:15:25.687 に答える