In Subsonic 2.1 how do I make this generic call take one generic parameter?

不问归期 提交于 2020-01-16 19:19:12

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!