I'm working with two third-party libraries, and racking my brain over a way to solve this particular issue. I'm implementing an interface which will pass me objects of type "object", and in a good number of the calls, I need to pass them into a generic method that expects a type with class and new() constraints defined.
I know that any object I pass through will meet these constraints, but as far as I can see, there's no simple way I can specify to a generic method that an object satifies these criteria. An interface can't specify constraints on constructors, and an abstract class isn't permitted as a type argument to the generic method.
The objects being passed in in this case are known and controlled by me, but the signature of both Delete methods can't be modified. Ideally I'd be able to just implement an interface guaranteeing the parameterless constructor criteria, but that doesn't seem to be an option.
Here's an example of what I'm talking about:
public void Delete(object toDelete) {
_repo.Delete(toDelete) // signature here is _repo.Delete<T>(T obj) where T : class, new()
}
To give some background and hopefully explain things - the "Delete" call is an implementation of IUpdatable in ADO.NET Data services, while the _repo.Delete call is from SubSonic. I have one DataContextProvider class that will be handling these requests (and other similar ones) for every class that I'm exposing through my model, so a direct cast to a specific class isn't feasible. I can guarantee that the classes always are classes and have a parameterless constructor, but I cannot say from the datacontext that there are only a fixed set of classes that may be passed down - ideally I'd like the DataContext to work with new classes without modification.