Get field info of base class with reflection

R

Rotsey

Hi,

I have a class that exist in my UI assembly that inherits from a class
in a referenced assembly like this.

class EmployeesDC
{
private string mstrEmployeeNumber;

etc
}

class EmployeesBL : EmployeesDC
{
}

Then in UI Assembly

class Employees : EmployeesBL
{
}

Can someone give me the code so I can get info on the private fields
in EmployeesDC using reflection.

I have googled but I am still not clear how??

thanks
rotsey
 
T

Tom Spink

Rotsey said:
Hi,

I have a class that exist in my UI assembly that inherits from a class
in a referenced assembly like this.

class EmployeesDC
{
private string mstrEmployeeNumber;

etc
}

class EmployeesBL : EmployeesDC
{
}

Then in UI Assembly

class Employees : EmployeesBL
{
}

Can someone give me the code so I can get info on the private fields
in EmployeesDC using reflection.

I have googled but I am still not clear how??

thanks
rotsey

Hi rotsey,

It's not too complicated, but may I ask what type of information you're
after? If you're after the contents of the field, then is there a reason
why you can't declare it as protected?

If you're after something else, let us know and we'll point you in the right
direction.
 
R

Rotsey

thanks Tom

All I want is to get the name of all the private fields.

I will then know from the prefix to do something else

rotsey
 
T

Tom Spink

Rotsey said:
thanks Tom

All I want is to get the name of all the private fields.

I will then know from the prefix to do something else

rotsey

Hi Rotsey,

Cool. Take a look at the following example:

///
using System;
using System.Reflection;

public class A
{
private string pfa_A;
private string pfb_A;
private string pfc_A;
}

public class B : A
{
private string pfa_B;
private string pfb_B;
private string pfc_B;
}

public class C : B
{
public void Foo ()
{
Type t = this.GetType();
BindingFlags bf = BindingFlags.Default;

bf |= BindingFlags.NonPublic;
bf |= BindingFlags.FlattenHierarchy;
bf |= BindingFlags.Instance;

foreach (FieldInfo field in t.GetFields(bf))
{
Console.WriteLine(field);
}
}
}

public class E
{
public static void Main ()
{
C c = new C();
c.Foo();
}
}
///

What's going on here is most interesting in the Foo() method of C. The
inheritance hierarchy is C -> B -> A, and when you call Foo() in C, it will
print out a list of fields it finds.

Modifying bf (the BindingFlags parameter) will help to refine/expand the
search.

The field variable (on each iteration of the foreach statement) will (most
likely) contain everything you need to know about the field.
 
R

Rotsey

Thank Tom I get it.

One thing though, how would you refactor the code so that
I dont't have to recode foo in all my objects like class C.

rotsey.


"Tom Sp
 
T

Tom Spink

Rotsey said:
Thank Tom I get it.

One thing though, how would you refactor the code so that
I dont't have to recode foo in all my objects like class C.

rotsey.


"Tom Sp

Hi Rotsey,

There are a couple of approaches to this, the most simple being a static
utility class that exposes a method, taking in the type of the object,
rather than calling GetType in the method.
 
R

Rotsey

Ok thats what I am doing.

So when I pass the "this" to the static method
do I use "Object" as type

ie
GetFieldtype(this,"EmployeeNumber");

GetFieldtype(Object obj,string fieldname)
{

}

Thats what puzzles me?
 
R

Rotsey

Tom I have tried using just Foo in each object
but I am not getting any fields returned

Any ideas???


Rotsey said:
Ok thats what I am doing.

So when I pass the "this" to the static method
do I use "Object" as type

ie
GetFieldtype(this,"EmployeeNumber");

GetFieldtype(Object obj,string fieldname)
{

}

Thats what puzzles me?
 
R

Rotsey

you gave up on me Tom.

I had to reference the base class directly to get the private fields.

Don't know why your method doesn't work?



Rotsey said:
Tom I have tried using just Foo in each object
but I am not getting any fields returned

Any ideas???
 
T

Tom Spink

Rotsey said:
you gave up on me Tom.

I had to reference the base class directly to get the private fields.

Don't know why your method doesn't work?
you gave up on me Tom.

I may be a student, but I have a job ;-)
I had to reference the base class directly to get the private fields.

Don't know why your method doesn't work?

Can you post the code you were using?
 
R

Rotsey

OK thanks for your input.

This is my whoe class in the UI assembly

public class Employees : EmployeesBL, ILoadableData, ILookUpObject

{

public Employees()

{

ConnectString = TBRSettings.Instance.Settings.ConnectionString;

}

public Employees(string connString)

{

ConnectString = connString;

}

public DataSet GetRowFromID(int ID)

{

return GetEmployee(ID);

}

public string GetFieldtype(string fieldname)

{

Type t = this.GetType();

BindingFlags bf = BindingFlags.Default;

bf |= BindingFlags.NonPublic;

bf |= BindingFlags.FlattenHierarchy;

bf |= BindingFlags.Instance;

foreach (FieldInfo field in t..GetFields(bf))

{

if (field.Name.EndsWith(fieldname))

return field.Name.Substring(0, 4);

}

return "";

}

}
 

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