.net 如何读取程序集属性
在我的程序中,如何读取AssemblyInfo.cs中设置的属性:.net 如何读取程序集属性,.net,reflection,assemblies,attributes,.net,Reflection,Assemblies,Attributes,在我的程序中,如何读取AssemblyInfo.cs中设置的属性: [assembly: AssemblyTitle("My Product")] [assembly: AssemblyDescription("...")] [assembly: AssemblyConfiguration("")] [assembly: AssemblyCompany("Radeldudel inc.")] [assembly: AssemblyProduct("My Product")] [assembly:
[assembly: AssemblyTitle("My Product")]
[assembly: AssemblyDescription("...")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Radeldudel inc.")]
[assembly: AssemblyProduct("My Product")]
[assembly: AssemblyCopyright("Copyright @ me 2008")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
我想向程序的用户显示其中的一些值,因此我想知道如何从主程序和我正在使用的组件程序集中加载它们。这相当简单。你必须使用反射。您需要一个Assembly实例,该实例表示具有要读取的属性的程序集。一个简单的方法是:
typeof(MyTypeInAssembly).Assembly
然后您可以执行此操作,例如:
object[] attributes = assembly.GetCustomAttributes(typeof(AssemblyProductAttribute), false);
AssemblyProductAttribute attribute = null;
if (attributes.Length > 0)
{
attribute = attributes[0] as AssemblyProductAttribute;
}
引用
attribute.Product
现在将为您提供在AssemblyInfo.cs中传递给属性的值。当然,如果您查找的属性可能出现多次,则GetCustomAttributes返回的数组中可能会有多个实例,但对于希望检索的组件级属性,这通常不是问题。好,对于最初的问题,现在可能有点过时了,但无论如何,我将提出这个问题以供将来参考
如果要从部件本身内部执行此操作,请使用以下命令:
using System.Runtime.InteropServices;
using System.Reflection;
object[] customAttributes = this.GetType().Assembly.GetCustomAttributes(false);
然后,您可以遍历所有自定义属性以找到所需的属性,例如
foreach (object attribute in customAttributes)
{
string assemblyGuid = string.Empty;
if (attribute.GetType() == typeof(GuidAttribute))
{
assemblyGuid = ((GuidAttribute) attribute).Value;
break;
}
}
我创建了这个使用Linq的扩展方法:
public static T GetAssemblyAttribute<T>(this System.Reflection.Assembly ass) where T : Attribute
{
object[] attributes = ass.GetCustomAttributes(typeof(T), false);
if (attributes == null || attributes.Length == 0)
return null;
return attributes.OfType<T>().SingleOrDefault();
}
public static T GetAssemblyAttribute(this System.Reflection.Assembly)其中T:Attribute
{
object[]attributes=ass.GetCustomAttributes(typeof(T),false);
if(attributes==null | | attributes.Length==0)
返回null;
返回属性.OfType().SingleOrDefault();
}
这样你就可以方便地使用它了:
var attr = targetAssembly.GetAssemblyAttribute<AssemblyDescriptionAttribute>();
if(attr != null)
Console.WriteLine("{0} Assembly Description:{1}", Environment.NewLine, attr.Description);
var attr=targetAssembly.GetAssemblyAttribute();
如果(attr!=null)
WriteLine(“{0}程序集描述:{1}”,Environment.NewLine,attr.Description);
好的,我试着浏览了很多资源,找到了一种方法来提取程序集的.dll属性。LoadFrom(path)
。但不幸的是,我找不到任何好的资源。这个问题是搜索c#get assembly attributes
的最重要的结果(至少对我来说),所以我想和大家分享我的工作
我编写了以下简单的控制台程序,以在经过数小时的努力后检索general assembly属性。在这里,我提供了代码,因此任何人都可以使用它进行进一步的参考工作
我为此使用CustomAttributes
属性。请随意评论这种方法
代码:
using System;
using System.Reflection;
namespace MetaGetter
{
class Program
{
static void Main(string[] args)
{
Assembly assembly = Assembly.LoadFrom("Path to assembly");
foreach (CustomAttributeData attributedata in assembly.CustomAttributes)
{
Console.WriteLine(" Name : {0}",attributedata.AttributeType.Name);
foreach (CustomAttributeTypedArgument argumentset in attributedata.ConstructorArguments)
{
Console.WriteLine(" >> Value : {0} \n" ,argumentset.Value);
}
}
Console.ReadKey();
}
}
}
样本输出:
Name : AssemblyTitleAttribute
>> Value : "My Product"
我用这个:
public static string Title
{
get
{
return GetCustomAttribute<AssemblyTitleAttribute>(a => a.Title);
}
}
公共静态字符串标题
{
得到
{
返回GetCustomAttribute(a=>a.Title);
}
}
供参考:
using System;
using System.Reflection;
using System.Runtime.CompilerServices;
namespace Extensions
{
public static class AssemblyInfo
{
private static Assembly m_assembly;
static AssemblyInfo()
{
m_assembly = Assembly.GetEntryAssembly();
}
public static void Configure(Assembly ass)
{
m_assembly = ass;
}
public static T GetCustomAttribute<T>() where T : Attribute
{
object[] customAttributes = m_assembly.GetCustomAttributes(typeof(T), false);
if (customAttributes.Length != 0)
{
return (T)((object)customAttributes[0]);
}
return default(T);
}
public static string GetCustomAttribute<T>(Func<T, string> getProperty) where T : Attribute
{
T customAttribute = GetCustomAttribute<T>();
if (customAttribute != null)
{
return getProperty(customAttribute);
}
return null;
}
public static int GetCustomAttribute<T>(Func<T, int> getProperty) where T : Attribute
{
T customAttribute = GetCustomAttribute<T>();
if (customAttribute != null)
{
return getProperty(customAttribute);
}
return 0;
}
public static Version Version
{
get
{
return m_assembly.GetName().Version;
}
}
public static string Title
{
get
{
return GetCustomAttribute<AssemblyTitleAttribute>(
delegate(AssemblyTitleAttribute a)
{
return a.Title;
}
);
}
}
public static string Description
{
get
{
return GetCustomAttribute<AssemblyDescriptionAttribute>(
delegate(AssemblyDescriptionAttribute a)
{
return a.Description;
}
);
}
}
public static string Product
{
get
{
return GetCustomAttribute<AssemblyProductAttribute>(
delegate(AssemblyProductAttribute a)
{
return a.Product;
}
);
}
}
public static string Copyright
{
get
{
return GetCustomAttribute<AssemblyCopyrightAttribute>(
delegate(AssemblyCopyrightAttribute a)
{
return a.Copyright;
}
);
}
}
public static string Company
{
get
{
return GetCustomAttribute<AssemblyCompanyAttribute>(
delegate(AssemblyCompanyAttribute a)
{
return a.Company;
}
);
}
}
public static string InformationalVersion
{
get
{
return GetCustomAttribute<AssemblyInformationalVersionAttribute>(
delegate(AssemblyInformationalVersionAttribute a)
{
return a.InformationalVersion;
}
);
}
}
public static int ProductId
{
get
{
return GetCustomAttribute<AssemblyProductIdAttribute>(
delegate(AssemblyProductIdAttribute a)
{
return a.ProductId;
}
);
}
}
public static string Location
{
get
{
return m_assembly.Location;
}
}
}
}
使用系统;
运用系统反思;
使用System.Runtime.CompilerServices;
命名空间扩展
{
公共静态类AssemblyInfo
{
私有静态组件m_组件;
静态汇编信息()
{
m_assembly=assembly.GetEntryAssembly();
}
公共静态无效配置(程序集ass)
{
m_组件=ass;
}
公共静态T GetCustomAttribute(),其中T:Attribute
{
object[]customAttributes=m_assembly.GetCustomAttributes(typeof(T),false);
如果(customAttributes.Length!=0)
{
返回(T)((对象)customAttributes[0]);
}
返回默认值(T);
}
公共静态字符串GetCustomAttribute(Func getProperty),其中T:Attribute
{
T customAttribute=GetCustomAttribute();
if(customAttribute!=null)
{
返回getProperty(customAttribute);
}
返回null;
}
公共静态int-GetCustomAttribute(Func-getProperty),其中T:Attribute
{
T customAttribute=GetCustomAttribute();
if(customAttribute!=null)
{
返回getProperty(customAttribute);
}
返回0;
}
公共静态版本
{
得到
{
返回m_assembly.GetName().Version;
}
}
公共静态字符串标题
{
得到
{
返回GetCustomAttribute(
代表(集合标题属性a)
{
返回a.标题;
}
);
}
}
公共静态字符串描述
{
得到
{
返回GetCustomAttribute(
委托(AssemblyDescriptionAttribute a)
{
返回a.描述;
}
);
}
}
公共静态字符串产品
{
得到
{
返回GetCustomAttribute(
委托(AssemblyProductAttribute a)
{
退货a.产品;
}
);
}
}
公共静态字符串版权
{
得到
{
返回GetCustomAttribute(
委托(属性a)
{
返回a.版权;
}
);
}
}
公共静态字符串公司
{
得到
{
返回GetCustomAttribute(
代表(大会公司属性a)
{
返回a.公司;
}
);
}
}
公共静态字符串信息版本
{
得到
{
返回GetCustomAttribute(
代表(汇编信息版本属性a)
{
返回a.InformationalVersion;
}
);
internal static class AssemblyInfo
{
public static string Company { get { return GetExecutingAssemblyAttribute<AssemblyCompanyAttribute>(a => a.Company); } }
public static string Product { get { return GetExecutingAssemblyAttribute<AssemblyProductAttribute>(a => a.Product); } }
public static string Copyright { get { return GetExecutingAssemblyAttribute<AssemblyCopyrightAttribute>(a => a.Copyright); } }
public static string Trademark { get { return GetExecutingAssemblyAttribute<AssemblyTrademarkAttribute>(a => a.Trademark); } }
public static string Title { get { return GetExecutingAssemblyAttribute<AssemblyTitleAttribute>(a => a.Title); } }
public static string Description { get { return GetExecutingAssemblyAttribute<AssemblyDescriptionAttribute>(a => a.Description); } }
public static string Configuration { get { return GetExecutingAssemblyAttribute<AssemblyConfigurationAttribute>(a => a.Configuration); } }
public static string FileVersion { get { return GetExecutingAssemblyAttribute<AssemblyFileVersionAttribute>(a => a.Version); } }
public static Version Version { get { return Assembly.GetExecutingAssembly().GetName().Version; } }
public static string VersionFull { get { return Version.ToString(); } }
public static string VersionMajor { get { return Version.Major.ToString(); } }
public static string VersionMinor { get { return Version.Minor.ToString(); } }
public static string VersionBuild { get { return Version.Build.ToString(); } }
public static string VersionRevision { get { return Version.Revision.ToString(); } }
private static string GetExecutingAssemblyAttribute<T>(Func<T, string> value) where T : Attribute
{
T attribute = (T)Attribute.GetCustomAttribute(Assembly.GetExecutingAssembly(), typeof(T));
return value.Invoke(attribute);
}
}
internal class ApplicationData
{
DirectoryInfo roamingDataFolder;
DirectoryInfo localDataFolder;
DirectoryInfo appDataFolder;
public ApplicationData()
{
appDataFolder = new DirectoryInfo(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), AssemblyInfo.Product,"Data"));
roamingDataFolder = new DirectoryInfo(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),AssemblyInfo.Product));
localDataFolder = new DirectoryInfo(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), AssemblyInfo.Product));
if (!roamingDataFolder.Exists)
roamingDataFolder.Create();
if (!localDataFolder.Exists)
localDataFolder.Create();
if (!appDataFolder.Exists)
appDataFolder.Create();
}
/// <summary>
/// Gets the roaming application folder location.
/// </summary>
/// <value>The roaming data directory.</value>
public DirectoryInfo RoamingDataFolder => roamingDataFolder;
/// <summary>
/// Gets the local application folder location.
/// </summary>
/// <value>The local data directory.</value>
public DirectoryInfo LocalDataFolder => localDataFolder;
/// <summary>
/// Gets the local data folder location.
/// </summary>
/// <value>The local data directory.</value>
public DirectoryInfo AppDataFolder => appDataFolder;
}
string company = ((AssemblyCompanyAttribute)Attribute.GetCustomAttribute(Assembly.GetExecutingAssembly(), typeof(AssemblyCompanyAttribute), false)).Company;