Instance of Variable over InvokeMember()

  • Thread starter Thread starter Frank Dzaebel
  • Start date Start date
F

Frank Dzaebel

Hello,
i want to fill all sqlDataAdapters in a form generically.
I tried sth. with MemberInfo[] from system.reflection.
But in the end i couldnt got the instance of my Variables.

what is the normal way to get the instance of component-
variable "CallByName" like ?

thanks, Frank Dzaebel
 
Hello,
i want to fill all sqlDataAdapters in a form generically.

Here's a quick sample of getting all SqlDataAdapters from an instance of
some class. It's assumed that the data adapters are fields. You can
modify it if they are properties:

class Class1
{
[STAThread]
static void Main(string[] args)
{
SomeClass x = new SomeClass();

FieldInfo[] fields = x.GetType().GetFields
(BindingFlags.Instance|BindingFlags.Public|BindingFlags.NonPublic);
foreach(FieldInfo f in fields)
{
if( f.FieldType == typeof(SqlDataAdapter) )
{
Console.WriteLine(f.Name);
}
}
}
}

class SomeClass
{
public SqlDataAdapter da1 = null;
protected SqlDataAdapter da2 = null;
private SqlDataAdapter da3 = null;
private int x;
private string abc = "123";
}
 
Hello Patrick Steele,

Thanks for the quick answer.
Your sample really retrieves all SqlDataAdapters - ok.
But only the type-properties are accessible.
The problem is that in your line
....
.... Console.WriteLine(f.Name);
....
i have to invoke the member "Fill(..)" like
....
.... f.Fill(dataset11);
....
So I need sth like f.FieldType.InvokeMember("Fill", ...).
But all my try's were accompanied by error messages.

I have to get the real instance of that variable.

It would be great for many developers if someone could
find the solution.

thanks Frank Dzaebel
 
Solution (testet) :

[Purpose]: Fill all SqlDataAdapters of a Windows Form.
Code:
:
private void Fill_all_SqlDataAdapters_with(DataSet
dataSet)
{
SqlDataAdapter da;
BindingFlags bfs = BindingFlags.Instance |
BindingFlags.Public |
BindingFlags.NonPublic | BindingFlags.GetField;
FieldInfo[] fields = this.GetType().GetFields(bfs);
foreach(FieldInfo f in fields)
{
if( f.FieldType == typeof(SqlDataAdapter) )
{
object[] args = new object[]{dataSet11};
da = (SqlDataAdapter)
this.GetType().InvokeMember
(f.Name,bfs,null,this,null);
da.Fill(dataSet11);
}
}
}

private void Form1_Load(object sender, System.EventArgs e)
{
Fill_all_SqlDataAdapters_with(dataSet11);
}

__________________
bye, Frank Dzaebel

[Thanks also to]:
Patrick Steele / Microsoft .NET MVP
http://weblogs.asp.net/psteele
 
Back
Top