方法重载仅由通用的限制有所不同 [英] Method overloads which differ only by generic constraint
问题描述
我碰到一个有点问题,我根本无法找到一个很好的解决方法,以
I've run into a bit of a problem, which I simply cannot find a good work-around to.
我想有这3个重载:
public IList<T> GetList<T>(string query) where T: string
public IList<T> GetList<T>(string query) where T: SomeClass
public IList<T> GetList<T>(string query) where T: struct
显然,第一个约束甚至不会编译独自一人,所以这是我的第一个问题。 (我知道我可以只是使它的IList,但我想这三个相同的语法)
Obviously the first constraint won't even compile alone, so that's my first issue. (I realise I could just make it IList but I want the same syntax for the three)
反正这一切的原因,是这些方法都是包装的一部分,围绕执行SQL查询针对数据库 - 我希望能够返回结果作为字符串列表(如果有人选择一个VARCHAR列),值类型的列表(整型,浮点,等等)或类的列表(这些类表示表,因此包含多个列)。
Anyway the reason for all this, is these methods are part of a wrapper around executing SQL queries against a database - I want to be able to return the result as a list of strings (in case someone selects a varchar column), a list of valuetypes (int, float, whatever) or a list of classes (these classes represent tables, and thus contains multiple columns)
我希望部分是有点理解的: - )
I hope that part was somewhat understandable :-)
不管怎样我的大问题是很明显,我不能让这些重载,因为它们使用相同的名称和PARAMETERLIST。
Anyway my big problem is obviously that I cannot make these overloads, since they use the same name and parameterlist.
我也无法将它们合并到同一个方法,因为我需要呼吁SomeClass的一个方法在执行,所以,除非我想要做一些繁重的类型转换,或者更糟糕,反思 - 我需要约束
Also I cannot merge them into the same method, since I need to call a method on SomeClass in that implementation, so unless I want to do some heavy typecasting, or worse, reflection - I need that constraint.
我认识到了我'想要做是不可能的,所以我正在寻找一个好办法,那会模仿我的意图。
I realise that what I'm trying to do isn't possible, so what I'm searching for is a good approach, that'll mimic my intentions.
如果一些这是一个有点目前还不清楚,随便问: - )
If some of this is a bit unclear, feel free to ask :-)
编辑:
下面是我的,其中T当前代码:SomeClass的版本。我试图为字符串/值类型支持添加到这个当前的代码,所以也许我最初的做法是完全错误的 - 任何想法,欢迎基本上是: - )
Here's my current code for the "where T: SomeClass" version. I'm trying to add support for string/valuetypes to this current code, so maybe my initial approach is just plain wrong - any ideas are welcome basically :-)
public IList<TValue> GetList<TValue>(string query) where TValue : DbTable, new()
{
DataSet dataSet = GetDataSet(query);
IList<TValue> result = new List<TValue>();
if (dataSet.Tables.Count > 0)
{
foreach (DataRow row in dataSet.Tables[0].Rows)
{
TValue col = new TValue();
col.Fill(row);
result.Add(col);
}
}
return result;
}
正如你所看到的,我需要DBTABLE的确切类型,以新的适当的构造函数。
填充是DBTABLE(这是一个抽象类)的抽象方法
As you can see I need the exact type of DbTable in order to new the proper constructor. Fill is an abstract method of DbTable (which is an abstract class).
推荐答案
当你注意;有没有这方面的任何好的选择。你可能会考虑不同的名称(而不是重载) - GetStringList
等
As you note; there aren't any good options for this. You might consider different names (rather than overloads) - GetStringList
etc.
不过,我不知道它是否会。简单的删除约束。用单式检查为不完全重型铸造,并可能保存了很多苦。
However, I wonder whether it would be simpler to drop the constraint. A single type-check with "as" isn't exactly "heavy" type-casting, and it might save a lot of pain.
这篇关于方法重载仅由通用的限制有所不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!