Assembly: custom attributes

  • Thread starter Thread starter julien
  • Start date Start date
J

julien

Hello,
my program loads a couple of DLLs. I use System.Reflection to obtain
information about them.

I a DLL, I have:
[assembly:AssemblyTitle("First plugin")]
[assembly:AssemblyCompany("MySelf")]
[assembly:AssemblyProduct("My first plugin")]

I tried to retrive these information in my program. RIght now, I can get
the types:
Object[] myAttributes = asm.GetCustomAttributes(false); //asm est un
Assembly
for(int i = 0; i < myAttributes.Length; i++)
{
Console.WriteLine("attribute {0}", myAttributes);
}

=>
attribute System.Reflection.AssemblyProductAttribute
attribute System.Reflection.AssemblyCompanyAttribute
attribute System.Reflection.AssemblyTitleAttribute

But then I'm not able to get the values ("MySelf", "First plugin", ...).

Thank you

Julien
 
But then I'm not able to get the values ("MySelf", "First plugin", ...).

Here's how to get "MySelf". The same principle can be used for the
other attributes.

AssemblyCompanyAttribute aca = myAttributes as
AssemblyCompanyAttribute;
if ( aca != null )
Console.WriteLine( aca.Company );



Mattias
 
Mattias said:
But then I'm not able to get the values ("MySelf", "First plugin", ...).


Here's how to get "MySelf". The same principle can be used for the
other attributes.

AssemblyCompanyAttribute aca = myAttributes as
AssemblyCompanyAttribute;
if ( aca != null )
Console.WriteLine( aca.Company );



Mattias


Thank you. But this means I know all the potential attributes name
before I load the DLL. How can I do if I don't know the different types?


Julien
 
Mattias said:
Use Reflection on the attribute objects.



Mattias

Actually, I thought I could create any CustomAttribute I want, but it
looks like the list of CustomAttribute is already define (i.e.
AssemblyMyVariable cannot be created).

Thanks
Julien
 
You can create and use attributes as you see fit:

[AttributeUsage(AttributeTargets.Assembly)]
public class MyCustomAttribute : Attribute {
....
}

in AssemblyInfo.cs:
[assembly: MyCustomAttribute()]
 
Back
Top