How to retrieve Custom Attributes from the Type

P

Paul

Hi!
I have a problem:
I've declared some classes and decorated them with my Custom Attributes.
(say ClassA and ClassB)
Now I have some Type member (in fact this is FieldInfo.FieldType) which is
eq. typeof (ClassB).
And when I call GetCustomAttributes() for this Type I never gets the
attribute, that should be there.
When I call GetType().GetCustonAttributes() everything works fine.
Doeas object of the requested type for attributes must be instantiated ?

Thanks for any help.
Paul.

___ code for my example __

[MyAttribute()]
public class ClassA
{
public int iVal;
}

[MyOtherAttribute()]
public class ClassB
{

ClassA member ;

void foo()
{
foreach (MyAttribute arre in GetType().GetField
("member").GetCustomAttributes (typeof(MyAttribute), true))
{
// never enters here
// this loop never finds MyAttribute of ClassA of field
"member".
}
}
}
 
J

John Wood

The attribute in this case is applied to Class A. You don't have any
attributes applied to "member" and that's why it's not going in there.

You probably want to access the FieldType property of the member, eg.

foreach (MyAttribute arre in GetType().GetField
("member").FieldType.GetCustomAttributes (typeof(MyAttribute), true))
...

Hope that helps.
 
P

Paul

Righ. I ommited by mistake those FieldType property. But this is the case,
when I do not get access to the atribute.
I check now, that when this field is null, GetCustomAttributes() throws
NullReferenceException.
But I do not want to extract attributes from the instance of the object, but
from the type info (System.Type).

Is it possible ? How to do this ?

grx
Paul


John Wood said:
The attribute in this case is applied to Class A. You don't have any
attributes applied to "member" and that's why it's not going in there.

You probably want to access the FieldType property of the member, eg.

foreach (MyAttribute arre in GetType().GetField
("member").FieldType.GetCustomAttributes (typeof(MyAttribute), true))
...

Hope that helps.

--
John Wood
EMail: first name, dot, last name, at priorganize.com

Paul said:
Hi!
I have a problem:
I've declared some classes and decorated them with my Custom Attributes.
(say ClassA and ClassB)
Now I have some Type member (in fact this is FieldInfo.FieldType) which
is
eq. typeof (ClassB).
And when I call GetCustomAttributes() for this Type I never gets the
attribute, that should be there.
When I call GetType().GetCustonAttributes() everything works fine.
Doeas object of the requested type for attributes must be instantiated ?

Thanks for any help.
Paul.

___ code for my example __

[MyAttribute()]
public class ClassA
{
public int iVal;
}

[MyOtherAttribute()]
public class ClassB
{

ClassA member ;

void foo()
{
foreach (MyAttribute arre in GetType().GetField
("member").GetCustomAttributes (typeof(MyAttribute), true))
{
// never enters here
// this loop never finds MyAttribute of ClassA of field
"member".
}
}
}
 
J

John Wood

I can't see why it wouldn't work... as soon as you use GetType() you're
working with the reflected types, not the instances, so that isn't the
problem.

Could you provide a full example, along with the error and stack trace.

Thanks.

--
John Wood
EMail: first name, dot, last name, at priorganize.com

Paul said:
Righ. I ommited by mistake those FieldType property. But this is the case,
when I do not get access to the atribute.
I check now, that when this field is null, GetCustomAttributes() throws
NullReferenceException.
But I do not want to extract attributes from the instance of the object, but
from the type info (System.Type).

Is it possible ? How to do this ?

grx
Paul


John Wood said:
The attribute in this case is applied to Class A. You don't have any
attributes applied to "member" and that's why it's not going in there.

You probably want to access the FieldType property of the member, eg.

foreach (MyAttribute arre in GetType().GetField
("member").FieldType.GetCustomAttributes (typeof(MyAttribute), true))
...

Hope that helps.

--
John Wood
EMail: first name, dot, last name, at priorganize.com

Paul said:
Hi!
I have a problem:
I've declared some classes and decorated them with my Custom Attributes.
(say ClassA and ClassB)
Now I have some Type member (in fact this is FieldInfo.FieldType) which
is
eq. typeof (ClassB).
And when I call GetCustomAttributes() for this Type I never gets the
attribute, that should be there.
When I call GetType().GetCustonAttributes() everything works fine.
Doeas object of the requested type for attributes must be instantiated ?

Thanks for any help.
Paul.

___ code for my example __

[MyAttribute()]
public class ClassA
{
public int iVal;
}

[MyOtherAttribute()]
public class ClassB
{

ClassA member ;

void foo()
{
foreach (MyAttribute arre in GetType().GetField
("member").GetCustomAttributes (typeof(MyAttribute), true))
{
// never enters here
// this loop never finds MyAttribute of ClassA of field
"member".
}
}
}
 
P

Paul

I am so shame ;)
Everything works fine, it was all my mistake within the code.
Sorry for disturbing you, friedns.

Paul
 

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