Dynamic access to class fields

G

garak

Hi,

I got the following problem:

I have a class named PRootObjects with the first FIELD named ID.
I want to use one method (shown below) to access different FIELDs of
different classes.

I have the name of the FIELD I want to access in a STRING Variable
named FIELDNAME (e.g. string FIELDNAME = "ID").
Now I first need to cast the INSOBJ to the correct CLASSTYPE.
Then I want to access the data stored the FIELD named [content of
FIELDNAME (e.g.ID)]

-------------------------------------------------------------------------------------
public void InsertRow (Object InsObj)
{
// This does not work indeed but may show what I want to to !!!
string FieldName = "ID";
Type myType = InsObj.GetType();
InsertQuery += InsObj.FieldName; // The Value of InsObj.Fieldname
should be added to the InsertQuery
}

InsertRow (PRootObjects)
-------------------------------------------------------------------------------------

Can anyone help?

Thanks
Frank
 
J

Jon Skeet [C# MVP]

garak said:
I got the following problem:

I have a class named PRootObjects with the first FIELD named ID.
I want to use one method (shown below) to access different FIELDs of
different classes.

I have the name of the FIELD I want to access in a STRING Variable
named FIELDNAME (e.g. string FIELDNAME = "ID").
Now I first need to cast the INSOBJ to the correct CLASSTYPE.
Then I want to access the data stored the FIELD named [content of
FIELDNAME (e.g.ID)]

Well, you don't need to cast to the right type, as that really only
gives the compiler more information. You need to use reflection to
access the appropriate information - use Type.GetField, for instance,
to give you a FieldInfo, and then FieldInfo.GetValue to get the value.

I would strongly suggest using parameters instead of building up the
query directly though.
 
N

Nicholas Paldino [.NET/C# MVP]

Frank,

You will have to call the GetField method on the Type to get the
FieldInfo instance. Once you have that, you will have to call the GetValue
method, passing the instance to the method to get the value. The value
returned is of type object and will have to be cast to the type of variable
you want to use.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

garak said:
Hi,

I got the following problem:

I have a class named PRootObjects with the first FIELD named ID.
I want to use one method (shown below) to access different FIELDs of
different classes.

I have the name of the FIELD I want to access in a STRING Variable
named FIELDNAME (e.g. string FIELDNAME = "ID").
Now I first need to cast the INSOBJ to the correct CLASSTYPE.
Then I want to access the data stored the FIELD named [content of
FIELDNAME (e.g.ID)]

-------------------------------------------------------------------------- -----------
public void InsertRow (Object InsObj)
{
// This does not work indeed but may show what I want to to !!!
string FieldName = "ID";
Type myType = InsObj.GetType();
InsertQuery += InsObj.FieldName; // The Value of InsObj.Fieldname
should be added to the InsertQuery
}

InsertRow (PRootObjects)
-------------------------------------------------------------------------- -----------

Can anyone help?

Thanks
Frank
 

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