looping through a classes fields

  • Thread starter Thread starter glenn
  • Start date Start date
G

glenn

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
 
glenn said:
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
 
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.Name);
Console.WriteLine("Declaring Type : {0}",
myFieldInfo.DeclaringType);
Console.WriteLine("IsPublic : {0}",
myFieldInfo.IsPublic);
Console.WriteLine("MemberType : {0}",
myFieldInfo.MemberType);
Console.WriteLine("FieldType : {0}",
myFieldInfo.FieldType);
Console.WriteLine("IsFamily : {0}",
myFieldInfo.IsFamily);
}
}
}



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



regards
Ansil
TRivandrum
 
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/default.asp?url=/library/en-us/dnpag2/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.
 
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
 
Back
Top