Click to See Complete Forum and Search --> : Two basic questions about reflection


George2
June 1st, 2008, 09:53 PM
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

cilu
June 2nd, 2008, 01:39 AM
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.

George2
June 2nd, 2008, 06:14 AM
Thanks cilu,


1.

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?


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.

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?


//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

cilu
June 2nd, 2008, 07:01 AM
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 (http://msdn.microsoft.com/en-us/default.aspx)? Have you consider searching there for answers, first? You ask SO many questions that could be answered in no time by looking into MSDN. ;)

George2
June 4th, 2008, 02:38 AM
Thanks cilu,


You are correct. I am shame. :blush: :wave:

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"?

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 (http://msdn.microsoft.com/en-us/default.aspx)? 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