How to access Assembly Information in Asp.Net
How do you access the Assembly Information in Asp.Net (Title, Description, Company, etc.)? Do you have to use refle开发者_运维问答ction?
To read the information in the assembly level attributes (eg. AssemblyCopyrightAttribute
) you will need to use reflection. However with a little helper it is quite easy:
public static T GetAttribute<T>(this Assembly asm) where T : Attribute {
if (asm == null) { throw new ArgumentNullException("asm"); }
var attrs = asm.GetCustomAttributes(typeof(T), false);
if (attrs.Length != 1) {
throw new ApplicationException(
String.Format("Found {0} instances of {1} in {2}. Expected 1",
attrs.Length, typeof(T).Name, asm.FullName));
}
return (T)(attrs[0]);
}
and thus given a type, TargetType
, from that assembly:
string copyright = typeof(TargetType).Assembly.GetAttribute<AssemblyCopyrightAttribute>().Copyright;
You're just talking about web.config?
<configuration>
<system.web>
<compilation>
<assemblies>
<add assembly="<AssemblyName>, Version=<Version>, Culture=<Culture>, PublicKeyToken=<PublicKeyToken>"/>
</assemblies>
</compilation>
</system.web>
</configuration>
In code-behind, you might do this:
Imports System.Web.Configuration
...
WebConfigurationManager.GetSection("sectionyouwant")
Here's an example from msdn: http://msdn.microsoft.com/en-us/library/system.web.configuration.webconfigurationmanager.aspx
精彩评论