jeudi 29 décembre 2016

How do I write a method that fills a property using the property's name?

Here is what I am looking at doing. Say I have a Class with a number of properties. I am trying to write a method that looks something like this

       public static void GetNullableInt32(this DbDataReader reader, Type property)
       {
           property = reader.IsDBNull(reader.GetOrdinal(property.name))
                         ? (int?)null
                         : reader.GetInt32(reader.GetOrdinal(property.name));

       }

Where the property name is pulled from reflection, and the property is set in the method. I put in Type in the parameters as a placeholder for whatever type it needs to be. Then I would call it like

      reader.GetNullableInt32(Class.Property1);

To set all the properties on a class from a database reader.

Is there an easy way to do this? Or is there a better structure where instead of having to enter the column name as a string it is pulled from the column name?





Aucun commentaire:

Enregistrer un commentaire