2

I have an interface, which is use by repositories) that with this member:

T FindById<T, TId>(TId id)
    where T : class, IEntity<TId>
    where TId : IEquatable<TId>;

This allows the caller to specify an entity type (T) and the type of it's Id field (TId). The implementor of this interface would then find the entities of type T and use the id parameter to filter them according to their id (which is defined on IEntity<TId>).

Currently I'm calling it like this:

int id = 123;
var myApproval = PartsDC.FindById<Approval, int>(id);

Ideally I'd like to do this:

int id = 123;
var myApproval = PartsDC.FindById<Approval>(id);

I figure the 2nd generic parameter should be able to be inferred by the fact that id is an int (so that's what my 2nd generic parameter is).

Is it possible to get anything like this, where I don't have to specify the type of the id field in my class?

George Duckett
  • 31,770
  • 9
  • 95
  • 162

0 Answers0