PC Review


Reply
Thread Tools Rate Thread

Assembly: custom attributes

 
 
julien
Guest
Posts: n/a
 
      7th Feb 2005
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[i]);
}

=>
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
 
Reply With Quote
 
 
 
 
Mattias Sjögren
Guest
Posts: n/a
 
      7th Feb 2005

>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[i] as
AssemblyCompanyAttribute;
if ( aca != null )
Console.WriteLine( aca.Company );



Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
 
Reply With Quote
 
julien
Guest
Posts: n/a
 
      7th Feb 2005
Mattias Sjögren wrote:
>>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[i] 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
 
Reply With Quote
 
Mattias Sjögren
Guest
Posts: n/a
 
      7th Feb 2005

>How can I do if I don't know the different types?


Use Reflection on the attribute objects.



Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
 
Reply With Quote
 
julien
Guest
Posts: n/a
 
      9th Feb 2005
Mattias Sjögren wrote:
>>How can I do if I don't know the different types?

>
>
> 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
 
Reply With Quote
 
Sean Hederman
Guest
Posts: n/a
 
      9th Feb 2005
You can create and use attributes as you see fit:

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

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

"julien" <(E-Mail Removed)> wrote in message
news:4209a120$0$524$(E-Mail Removed)...
> Mattias Sjögren wrote:
>>>How can I do if I don't know the different types?

>>
>>
>> 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



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Use of Assembly Attributes Shalom Shachne Microsoft C# .NET 1 30th Jun 2010 04:28 PM
WebControl.Attributes.Add and custom attributes P4trykx Microsoft ASP .NET 2 31st Jan 2007 04:33 PM
Is it possible to Define multiple assembly attributes of the sametype in one assembly? Andreas Mueller Microsoft C# .NET 1 11th May 2006 11:03 PM
Enumerate Assembly Attributes? xenophon Microsoft Dot NET Framework 3 25th May 2005 02:38 PM
Setting assembly attributes Madhav Desetty Microsoft C# .NET 3 20th Nov 2003 06:30 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:12 PM.