-
December 16th, 2010, 11:06 PM
#1
How to know if field returned by Type.GetFields() is from base class?
I have some code that uses one base class and about 100 (yes, really!) derived classes.
I wish to put into the base class some code that will serialize/deserialize any derived class. To do this I use reflection and get fields and values one at a time.
The problem I am having is that I am getting the fields for my derived class and my base class all at once.
For instance:
public class BaseClass
{
public int base_var;
}
public class A : BaseClass
{
public string a_string;
}
....
A instance_of_A= new A();
Type type = instance_of_A.GetType();
FieldInfo [] fi = type.GetFields();
foreach(FieldInfo f in fi)
{
... here I see both base_var and a_string...
... I want to be able to filter out the stuff in my base class and just deal with a_string etc.
...Is there a flag in FieldInfo that gives me this information?
}
If I want *only* the base stuff I can use a BindingFlag for a filter. But that is not what I want.
Eric
-
December 17th, 2010, 09:51 PM
#2
Re: How to know if field returned by Type.GetFields() is from base class?
I needed FieldInfo.DeclaringType.
Thank me very much ..
-
December 18th, 2010, 01:59 AM
#3
Re: How to know if field returned by Type.GetFields() is from base class?
Originally Posted by RileyDeWiley
If I want *only* the base stuff I can use a BindingFlag for a filter. But that is not what I want.
I wanted to suggest you exacly that but now I realize I don't actually understand what you are trying to do. can you explain it in another way?
win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming
remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation
private lessons are not an option so please don't ask for help in private, I won't replay
if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|