Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / December 2007

Tip: Looking for answers? Try searching our database.

Custom Attribute and Instance Sample?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
coconet - 06 Dec 2007 20:20 GMT
I have a currently executing assembly with 3 types, they are all
decorated with a Custom Attribute. Can anyone show a sample of how to
find all of my Types in the current Assembly that have the attribute,
and then put a New instance of each type in a List?

Thanks.
Jon Skeet [C# MVP] - 06 Dec 2007 20:27 GMT
> I have a currently executing assembly with 3 types, they are all
> decorated with a Custom Attribute. Can anyone show a sample of how to
> find all of my Types in the current Assembly that have the attribute,
> and then put a New instance of each type in a List?

Assembly source = Assembly.GetExecutingAssembly();

List<object> list = new List();
foreach (Type type in source.GetTypes())
{
   if (type.GetCustomAttributes(typeof(MyAttribute), false).Length!=0)
   {
       list.Add(Activator.CreateInstance(type));
   }
}

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

Alun Harford - 07 Dec 2007 00:44 GMT
>> I have a currently executing assembly with 3 types, they are all
>> decorated with a Custom Attribute. Can anyone show a sample of how to
[quoted text clipped - 7 lines]
> {
>     if (type.GetCustomAttributes(typeof(MyAttribute), false).Length!=0)

You can use type.IsDefined(...) for this.

Alun Harford
Jon Skeet [C# MVP] - 07 Dec 2007 00:56 GMT
> You can use type.IsDefined(...) for this.

Oops, yes indeed. I thought there was a simpler way :)

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

Alun Harford - 08 Dec 2007 15:20 GMT
>> You can use type.IsDefined(...) for this.
>
> Oops, yes indeed. I thought there was a simpler way :)

Why they didn't call it HasCustomAttribute I don't know...

Alun Harford
Alun Harford - 07 Dec 2007 00:42 GMT
> I have a currently executing assembly with 3 types, they are all
> decorated with a Custom Attribute. Can anyone show a sample of how to
> find all of my Types in the current Assembly that have the attribute,
> and then put a New instance of each type in a List?

C# 3:

Type[] result = Assembly.GetExecutingAssembly().GetTypes()
    .Where(t=>t.IsDefined(typeof(MyAttibute), false)
    .Select(t=>Activator.CreateInstance(t));
Jon Skeet [C# MVP] - 07 Dec 2007 00:58 GMT
> > I have a currently executing assembly with 3 types, they are all
> > decorated with a Custom Attribute. Can anyone show a sample of how to
[quoted text clipped - 6 lines]
>     .Where(t=>t.IsDefined(typeof(MyAttibute), false)
>     .Select(t=>Activator.CreateInstance(t));

The result type is wrong there, but hey, who's counting :)

I prefer it as a query expression though:

var result = from type in Assembly.GetExecutingAssembly().GetTypes()
            where type.IsDefined(typeof(MyAttribute), false)
            select Activator.CreateInstance(type);

List<object> objects = result.ToList();

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

Alun Harford - 08 Dec 2007 15:22 GMT
>>> I have a currently executing assembly with 3 types, they are all
>>> decorated with a Custom Attribute. Can anyone show a sample of how to
[quoted text clipped - 7 lines]
>
> The result type is wrong there, but hey, who's counting :)

That'll teach me for answering questions at 1am :-)
Good catch!

Alun Harford

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.