CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2007
    Posts
    14

    Question 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

  2. #2
    Join Date
    Mar 2007
    Posts
    14

    Exclamation Re: How to know if field returned by Type.GetFields() is from base class?

    I needed FieldInfo.DeclaringType.

    Thank me very much ..

  3. #3
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    Re: How to know if field returned by Type.GetFields() is from base class?

    Quote Originally Posted by RileyDeWiley View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured