Re: Custom Attribute and Instance Sample?
- From: Jon Skeet [C# MVP] <skeet@xxxxxxxxx>
- Date: Fri, 7 Dec 2007 00:58:16 -0000
Alun Harford <devnull@xxxxxxxxxxxxxxxxx> wrote:
coconet wrote:
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));
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();
--
Jon Skeet - <skeet@xxxxxxxxx>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
.
- Follow-Ups:
- Re: Custom Attribute and Instance Sample?
- From: Alun Harford
- Re: Custom Attribute and Instance Sample?
- References:
- Custom Attribute and Instance Sample?
- From: coconet
- Re: Custom Attribute and Instance Sample?
- From: Alun Harford
- Custom Attribute and Instance Sample?
- Prev by Date: Re: Custom Attribute and Instance Sample?
- Next by Date: RE: httpwebrequest with https behind proxy with authentication
- Previous by thread: Re: Custom Attribute and Instance Sample?
- Next by thread: Re: Custom Attribute and Instance Sample?
- Index(es):
Relevant Pages
|