質問

以下のコードを使用して、.NETアプリのAssemblyTitle属性を読み取ります。残念ながら、Assembly.getEntryAssembly()は、ASP.NETアプリで常にnullを返します。 ASP.NETアプリでAssemblyTitleを読む方法は?

  public static string Title
  {
      get
      {
          var attributes = Assembly.GetEntryAssembly().GetCustomAttributes(typeof(AssemblyTitleAttribute), false);
          if (attributes.Length > 0)
          {
              var titleAttribute = (AssemblyTitleAttribute)attributes[0];
              if (titleAttribute.Title.Length > 0)
                  return titleAttribute.Title;
          }
          return System.IO.Path.GetFileNameWithoutExtension(Assembly.GetEntryAssembly().CodeBase);
      }
  }
役に立ちましたか?

解決

あなたが知っているタイプが必要です。 AssemblyTitle. 。その後、あなたはできます:

typeof(MyType).Assembly.GetCustomAttributes

(私が知っていることのために)他の防弾法はないことに注意してください。

たとえば、使用します HttpContext.Current Webリクエスト中にはしたくない場合は機能しません(したがって、ユーザーアクションの応答では、別のスレッド、静的イニシャル、または global.asax)

いくつかの同様の読み物(成功の半分でいっぱい):

WebアプリケーションのgetEntryAssembly

アセンブリからWebアプリケーションバージョン番号を使用する(ASP.net/c#)

他のヒント

ASP.NET Webアプリで以下を使用します。

if (ApplicationDeployment.IsNetworkDeployed)
    return ApplicationDeployment.CurrentDeployment.CurrentVersion.ToString();
return System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();

編集:申し訳ありませんが、それは単なるバージョンであり、タイトルではありません!私はあなたのバージョンと私のものを組み合わせました:

System.Reflection.Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyTitleAttribute), false); 

これにより、アセンブリタイトル属性は問題ありません。違いはあります GetExecutingAssembly() あなたとあなた GetEntryAssembly().

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top