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

looping through a classes fields

P: n/a
Is there a way to loop through a classes fields without first knowing what
fields are there? Trying to write a generic class that could build a query
based on a classes fields and types. This would save me having to write a
method for each class I write to handle its database access and could have a
single function to handle all classes.

Not thinking it can be done but figured I'd ask...

Thanks,

glenn
Nov 16 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
glenn wrote:
Is there a way to loop through a classes fields without first knowing what
fields are there? Trying to write a generic class that could build a query
based on a classes fields and types. This would save me having to write a
method for each class I write to handle its database access and could have a
single function to handle all classes.

Not thinking it can be done but figured I'd ask...

You can, using reflection.
There are a number of tools that will do this (Object Relational Mapping).
It does get very complex if you look at tables that have foreign keys in
them, classes that have objects as properties etc...
I think, the accepted way is to define an attribute on the class and
relevant properties to define the mapping.
HTH

JB
Nov 16 '05 #2

P: n/a
hi
you can use reflection .
FieldInfo gives you the fields of a type
using System;
using System.Reflection;

public class FieldInfoClass
{
public int myField1 = 0;
protected string myField2 = null;
public static void Main()
{
FieldInfo[] myFieldInfo;
Type myType = typeof(FieldInfoClass);
// Get the type and fields of FieldInfoClass.
myFieldInfo = myType.GetFields(BindingFlags.NonPublic |
BindingFlags.Instance
| BindingFlags.Public);
Console.WriteLine("\nThe fields of " +
"FieldInfoClass are \n");
// Display the field information of FieldInfoClass.
for(int i = 0; i < myFieldInfo.Length; i++)
{
Console.WriteLine("\nName : {0}", myFieldInfo[i].Name);
Console.WriteLine("Declaring Type : {0}",
myFieldInfo[i].DeclaringType);
Console.WriteLine("IsPublic : {0}",
myFieldInfo[i].IsPublic);
Console.WriteLine("MemberType : {0}",
myFieldInfo[i].MemberType);
Console.WriteLine("FieldType : {0}",
myFieldInfo[i].FieldType);
Console.WriteLine("IsFamily : {0}",
myFieldInfo[i].IsFamily);
}
}
}

if you want more details about reflection read this article
http://www.dotnetdevs.com/articles/R...mystified.aspx

regards
Ansil
TRivandrum

"glenn" wrote:
Is there a way to loop through a classes fields without first knowing what
fields are there? Trying to write a generic class that could build a query
based on a classes fields and types. This would save me having to write a
method for each class I write to handle its database access and could have a
single function to handle all classes.

Not thinking it can be done but figured I'd ask...

Thanks,

glenn

Nov 16 '05 #3

P: n/a
Check the whole topic of Reflection, it should help you.

If you have the name of the class and the name of the assembly that
contains it, you can load the assembly, and get all the info you may
want about the class.

You'll probably use methods such as GetClassInfo, GetMethodInfo, ...
from the Assembly class (if I remember correctly).

On the other hand, in regard to writing a generic data layer, let me
suggest you a few possible alternatives:
1~ use the MS Data Access Application block from their Application
Blocks (a.k.a. Enterprise libraries:
see http://www.microsoft.com/resources/practices/code.mspx
and
http://msdn.microsoft.com/library/de.../html/daab.asp

2~ or, if like me, you like to re-invent the wheel, try and use
interfaces instead of Reflection. Reflection is slower (I claim, I have
not measured it), and if you use interfaces, you get more safety. The
basic trick is to make your data layer class (that makes SQL queries
for your objects) assume that it will handle classes that implement a
certain interface. The interface defines a few methods that let the
Data layer class get a list of the fields and tables used to persist
objects of the implementing class in a database. That should be all.
Yes, this solution requires that each class you plan to use with your
generic data layer class needs to implement those methods defined in
the interface, but that's the cost we pay for using interfaces :-)

HTH,
F.O.R.

Nov 16 '05 #4

P: n/a
Thanks guys for the quick reply. Looks like I have a little reading to do
but sounds like its very possible to do what I'm trying to do.

Thanks, again...

glenn
"glenn" <gh******@softeksoftware.com> wrote in message
news:u7**************@TK2MSFTNGP14.phx.gbl...
Is there a way to loop through a classes fields without first knowing what
fields are there? Trying to write a generic class that could build a query based on a classes fields and types. This would save me having to write a
method for each class I write to handle its database access and could have a single function to handle all classes.

Not thinking it can be done but figured I'd ask...

Thanks,

glenn

Nov 16 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.