attributes

  • Thread starter Thread starter csharpula csharp
  • Start date Start date
C

csharpula csharp

Hello,
I am putting attribute on some of the classes I work with and on some I
don't. How can I iterate over collection of such objects and check on
which of them there is attribute?

Thank you!
 
Hello,
I am putting attribute on some of the classes I work with and on some I
don't. How can I iterate over collection of such objects and check on
which of them there is attribute?

Thank you!

*** Sent via Developersdexhttp://www.developersdex.com***

I think the following should help.

Assembly a;//load your assembly
a.GetTypes(); // iterate for each type t
Type t;
t.GetCustomAttributes();iterate and check if your type
 
I think the following should help.

Assembly a;//load your assembly
a.GetTypes(); // iterate for each type t
Type t;
t.GetCustomAttributes();iterate and check if your type

oops..
Foreach object in your collection
Type t= object.gettype();
t.GetCustomAttributes();iterate
 
parez said:
oops..
Foreach object in your collection
Type t= object.gettype();
t.GetCustomAttributes();iterate

If all you want to know is wether the attribute is there or not, use
IsDefined instead of GetCustomAttributes, it's a cheaper operation.
 
How can I use IsDefined,which parameter to pass?

foreach (Iobj obj in objList)
{
Type t = obj.GetType();

if (t.IsDefind (????,true)
}

what can I put insead of ???? if I know the attrbute name I am looking
ofr?

Thanks!
 
How can I use IsDefined,which parameter to pass?

foreach (Iobj obj in objList)
{
Type t = obj.GetType();

if (t.IsDefind (????,true)

}

what can I put insead of ???? if I know the attrbute name I am looking
ofr?

if (t.IsDefined(typeof(MyAttribute), true)

There's an example in MSDN.

Jon
 

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

Back
Top