Access class fields generically

  • Thread starter Thread starter Tomislav Bartolin
  • Start date Start date
T

Tomislav Bartolin

Hi

I'm trying do create a generic base class for all of my data objects.
One of
the main requirements for this class is to be able to handle all
common db actions without need for implementing them in inherited
classes. To accomplish this I need a way to map values of class
fields to corresponding stored procedure parameters. The only way i
have figured out to do this would be to create a name-value struct
array and hold class fields in it.
My question is: is there a better way to do this? Is there a way to
get a field value by matching its name to a string (in this case it
would be a parameter name)?

Thanks
 
why not use parameter collection object to store parameters. you can write a
wrapper to accept name/value and add that to paratemeter collection.

Av.
 
Tomislav Bartolin said:
I'm trying do create a generic base class for all of my data objects.
One of
the main requirements for this class is to be able to handle all
common db actions without need for implementing them in inherited
classes. To accomplish this I need a way to map values of class
fields to corresponding stored procedure parameters. The only way i
have figured out to do this would be to create a name-value struct
array and hold class fields in it.
My question is: is there a better way to do this? Is there a way to
get a field value by matching its name to a string (in this case it
would be a parameter name)?

Sounds like a job for reflection - look up reflection in the MSDN
index, then "about reflection".
 
in addition to what Jon said, you might also want to look into using custom attributes to provide a level of flexibility. sounds like you are trying to build simple ORM framework

----- Jon Skeet [C# MVP] wrote: ----

Tomislav Bartolin said:
I'm trying do create a generic base class for all of my data objects
One o
the main requirements for this class is to be able to handle al
common db actions without need for implementing them in inherite
classes. To accomplish this I need a way to map values of clas
fields to corresponding stored procedure parameters. The only way
have figured out to do this would be to create a name-value struc
array and hold class fields in it
My question is: is there a better way to do this? Is there a way t
get a field value by matching its name to a string (in this case i
would be a parameter name)

Sounds like a job for reflection - look up reflection in the MSDN
index, then "about reflection"
 
Back
Top