C# - IDataReader to Object mapping using generics

后端 未结 10 731
情歌与酒
情歌与酒 2020-12-16 19:07

How can I map a DataReader object into a class object by using generics?

For example I need to do the following:

public class Mapper
    {
          


        
10条回答
  •  死守一世寂寞
    2020-12-16 19:47

    The easiest way I can think of offhand would be to supply a Func delegate for converting each column and constructing your book.

    Alternatively, if you followed some conventions, you could potentially handle this via reflection. For example, if each column mapped to a property in the resulting object using the same name, and you restricted T in your Mapper to providing a constructable T, you could use reflection to set the value of each property to the value in the corresponding column.

提交回复
热议问题