Why is my GetFields(BindingFlags.Instance | BindingFlags.Public) not working? (Sample working code i

S

sloan

My code in see the NonPublic members, but not the Public ones.



Full sample code below.

FieldInfo[] publicFieldInfos = t.GetFields(BindingFlags.Instance |
BindingFlags.Public);

is returning nothing.

Note, I'm trying to get at the properties on the abstract class as well as
the 1 concrete class.

(And read the attributes as well).



I'm going bonkers on this one....the msdn example works with the 2 flags
(BindingFlags.Instance | BindingFlags.Public).....but my mini inheritance
example below is not.

THANKS in advance.









private void RunTest1()

{

try

{

textBox1.Text = string.Empty;

Type t = typeof(MyInheritedClass);


//Look at the BindingFlags *** NonPublic ***

int fieldCount = 0;

while (null != t)

{

fieldCount += t.GetFields(BindingFlags.Instance |
BindingFlags.NonPublic).Length;

FieldInfo[] nonPublicFieldInfos = t.GetFields(BindingFlags.Instance |
BindingFlags.NonPublic);

foreach (FieldInfo field in nonPublicFieldInfos)

{

if (null != field)

{

Console.WriteLine(field.Name);



}

}

t = t.BaseType;

}



Console.WriteLine("\n\r------------------\n\r");





//Look at the BindingFlags *** Public ***

t = typeof(MyInheritedClass);

FieldInfo[] publicFieldInfos = t.GetFields(BindingFlags.Instance |
BindingFlags.Public);

foreach (FieldInfo field in publicFieldInfos)

{

if (null != field)

{

Console.WriteLine(field.Name);



object[] attributes = field.GetCustomAttributes(t, true);

if (attributes != null && attributes.Length > 0)

{

foreach (Attribute att in attributes)

{

Console.WriteLine(att.GetType().Name);



}

}

}

}

}

catch (Exception ex)

{

ReportException(ex);

}

}

private void ReportException(Exception ex)

{



Exception innerException = ex;

while (innerException != null)

{

Console.WriteLine(innerException.Message + System.Environment.NewLine +
innerException.StackTrace + System.Environment.NewLine +
System.Environment.NewLine);



innerException = innerException.InnerException;

}

}


public abstract class MySuperType

{

public MySuperType(string st)

{

this.STString = st;

}

public string STString

{

get;

set;

}

public abstract string MyAbstractString {get;set;}

}

public class MyInheritedClass : MySuperType

{

public MyInheritedClass(string ic)

: base(ic)

{

this.ICString = ic;

}

[Description("This is an important property"),Category("HowImportant")]

public string ICString

{

get;

set;

}


private string _oldSchoolPropertyString = string.Empty;

public string OldSchoolPropertyString

{

get { return _oldSchoolPropertyString; }

set { _oldSchoolPropertyString = value; }

}


[Description("This is a not so importarnt property"),
Category("HowImportant")]

public override string MyAbstractString

{

get; set;

}


}
 
A

Adam Clauss

On 6/11/2010 8:52 AM, sloan wrote
FieldInfo[] publicFieldInfos = t.GetFields(BindingFlags.Instance |
BindingFlags.Public);

is returning nothing.
public abstract class MySuperType

{

public string STString

{

get;

set;

}

public abstract string MyAbstractString {get;set;}

}

You are confusing fields and properties. You are retrieving public
fields, but your class only has public properties.

-Adam
 
S

sloan

I forgot these newsgroups are being abandoned.

The answer is "Use .GetProperties" instead.

Someone at stackoverflow answered it for me.



sloan said:
My code in see the NonPublic members, but not the Public ones.



Full sample code below.

FieldInfo[] publicFieldInfos = t.GetFields(BindingFlags.Instance |
BindingFlags.Public);

is returning nothing.

Note, I'm trying to get at the properties on the abstract class as well as
the 1 concrete class.

(And read the attributes as well).



I'm going bonkers on this one....the msdn example works with the 2 flags
(BindingFlags.Instance | BindingFlags.Public).....but my mini inheritance
example below is not.

THANKS in advance.









private void RunTest1()

{

try

{

textBox1.Text = string.Empty;

Type t = typeof(MyInheritedClass);


//Look at the BindingFlags *** NonPublic ***

int fieldCount = 0;

while (null != t)

{

fieldCount += t.GetFields(BindingFlags.Instance |
BindingFlags.NonPublic).Length;

FieldInfo[] nonPublicFieldInfos = t.GetFields(BindingFlags.Instance |
BindingFlags.NonPublic);

foreach (FieldInfo field in nonPublicFieldInfos)

{

if (null != field)

{

Console.WriteLine(field.Name);



}

}

t = t.BaseType;

}



Console.WriteLine("\n\r------------------\n\r");





//Look at the BindingFlags *** Public ***

t = typeof(MyInheritedClass);

FieldInfo[] publicFieldInfos = t.GetFields(BindingFlags.Instance |
BindingFlags.Public);

foreach (FieldInfo field in publicFieldInfos)

{

if (null != field)

{

Console.WriteLine(field.Name);



object[] attributes = field.GetCustomAttributes(t, true);

if (attributes != null && attributes.Length > 0)

{

foreach (Attribute att in attributes)

{

Console.WriteLine(att.GetType().Name);



}

}

}

}

}

catch (Exception ex)

{

ReportException(ex);

}

}

private void ReportException(Exception ex)

{



Exception innerException = ex;

while (innerException != null)

{

Console.WriteLine(innerException.Message + System.Environment.NewLine +
innerException.StackTrace + System.Environment.NewLine +
System.Environment.NewLine);



innerException = innerException.InnerException;

}

}


public abstract class MySuperType

{

public MySuperType(string st)

{

this.STString = st;

}

public string STString

{

get;

set;

}

public abstract string MyAbstractString {get;set;}

}

public class MyInheritedClass : MySuperType

{

public MyInheritedClass(string ic)

: base(ic)

{

this.ICString = ic;

}

[Description("This is an important property"),Category("HowImportant")]

public string ICString

{

get;

set;

}


private string _oldSchoolPropertyString = string.Empty;

public string OldSchoolPropertyString

{

get { return _oldSchoolPropertyString; }

set { _oldSchoolPropertyString = value; }

}


[Description("This is a not so importarnt property"),
Category("HowImportant")]

public override string MyAbstractString

{

get; set;

}


}
 
A

Arne Vajhøj

I forgot these newsgroups are being abandoned.

The answer is "Use .GetProperties" instead.

Someone at stackoverflow answered it for me.

Well - you got an answer here too, so ....

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top