By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,911 Members | 1,137 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,911 IT Pros & Developers. It's quick & easy.

reflection to solve problem

P: n/a
I currently have a class with 29 data adapters and I will probably add
a few more as time goes on. When I was testing earlier, i had 28 of
them pointing to one connection and 1 pointing to a different one by
mistake. This really caused some confusion for a while. What I would
like to do is use reflection to have the object scan itself and pick
out all of the data adapters and then for each set the connection for
all of the various command objects so they will be the same.

class MyDataManager
{
OleDbDataAdapter _daOne;
OleDbDataAdapter _daTwo;
// and so on
OleDbConnection _dbConn;
private void SetConnections()
{
foreach( OleDbDataAdapter v_da in method_to_get_data_adapters()

)
{
if( v_da.SelectCommand != null )
v_da.SelectCommand.Connection = _dbConn;
// and so on..
}
}
}
could someone please show me how to code the method
method_to_get_data_adapters(). I've tried it and I can't seem to get
it to work right.

gkelly

Aug 8 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
gkelly,

Are you using .NET 2.0? I would use iterators and Generics to solve
this:

static IEnumerable<TGetFieldsOfType<T>(object instance)
{
// Check for null.

// Get the fields of the object.
foreach (FieldInfo field in instance.GetType().GetFields(
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic
| BindingFlags.GetField))
{
// If the field is of type OleDbDataAdapter, then return it,
otherwise, skip.
if (field.FieldType == typeof(T))
{
// Return the field.
yield return (T) field.GetValue(instance);
}
}
}

Then, what you can do is call it like this:

foreach (OleDbDataAdapter v_da in GetFieldsOfType<OleDbDataAdapter>(this))
{
// Do your work here.
}

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<gk********@gmail.comwrote in message
news:11*********************@i3g2000cwc.googlegrou ps.com...
>I currently have a class with 29 data adapters and I will probably add
a few more as time goes on. When I was testing earlier, i had 28 of
them pointing to one connection and 1 pointing to a different one by
mistake. This really caused some confusion for a while. What I would
like to do is use reflection to have the object scan itself and pick
out all of the data adapters and then for each set the connection for
all of the various command objects so they will be the same.

class MyDataManager
{
OleDbDataAdapter _daOne;
OleDbDataAdapter _daTwo;
// and so on
OleDbConnection _dbConn;
private void SetConnections()
{
foreach( OleDbDataAdapter v_da in method_to_get_data_adapters()

)
{
if( v_da.SelectCommand != null )
v_da.SelectCommand.Connection = _dbConn;
// and so on..
}
}
}
could someone please show me how to code the method
method_to_get_data_adapters(). I've tried it and I can't seem to get
it to work right.

gkelly

Aug 8 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.