问题
Using Subsonic 2.1 I want to make my method call to results look like this: results(searchCriteria)
right now I have to pass the CollectionType as well as the type.
Animal searchCriteria = GetSearchCritera();
AnimalCollection results = results<Animal, AnimalCollection>(searchCriteria);
// I want the call to be results(searchCriteria);
Here is the results method that I want to just take Y
public static T results<Y, T>(Y searchCriteria)
where Y: ReadOnlyRecord<Y>, new()
where T: ReadOnlyList<Y, T>, new()
{
using (IDataReader results = ReadOnlyRecord<Y>.Find(searchCriteria))
{
T a = new T();
a.Load(results);
return a;
}
}
回答1:
I made this class:
public class ConcreteList<T> : ReadOnlyList<T, ConcreteList<T>> where T: ReadOnlyRecord<T>, new()
{
public ConcreteList() { }
}
changed this code:
public static ConcreteList<T> results2<T>(T searchCriteria)
where T : ReadOnlyRecord<T>, new()
{
using (IDataReader results = ReadOnlyRecord<T>.Find(searchCriteria))
{
ConcreteList<T> a = new ConcreteList<T>();
a.Load(results);
return a;
}
}
and I'm able to call it like this:
Animal searchCriteria = GetSearchCritera();
ConcreteList<Animal> results = results2(searchCriteria);
Oh yeah I wanted this to be an extension method:
public static class ReadOnlyRecordExtensions
{
public static ConcreteList<T> ExecuteFind<T>(this T searchCriteria)
where T : ReadOnlyRecord<T>, new()
{
using (IDataReader results = ReadOnlyRecord<T>.Find(searchCriteria))
{
ConcreteList<T> list = new ConcreteList<T>();
list.Load(results);
return list;
}
}
}
来源:https://stackoverflow.com/questions/8154395/in-subsonic-2-1-how-do-i-make-this-generic-call-take-one-generic-parameter