1

私はこのコードを持っています:

public static Func<IDataReader, T> CreateBinder<T>() {

    NewExpression dataTransferObject = Expression.New(typeof(T).GetConstructor(Type.EmptyTypes)); 
    ParameterExpression dataReader = Expression.Parameter(typeof(IDataReader), "reader");

    IEnumerable<Expression> columnAssignments = typeof(T).GetProperties().Select(property => {
        MethodCallExpression columnData = Expression.Call(dataReader, dataReaderIndexer, new[] { Expression.Constant(property.Name) });
        MethodCallExpression setter = Expression.Call(dataTransferObject, property.SetMethod, new[] { Expression.Convert( columnData, property.PropertyType ) });

        return setter;
    });

    columnAssignments = columnAssignments.Concat(new Expression[] { dataTransferObject });
    BlockExpression assignmentBlock = Expression.Block(columnAssignments);

    Func<IDataReader, T> binder = Expression.Lambda<Func<IDataReader, T>>(assignmentBlock, new[] { dataReader }).Compile();

    return binder;
}

簡単に言えば、データベース行のプロパティを にバインドします<T>。問題は、私が / return を使用したいときdataTransferObject、毎回新しいコピーをインスタンス化していることです。オブジェクトを再作成せずに参照を取得するにはどうすればよいですか?

4

2 に答える 2