CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468

    Two basic questions about reflection

    Hello everyone,


    I am new to Reflection. Two basic questions about Reflection after some tests.

    1.

    About BindingFlags.Instance, I think it should refer to all entities which are not static but instance level, but I have made some tests and found it is not correct.

    What does BindingFlags.Instance mean?

    2.

    What is the differences between FieldInfo and FieldType classes? In what situation should we use FieldInfo and in what situation should we use FieldType?


    thanks in advance,
    George

  2. #2
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Two basic questions about reflection

    BindingFlags.Instance specifies that instance members are to be included in the search. What were your findings, exactly?

    FieldInfo is a class that provides information about a field of a .NET type. FieldType is an enumeration in the namespace Microsoft.VisualBasic.FileIO, indicating whether text fields are delimited or fixed width. No connection to the first.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  3. #3
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468

    Re: Two basic questions about reflection

    Thanks cilu,


    1.

    Quote Originally Posted by cilu
    BindingFlags.Instance specifies that instance members are to be included in the search. What were your findings, exactly?
    I think when setting Instance, all non-static fields should be returned. But in my sample code below, nothing is returned. Any ideas?

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Reflection;
    
    namespace TestReflection1
    {
        class Program
        {
            class Foo
            {
                static int abc;
                int bcd;
            }
            
            static void Main(string[] args)
            {
                Foo f = new Foo();
                FieldInfo[] fields = f.GetType().GetFields(BindingFlags.Instance);
    
                return;
            }
        }
    }
    2.

    Quote Originally Posted by cilu
    FieldInfo is a class that provides information about a field of a .NET type. FieldType is an enumeration in the namespace Microsoft.VisualBasic.FileIO, indicating whether text fields are delimited or fixed width. No connection to the first.
    I am also confused when reading code which uses FieldInfo and FieldType together. Actually, here is where I met with this issue,

    http://www.elementalrain.com/?p=66

    It has code like this, you can search that it contains both of the types. Any ideas?

    Code:
     //We get the array of fields for the new type instance.
    
            FieldInfo[] fields = this.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance);
    
     
    
            int i = 0;
    
     
    
            foreach( FieldInfo fi in this.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance))
    
            {
    
                //We query if the fields support the ICloneable interface.
    
                System.Type ICloneType = fi.FieldType.GetInterface(“ICloneable”, true);
    
     
    
                if ((ICloneType != null)&&(fi.GetValue(this) != null))
    
                {
    
                    ICloneable IClone = (ICloneable)fi.GetValue(this);
    
                    fields[i].SetValue(newObject, IClone.Clone());
    
                }
    
                else if (fi.GetValue(this) != null)
    
                    fields[i].SetValue(newObject, fi.GetValue(this));

    regards,
    George

  4. #4
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Two basic questions about reflection

    Quote Originally Posted by george
    I am also confused when reading code which uses FieldInfo and FieldType together. Actually, here is where I met with this issue,

    http://www.elementalrain.com/?p=66

    It has code like this, you can search that it contains both of the types. Any ideas?
    I was sure you were not telling everything. FieldType is not a TYPE. Is a PROPERTY of FieldInfo, and its type is Type!!!

    George, have you heard of MSDN? Have you consider searching there for answers, first? You ask SO many questions that could be answered in no time by looking into MSDN.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  5. #5
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468

    Re: Two basic questions about reflection

    Thanks cilu,


    You are correct. I am shame.

    I did more test cases and find,

    1. It seems useless to use Instance tag alone, we'd better use Instance combined with Public or Non-Public? Any comments?

    2.

    From link,

    http://msdn.microsoft.com/en-us/library/6ztex2dc.aspx

    What means "When overridden in a derived class"?

    Quote Originally Posted by cilu
    I was sure you were not telling everything. FieldType is not a TYPE. Is a PROPERTY of FieldInfo, and its type is Type!!!

    George, have you heard of MSDN? Have you consider searching there for answers, first? You ask SO many questions that could be answered in no time by looking into MSDN.

    regards,
    George

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