【发布时间】:2011-09-06 12:40:03
【问题描述】:
我使用下面的代码来读取 .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);
}
}
【问题讨论】:
-
使用你的代码 (codekeep.net/snippets/170dc91f-1077-4c7f-ab05-8f82b9d1b682.aspx) 我总能得到程序集标题:i.stack.imgur.com/b7qGQ.png 那你在做什么?您在哪里运行该代码?
-
@Balexandre 尝试从 IIS7 上托管的 ASP.NET 应用程序执行代码
-
我将它托管在 IIS 7.5 Express 上并且工作正常。
-
注意:假设它是第一个属性 (
attribute[0]) 并不是那么好。这部分可以用更安全的方式写成:assembly.GetCustomAttributes()?.Where(x => x is AssemblyTitleAttribute).Select(x => ((AssemblyTitleAttribute) x).Title).FirstOrDefault(x => !string.IsNullOrWhiteSpace(x));
标签: asp.net reflection .net-assembly