在ASP.NET中读取AssemblyTitle属性

在ASP.NET中读取AssemblyTitle属性,asp.net,reflection,.net-assembly,Asp.net,Reflection,.net Assembly,我使用下面的代码读取.NET应用程序的AssemblyTitle属性,不幸的是Assembly.GetEntryAssembly()在ASP.NET应用程序中总是返回Null。如何在ASP.NET应用程序中读取AssemblyTitle public static string Title { get { var attributes = Assembly.GetEntryAssembly().GetCustomAttributes(typeo

我使用下面的代码读取.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);
      }
  }

我在asp.net web app中使用以下内容:

if (ApplicationDeployment.IsNetworkDeployed)
    return ApplicationDeployment.CurrentDeployment.CurrentVersion.ToString();
return System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
编辑:对不起,那只是版本,不是标题!我将你的版本和我的版本结合起来:

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

这样就可以很好地获得程序集标题属性。区别在于
getExecutionGassembly()
与您的
GetEntryAssembly()
我在asp.net web app中使用了以下内容:

if (ApplicationDeployment.IsNetworkDeployed)
    return ApplicationDeployment.CurrentDeployment.CurrentVersion.ToString();
return System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
编辑:对不起,那只是版本,不是标题!我将你的版本和我的版本结合起来:

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

这样就可以很好地获得程序集标题属性。区别在于
getExecutionGassembly()
与您的
GetEntryAssembly()
必须有一个类型,您知道该类型是在包含
AssemblyTitle
的同一程序集中定义的。然后你可以做:

typeof(MyType).Assembly.GetCustomAttributes
注意(据我所知)没有任何其他防弹方法

例如,如果您不想在web请求期间使用
HttpContext.Current
,则无法使用它(因此您可以在响应用户操作时使用,但不能从单独的线程、静态初始值设定项或从
global.asax

一些类似的阅读材料(满是成功的一半):


您必须拥有一个类型,您知道该类型是在包含
AssemblyTitle
的同一程序集中定义的。然后你可以做:

typeof(MyType).Assembly.GetCustomAttributes
注意(据我所知)没有任何其他防弹方法

例如,如果您不想在web请求期间使用
HttpContext.Current
,则无法使用它(因此您可以在响应用户操作时使用,但不能从单独的线程、静态初始值设定项或从
global.asax

一些类似的阅读材料(满是成功的一半):


使用您的代码()我总能得到程序集标题:那么您在做什么?您在哪里运行该代码?@Balexandre尝试执行IIS7I上托管的ASP.NET应用程序中的代码。我在IIS 7.5 Express上托管该应用程序,效果很好。注意:假设它是第一个属性(
属性[0]
)。该部分可以以更安全的方式编写为:
assembly.GetCustomAttributes()?。其中(x=>x是AssemblyTitleAttribute)。选择(x=>((AssemblyTitleAttribute)x)。Title)。FirstOrDefault(x=>!string.IsNullOrWhiteSpace(x))使用您的代码()我总能得到程序集标题:那么您在做什么?您在哪里运行该代码?@Balexandre尝试执行IIS7I上托管的ASP.NET应用程序中的代码。我在IIS 7.5 Express上托管该应用程序,效果很好。注意:假设它是第一个属性(
属性[0]
)。该部分可以以更安全的方式编写为:
assembly.GetCustomAttributes()?。其中(x=>x是AssemblyTitleAttribute)。选择(x=>((AssemblyTitleAttribute)x)。Title)。FirstOrDefault(x=>!string.IsNullOrWhiteSpace(x))Assembly.GetExecutionGassembly()的问题在于,如果代码位于类库中并在ASP.NET中引用,则标题将始终来自类库而不是ASP.NET应用程序。Assembly.GetExecutionGassembly()的问题在于,如果代码位于类库中并在ASP.NET中引用,标题将始终来自类库,而不是来自ASP.NET应用程序。