Retrieve CustomAttributes of Child From Base Class

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Scenario: I have a base class that I want to enumerate the custom
attributes of a field defined in the child of the class.

Is this possible? I've tried various versions of BindingFlags on
type.GetFields() but none of them retrun any fields.

Thanks,

Michael
 
Michael,

No, it is not possible. You have to have the type of the child class.
The reason for this is that any class that is not sealed can be extended in
any number of places. It is impossible for the base class to know what
derives from it.

However, if you are trying to do this from within a base class, then you
can call this.GetType, and it will return you the type of the derived class.
Then, you can search for what you are looking for.

Hope this helps.
 
Nicolas,

I may be misunderstanding his question, that seems to be exactly what he
wants to do: From within the base class, call GetType(), which of course
will return the type of the derived class, and then find the field and
enumerate the attributes from it.

Did I misunderstand what he's asking?

Pete

Nicholas Paldino said:
Michael,

No, it is not possible. You have to have the type of the child class.
The reason for this is that any class that is not sealed can be extended
in any number of places. It is impossible for the base class to know what
derives from it.

However, if you are trying to do this from within a base class, then
you can call this.GetType, and it will return you the type of the derived
class. Then, you can search for what you are looking for.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Michael said:
Scenario: I have a base class that I want to enumerate the custom
attributes of a field defined in the child of the class.

Is this possible? I've tried various versions of BindingFlags on
type.GetFields() but none of them retrun any fields.

Thanks,

Michael
 
That is exactly what I am asking and exactly what I was hoping to do. I
just haven't been able to get it to work! :(

Michael
 
That is what I am trying to do..

Type type = this.GetType();
Type TestAttrib = typeof(TestCustomAttribute) ;
FieldInfo[] fieldInfo = type.GetFields( BindingFlags.NonPublic );
// or FieldInfo[] fieldInfo = type.GetFields();
foreach ( FieldInfo info in fieldInfo ) { ... }

fieldInfo is always empty. No matter which BindingFlags I use. What am I
missing?

Michael


Nicholas Paldino said:
Michael,

No, it is not possible. You have to have the type of the child class.
The reason for this is that any class that is not sealed can be extended in
any number of places. It is impossible for the base class to know what
derives from it.

However, if you are trying to do this from within a base class, then you
can call this.GetType, and it will return you the type of the derived class.
Then, you can search for what you are looking for.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Michael said:
Scenario: I have a base class that I want to enumerate the custom
attributes of a field defined in the child of the class.

Is this possible? I've tried various versions of BindingFlags on
type.GetFields() but none of them retrun any fields.

Thanks,

Michael
 
Trying BindingFlags.NonPublic | BindingFlags.Instance

I believe you always have to specify instance and/or static

Pete

Michael said:
That is what I am trying to do..

Type type = this.GetType();
Type TestAttrib = typeof(TestCustomAttribute) ;
FieldInfo[] fieldInfo = type.GetFields( BindingFlags.NonPublic );
// or FieldInfo[] fieldInfo = type.GetFields();
foreach ( FieldInfo info in fieldInfo ) { ... }

fieldInfo is always empty. No matter which BindingFlags I use. What am I
missing?

Michael


Nicholas Paldino said:
Michael,

No, it is not possible. You have to have the type of the child
class.
The reason for this is that any class that is not sealed can be extended
in
any number of places. It is impossible for the base class to know what
derives from it.

However, if you are trying to do this from within a base class, then
you
can call this.GetType, and it will return you the type of the derived
class.
Then, you can search for what you are looking for.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Michael said:
Scenario: I have a base class that I want to enumerate the custom
attributes of a field defined in the child of the class.

Is this possible? I've tried various versions of BindingFlags on
type.GetFields() but none of them retrun any fields.

Thanks,

Michael
 
Ah, that did it. I tried NonPublic and I tried Instance I never tried (
NonPublic | Instance ) which my boolean math doesn't understand why it would
make a difference. Maybe it was the break I took from it! :)

Thanks for your help.

Michael

Pete Davis said:
Trying BindingFlags.NonPublic | BindingFlags.Instance

I believe you always have to specify instance and/or static

Pete

Michael said:
That is what I am trying to do..

Type type = this.GetType();
Type TestAttrib = typeof(TestCustomAttribute) ;
FieldInfo[] fieldInfo = type.GetFields( BindingFlags.NonPublic );
// or FieldInfo[] fieldInfo = type.GetFields();
foreach ( FieldInfo info in fieldInfo ) { ... }

fieldInfo is always empty. No matter which BindingFlags I use. What am I
missing?

Michael


Nicholas Paldino said:
Michael,

No, it is not possible. You have to have the type of the child
class.
The reason for this is that any class that is not sealed can be extended
in
any number of places. It is impossible for the base class to know what
derives from it.

However, if you are trying to do this from within a base class, then
you
can call this.GetType, and it will return you the type of the derived
class.
Then, you can search for what you are looking for.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Scenario: I have a base class that I want to enumerate the custom
attributes of a field defined in the child of the class.

Is this possible? I've tried various versions of BindingFlags on
type.GetFields() but none of them retrun any fields.

Thanks,

Michael
 
Back
Top