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 6 1346
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]
- mv*@spam.guard.caspershouse.com
"Michael" <Mi*****@discussions.microsoft.com> wrote in message
news:40**********************************@microsof t.com... 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
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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:O$****************@tk2msftngp13.phx.gbl... 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] - mv*@spam.guard.caspershouse.com
"Michael" <Mi*****@discussions.microsoft.com> wrote in message news:40**********************************@microsof t.com... 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
"Pete Davis" wrote: 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
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 [.NET/C# MVP]" wrote: 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] - mv*@spam.guard.caspershouse.com
"Michael" <Mi*****@discussions.microsoft.com> wrote in message news:40**********************************@microsof t.com... 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" <Mi*****@discussions.microsoft.com> wrote in message
news:92**********************************@microsof t.com... 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 [.NET/C# MVP]" wrote:
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] - mv*@spam.guard.caspershouse.com
"Michael" <Mi*****@discussions.microsoft.com> wrote in message news:40**********************************@microsof t.com... > > 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" wrote: Trying BindingFlags.NonPublic | BindingFlags.Instance
I believe you always have to specify instance and/or static
Pete
"Michael" <Mi*****@discussions.microsoft.com> wrote in message news:92**********************************@microsof t.com... 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 [.NET/C# MVP]" wrote:
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] - mv*@spam.guard.caspershouse.com
"Michael" <Mi*****@discussions.microsoft.com> wrote in message news:40**********************************@microsof t.com... > > 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
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Suzanne Vogel |
last post by:
Hi,
I've been trying to write a function to test whether one class is
derived from another class. I am given only id's of the two classes.
Therefore, direct use of template methods is not an...
|
by: modemer |
last post by:
Question is as in subject.
For example:
class BaseClass {
public:
void func() { do something; } // I don't want this function being
overloaded in its inherited class
};
|
by: Martin Herbert Dietze |
last post by:
Hello,
I would like to implement a callback mechanism in which a
child class registers some methods with particular signatures
which would then be called in a parent class method.
In...
|
by: james |
last post by:
I have a custom UserControl, which can have many sub class levels
derived from it. I want to be able to discover all the components at
Load time, but the only components I can see from the base...
|
by: Peter Oliphant |
last post by:
Is there a way of defining a method in a base class such that derived
classes will call their own version, EVEN if the derived instance is
referred to by a pointer to the base class? Note that the...
|
by: Danny Tuppeny |
last post by:
Hi all,
I've been trying to write some classes, so when I have a parent-child
relationship, such as with Folders in my application, I don't have to
remember to add a parent reference, as well as...
|
by: Bruin |
last post by:
Hi All,
I'm having a problem with MDI child forms when the
reference to the MDI Parent is set in a Control library.
(Sorry for
the long post)
I have an control library assembly which holds all...
|
by: mwebel |
last post by:
Hi,
i have another problem:
i have a container basis class and want to write a virtual "copyFrom()"
function.
Basically the child class should copy from another child class....
|
by: Allan Ebdrup |
last post by:
I would like to use reflection to find all classes that inherit from my
current class, even if they are in another assembly I want to find them if
the current project has a reference to that...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |