Reflection: accessing member of given object

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi, I have trouble finding info on the following:

I have an object myDataAccessObject that contains heaps of SQLDataAdapters
and derivatives thereof. These are auto-generated for me by VS.

I want to be able to call the update-method on all of these (in effect doing
a database-dump) with myDataSet as input (since that's where my data is).

now I *could* put all the adapters into a list and loop through them,
calling the update method, but that's not pretty and I will have to manage
that list as the DB expands so I don't want to do that.

basicly I want to:

-get all sqladapters from myObject
-call "Update" with parameter myDataSet on all of them

that shouldn't be so hard, yet I am stuck and am finding no docs on it.

yours
Andreas Knudsen
 
Andreas,

I haven't tested the following code, but it *should* work.

Type typeToUse = objectToUse.GetType();
foreach(PropertyInfo pi in typeToUse.GetProperties(BindingFlags.NonPublic |
BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public))
{
if (pi.PropertyType == typeof(SqlDataAdapter))
{
SqlDataAdapter da = pi.GetValue(objectToUse, new object[0]) as
SqlDataAdapter;
da.Update(myDataSet);
}
}
 
Thanks for answer ,but:

Jorge L Matos said:
Andreas,

I haven't tested the following code, but it *should* work.

Type typeToUse = objectToUse.GetType();


which object would "objectToUse" be?
would that be the class that contains several sqlDataAdapters?
would that be a null object in which to place the dataadapter once it is
located using reflection?

or do you presume that objectToUse is the adapter that I am looking for?

yours
Andreas
 
"objectToUse" is the object that cotains the SqlDataAdapter objects as
properties.

For example:

class MyDataAccessObject
{

SqlDataAdapter PubsAdapter
{
get {[omitted code]}
set {[omitted code]}
}

SqlDataAdapter NorthWindAdapter
{
get {[omitted code]}
set {[omitted code]}
}
}
 
Back
Top