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".
}
}
}

Re: How to retrieve Custom Attributes from the Type by John

John
Tue Jun 29 14:24:29 CDT 2004

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" <no_spam@dupa.com> wrote in message
news:eO7gP3gXEHA.4032@TK2MSFTNGP11.phx.gbl...
> 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".
> }
> }
> }
>
>



Re: How to retrieve Custom Attributes from the Type by Paul

Paul
Tue Jun 29 15:19:51 CDT 2004

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" <j@ro.com> wrote in message
news:%23eJP86gXEHA.3972@TK2MSFTNGP12.phx.gbl...
> 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" <no_spam@dupa.com> wrote in message
> news:eO7gP3gXEHA.4032@TK2MSFTNGP11.phx.gbl...
>> 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".
>> }
>> }
>> }
>>
>>
>
>



Re: How to retrieve Custom Attributes from the Type by John

John
Tue Jun 29 15:24:55 CDT 2004

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" <no_spam@dupa.com> wrote in message
news:ec4z3ZhXEHA.736@TK2MSFTNGP10.phx.gbl...
> 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" <j@ro.com> wrote in message
> news:%23eJP86gXEHA.3972@TK2MSFTNGP12.phx.gbl...
> > 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" <no_spam@dupa.com> wrote in message
> > news:eO7gP3gXEHA.4032@TK2MSFTNGP11.phx.gbl...
> >> 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".
> >> }
> >> }
> >> }
> >>
> >>
> >
> >
>
>



Re: How to retrieve Custom Attributes from the Type by Paul

Paul
Tue Jun 29 15:40:38 CDT 2004

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

Paul