Using Reflection on FieldInfo

R

Ron

I have a windows form that uses several typed datasets which generates the
usual Designer code. I need to reference these objects elsewhere in my
code. I was successful (partially) in using reflection and field info:

System.Reflection.FieldInfo[] fi =
this.GetType().GetFields(System.Reflection.BindingFlags.Instance |
System.Reflection.BindingFlags.NonPublic |
System.Reflection.BindingFlags.Public);

for (int i = 0; i < fi.Length; i++)
{
System.Diagnostics.Debug.WriteLine(string.Format("{0}",
fi.FieldType));
}

This allows me to see each of the objects on the form which is good. My
question specifically is how do I reference those objects to store them in a
collection?

I'm particularily interested in the typed DataSet Objects, the typed
BindingSources, and the typed TableAdapters. The problem is that the
designer code resolves to a type of
Form1.EmployeeDataSetTableAdapters.EmployeeBaseTableAdapter causing a
problem when I want to store them in a collection.

Please help Im really stuck here.

Thank you!
Ron
 
J

Jon Skeet [C# MVP]

Ron said:
I have a windows form that uses several typed datasets which generates the
usual Designer code. I need to reference these objects elsewhere in my
code. I was successful (partially) in using reflection and field info:

System.Reflection.FieldInfo[] fi =
this.GetType().GetFields(System.Reflection.BindingFlags.Instance |
System.Reflection.BindingFlags.NonPublic |
System.Reflection.BindingFlags.Public);

for (int i = 0; i < fi.Length; i++)
{
System.Diagnostics.Debug.WriteLine(string.Format("{0}",
fi.FieldType));
}

This allows me to see each of the objects on the form which is good. My
question specifically is how do I reference those objects to store them in a
collection?


I suspect you're looking for the FieldInfo.GetValue() method.
I'm particularily interested in the typed DataSet Objects, the typed
BindingSources, and the typed TableAdapters. The problem is that the
designer code resolves to a type of
Form1.EmployeeDataSetTableAdapters.EmployeeBaseTableAdapter causing a
problem when I want to store them in a collection.

Why? What problem are you getting?
 
R

Ron

Jon,

Thanks for the reply. The problem Im getting is a want to store the
designer created typed objects in collections, but I cant figure out the
base types so I can setup a Generic List of Bindings and TableAdapters:

List<?>

Thanks for your quick reply...Im off to try your suggestion!
Ron




Jon Skeet said:
Ron said:
I have a windows form that uses several typed datasets which generates
the
usual Designer code. I need to reference these objects elsewhere in my
code. I was successful (partially) in using reflection and field info:

System.Reflection.FieldInfo[] fi =
this.GetType().GetFields(System.Reflection.BindingFlags.Instance |
System.Reflection.BindingFlags.NonPublic |
System.Reflection.BindingFlags.Public);

for (int i = 0; i < fi.Length; i++)
{

System.Diagnostics.Debug.WriteLine(string.Format("{0}",
fi.FieldType));
}

This allows me to see each of the objects on the form which is good. My
question specifically is how do I reference those objects to store them
in a
collection?


I suspect you're looking for the FieldInfo.GetValue() method.
I'm particularily interested in the typed DataSet Objects, the typed
BindingSources, and the typed TableAdapters. The problem is that the
designer code resolves to a type of
Form1.EmployeeDataSetTableAdapters.EmployeeBaseTableAdapter causing a
problem when I want to store them in a collection.

Why? What problem are you getting?
 
J

Jon Skeet [C# MVP]

Ron said:
Thanks for the reply. The problem Im getting is a want to store the
designer created typed objects in collections, but I cant figure out the
base types so I can setup a Generic List of Bindings and TableAdapters:

List<?>

Thanks for your quick reply...Im off to try your suggestion!

If you're only interested in them as a collection of TableAdapters,
just use

List<TableAdapter>

That won't stop you from adding references which refer to more specific
kinds of TableAdapter.
 
R

Ron

Jon,

I tried the .GetValue() but it expects a parameter and I'm not sure what I
need to pass there. Im trying to get a reference to the Designer created
DataSet below:

System.Reflection.FieldInfo[] fi =
this.GetType().GetFields(System.Reflection.BindingFlags.Instance |
System.Reflection.BindingFlags.NonPublic |
System.Reflection.BindingFlags.Public);

for (int i = 0; i < fi.Length; i++)
{
if (fi.Name.ToUpper().Contains("DATASET"))
{
dDataSetCollection.Add(fi.Name,
(DataSet)fi.(???????));
}
}

Thanks!
Ron
 
J

Jon Skeet [C# MVP]

Ron said:
I tried the .GetValue() but it expects a parameter and I'm not sure what I
need to pass there.

You're trying to find the value of a field. If it's a static field, you
can pass in null as the parameter. If it's an instance field, you need
to pass in the instance whose field you want to find the value of.

I *suspect* you just want to pass in "this", given that you're getting
the fields of this.GetType().
 

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