• 検索結果がありません。

26 :バージョン情報などを取得するための ProductInfo クラスの定義

ドキュメント内 Windows Presentation Foundation (ページ 130-133)

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

23 24 25 26 27 28 29

namespace YKWpfIntroduction.Practices.Models {

using System;

using System.Reflection;

/// <summary>

/// プロダクト情報を取得するための静的なクラスを表します。

/// </summary>

public static class ProductInfo {

/// <summary>

/// 自分のアセンブリを保持します。

/// </summary>

private static Assembly assembly = Assembly.GetExecutingAssembly();

private static string title;

/// <summary>

/// アプリケーションの名前を取得します。

/// </summary>

public static string Title {

get { return title ?? (title =

((AssemblyTitleAttribute)Attribute.GetCustomAttribute(assembly, typeof(AssemblyTitleAttribute))).Title); }

}

private static string description;

/// <summary>

/// アプリケーションの詳細を取得します。

/// </summary>

public static string Description

30 31

32 33 34 35 36 37 38 39 40

41 42 43 44 45 46 47 48 49

50 51 52 53 54 55 56 57 58

59 60 61 62 63 64 65 66 67

68 69 70 71 72 73 74 75 76 77 78 79

{

get { return description ?? (description =

((AssemblyDescriptionAttribute)Attribute.GetCustomAttribute(assembly, typeof(AssemblyDescriptionAttribute))).Description); }

}

private static string company;

/// <summary>

/// アプリケーション開発元を取得します。

/// </summary>

public static string Company {

get { return company ?? (company =

((AssemblyCompanyAttribute)Attribute.GetCustomAttribute(assembly, typeof(AssemblyCompanyAttribute))).Company); }

}

private static string product;

/// <summary>

/// アプリケーションのプロダクト名を取得します。

/// </summary>

public static string Product {

get { return product ?? (product =

((AssemblyProductAttribute)Attribute.GetCustomAttribute(assembly, typeof(AssemblyProductAttribute))).Product); }

}

private static string copyright;

/// <summary>

/// アプリケーションのコピーライトを取得します。

/// </summary>

public static string Copyright {

get { return copyright ?? (copyright =

((AssemblyCopyrightAttribute)Attribute.GetCustomAttribute(assembly, typeof(AssemblyCopyrightAttribute))).Copyright); }

}

private static string trademark;

/// <summary>

/// アプリケーションのトレードマークを取得します。

/// </summary>

public static string Trademark {

get { return trademark ?? (trademark =

((AssemblyTrademarkAttribute)Attribute.GetCustomAttribute(assembly, typeof(AssemblyTrademarkAttribute))).Trademark); }

}

private static Version version;

/// <summary>

/// アプリケーションのバージョンを取得します。

/// </summary>

public static Version Version {

get { return version ?? (version = assembly.GetName().Version); } }

private static string versionString;

80 81 82 83 84 85

86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128

/// <summary>

/// アプリケーションのバージョン文字列を取得します。

/// </summary>

public static string VersionString {

get { return versionString ?? (versionString = string.Format("{0}{1}{2}{3}",

Version.ToString(3), IsBetaMode ? " β" : "", Version.Revision == 0 ? "" : " rev." + Version.Revision, IsDebugMode ? " Debug Mode" : "")); }

}

/// <summary>

/// ビルド時の CLR バージョン文字列を取得します。

/// </summary>

public static string CLRBuildVersion {

get { return System.Reflection.Assembly.GetExecutingAssembly().ImageRuntimeVersion; } }

/// <summary>

/// 実行中の CLR バージョン文字列を取得します。

/// </summary>

public static string CLRExecuteVersion {

get { return System.Runtime.InteropServices.RuntimeEnvironment.GetSystemVersion(); } }

/// <summary>

/// デバッグモードかどうか確認します。

/// </summary>

public static bool IsDebugMode {

#if DEBUG

get { return true; }

#else

get { return false; }

#endif }

/// <summary>

/// ベータ版かどうか確認します。

/// </summary>

public static bool IsBetaMode {

#if BETA

get { return true; }

#else

get { return false; }

#endif } } }

14 行目で現在実行中のアセンブリを取得し、これを元にリフレクション機能を利用しながら各情報を取得しています。

バージョン情報は 76 行目のように取得できます。また、ここで得たバージョン情報を元に独自の書式のバージョン表記に 変換したものを VersionString プロパティとして 83 行目に定義しています。85 行目を見ると複雑に見えますが、 "?"

記号による二項演算子で条件によって文字列を追加したりしなかたりしているだけなので、ひとつひとつ分解しながら見て いけばわかると思います。

ここで、107 行目や 119 行目に定義している IsDebugMode プロパティや IsBetaMode プロパティを見てみましょう。

#if ~ #endif で条件分岐させることで、コンパイルシンボルに "DEBUG" が定義されているときは IsDebugMode プロ パティが true となるようにしています。同様にコンパイルシンボルに "BETA" が定義されているときは IsBetaMode プ

ロパティが true となるようにしています。このプロパティを用いることで VersionString プロパティが返す文字列を 切り替えるようにしています。つまり、Debug モードでビルドしたり、β版としてビルドした場合、バージョン情報にその 情報が表示されるようにしています。

コンパイラシンボルはソリューションエクスプローラーの "Properties" をダブルクリックすると開く設定画面で設定 できます。

図 5.15:「ビルド」メニューの上部にある「条件付きコンパイラシンボル」で任意文字列を設定できる

Debug 構成に対する設定では、図 5.15 にあるように「DEBUG 定数の定義」チェックボックスがデフォルトで true に なっているので、Debug 構成によるコンパイルでは自動的に "DEBUG" というコンパイルシンボルが認識されることになり ます。β版としての構成を作成した場合、「条件付きコンパイルシンボル」のテキストボックスに "BETA" と入力すること で、"BETA" をコンパイルシンボルとして認識するようになります。

ドキュメント内 Windows Presentation Foundation (ページ 130-133)