问题描述
在我的计划,我怎么能读AssemblyInfo.cs中设置的属性:
In my program, how can I read the properties set in AssemblyInfo.cs:
[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("")]
我想显示一些值,以我的程序的用户,所以我想知道如何从主程序和komponent组件我使用加载它们。
I'd like to display some of these values to the user of my program, so I'd like to know how to load them from the main program and from komponent assemblies I'm using.
推荐答案
这是相当容易的。你必须使用反射。你需要议会的再presents你想读的属性的组件实例。得到这一个简单的方法就是做:
This is reasonably easy. You have to use reflection. You need an instance of Assembly that represents the assembly with the attributes you want to read. An easy way of getting this is to do:
typeof(MyTypeInAssembly).GetAssembly()
然后就可以做到这一点,例如:
Then you can do this, for example:
object[] attributes = assembly.GetCustomAttributes(typeof(AssemblyProductAttribute), false);
AssemblyProductAttribute attribute = null;
if (attributes.Length > 0)
{
attribute = attributes[0] as AssemblyProductAttribute;
}
引用 attribute.Product
现在给你传递给你的AssemblyInfo.cs中属性的值。当然,如果您要查找的属性可以出现多次,你可能会得到多个实例由GetCustomAttributes返回的数组中,但是这通常不是一个问题汇编级属性,如那些你希望能找回。
Referencing attribute.Product
will now give you the value you passed to the attribute in your AssemblyInfo.cs. Of course, if the attribute you look for can occur more than once, you may get multiple instances in the array returned by GetCustomAttributes, but this is not usually an issue for assembly level attributes like the ones you hope to retrieve.
这篇关于如何阅读组件的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!