Jake Rote
Jake Rote

Reputation: 2267

Generic method with having to specify TResult

I have to following method

public DataSetConfiguration<T> PropertyMap<TEntity, TResult>( Expression<Func<TEntity, TResult>> property, Func<DataRow, TResult> mapping )

I was wondering if anyone knows a way so that user does not have to specify TResult and only TEntity or is this not possible thanks in advance

Upvotes: 0

Views: 66

Answers (2)

Lukazoid
Lukazoid

Reputation: 19426

When using your PropertyMap method, you may specify the type of the parameter of your Expression<Func<TEntity, TResult>>, this will mean you do not need to specify the generic types, i.e.

PropertyMap((SomeEntity e) => e.SomeProperty, dr => dr.Field<int>("SomeKey"));

However, this does still require you to return the correct type of result when reading from the DataRow, hence the use of dr.Field<int> (The example assumes the property is an int).

Upvotes: 2

Patrick Hofman
Patrick Hofman

Reputation: 157136

You could use the dynamic keyword, read more here.

The problem is though, that it does not give the full Intellisense, and compilation time checking of your code, which may make thing more difficult to analyze.

public DataSetConfiguration<T> PropertyMap<TEntity>( Expression<Func<TEntity, dynamic>> property, Func<DataRow, dynamic> mapping )

Also T is not specified in your code. Not clear what to do with it.

Upvotes: 0

Related Questions