|
-
June 1st, 2008, 09:53 PM
#1
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
-
June 2nd, 2008, 01:39 AM
#2
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.
-
June 2nd, 2008, 06:14 AM
#3
Re: Two basic questions about reflection
Thanks cilu,
1.
 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.
 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
-
June 2nd, 2008, 07:01 AM
#4
Re: Two basic questions about reflection
 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.
-
June 4th, 2008, 02:38 AM
#5
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"?
 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|