473,320 Members | 1,848 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

reflection to solve problem

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
1 1651
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: forvino | last post by:
Hi Geeks, Ihave a doubt in dotNet reflection. I m developing a tool which will returns set of public(access specifier) methods of the selected assembly. this works completely fine,...
10
by: Sunny | last post by:
Hi, I have an old problem which I couldn't solve so far. Now I have found a post in that group that gave me an idea, but I can not fully understand it. The problem is: I'm trying to use a...
8
by: paccala | last post by:
Ciao Being google-ing since hours and still I'm not able to solve my problem... I have following hierarchy: class A { public void myMethod(); }
2
by: forvino | last post by:
Hi Geeks, Ihave a doubt in dotNet reflection. I m developing a tool which will returns set of public(access specifier) methods of the selected assembly. this works completely fine,...
2
by: Trapulo | last post by:
I have a reference to an object via a specific interface. I need to read a propery of this object via reflection, and I have only the property name. My problem is that I cannot retrieve a type...
4
by: Per Bolmstedt | last post by:
(This question has been asked previously in this group, but I don't think it was ever really properly answered.) I want to use reflection - preferably - to find all Web Forms in my web site that...
1
by: g4685034 | last post by:
Hello, I develop a program to dump what are in an assembly using Reflection. I try both Assembly.LoadFile and Assembly.ReflectionOnlyLoadFrom. A problem is if a type of some field refers to...
0
by: csharpuser | last post by:
Does anybody know how to disallow the direct invokation of a private method using reflection but still allow the invokation through reflection of a public method which uses the private method. To...
3
by: Tod Birdsall | last post by:
Hi, My boss has asked me to create a tool that can give us a list of stored procedure names that are being called from each of our app's web pages. This is an ASP.NET application that is rather...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.