在Subsonic 2.1中,如何使这个通用调用采用一个通用参数?

时间:2011-11-16 15:44:03

标签: generics c#-3.0 subsonic

使用Subsonic 2.1我想让我的方法调用结果看起来像这样:results(searchCriteria)我现在必须传递CollectionType和类型。

Animal searchCriteria = GetSearchCritera();
AnimalCollection results = results<Animal, AnimalCollection>(searchCriteria);
// I want the call to be results(searchCriteria);

这是我想要采用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 个答案:

答案 0 :(得分:1)

我上了这堂课:

    public class ConcreteList<T> : ReadOnlyList<T, ConcreteList<T>> where T: ReadOnlyRecord<T>, new()
    {
        public ConcreteList() { }
    }

更改了此代码:

    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;
        }
    }

我可以这样称呼它:

    Animal searchCriteria = GetSearchCritera();
    ConcreteList<Animal> results = results2(searchCriteria);

哦,是的,我希望这是一个扩展方法:

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;
        }
    }
}