Custom Attribute and Instance Sample?

C

coconet

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

Jon Skeet [C# MVP]

coconet said:
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));
}
}
 
A

Alun Harford

coconet said:
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));
 
A

Alun Harford

Jon said:
Assembly source = Assembly.GetExecutingAssembly();

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

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

Alun Harford
 
J

Jon Skeet [C# MVP]

Alun Harford said:
coconet said:
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();
 
A

Alun Harford

Jon said:
Alun Harford said:
coconet said:
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 :)

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

Alun Harford
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top