在Subsonic 2.1中,我如何使这个泛型调用采用一个通用参数? [英] In Subsonic 2.1 how do I make this generic call take one generic parameter?
本文介绍了在Subsonic 2.1中,我如何使这个泛型调用采用一个通用参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用Subsonic 2.1我想让我的方法调用结果如下所示: results(searchCriteria)
现在我必须传递CollectionType以及类型。
动物searchCriteria = GetSearchCritera();
AnimalCollection结果=结果< Animal,AnimalCollection>(searchCriteria);
//我希望调用是结果(searchCriteria);
以下是我想要的结果方法Y
$ (Y searchCriteria)
其中Y:ReadOnlyRecord< Y>,new()
其中T: ReadOnlyList< Y,T> ;, new()
{
using(IDataReader results = ReadOnlyRecord< Y> Find(searchCriteria))
{
T a = new T() ;
a.Load(results);
返回a;
}
}
解决方案
I创建这个类:
public class ConcreteList< T> :ReadOnlyList< T,ConcreteList< T>>其中T:ReadOnlyRecord< T> ;, new()
{
public ConcreteList(){}
}
更改了以下代码:
public static ConcreteList< T> (T)搜索标准)
其中T:ReadOnlyRecord< b {
ConcreteList< T> a = new ConcreteList< T>();
a.Load(results);
返回a;
}
}
我可以这样调用它:
动物searchCriteria = GetSearchCritera();
ConcreteList< Animal> results = results2(searchCriteria);
哦,我希望这是一个扩展方法:
public static class ReadOnlyRecordExtensions
{
public static ConcreteList< T> ExecuteFind< T>(this T searchCriteria)
where T:ReadOnlyRecord< T> ;,新()
{
using(IDataReader results = ReadOnlyRecord< T> .Find(searchCriteria))
{
ConcreteList< T> list = new ConcreteList< T>();
list.Load(results);
返回列表;
}
}
}
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;
}
}
解决方案
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;
}
}
}
这篇关于在Subsonic 2.1中,我如何使这个泛型调用采用一个通用参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文