如何将泛型类型作为元组的字段? [英] How do I have a generic type as a field of a tuple?
问题描述
我觉得这个问题的答案要么显而易见,要么模糊不清我需要找到更好的方法,但我一直试图在一个元组中包含泛型类型。以下代码 无法编译 ,显示了我想要做的事情:
列表与LT;元组<字符串列表与LT; T>>> listOfTuples = null;
void InitialiseList()
{
listOfTuples = new ...
listOfTuples.Add(new ...);
...
}
我尝试过:
我已经尝试了相当多的谷歌搜索,但到目前为止我发现的所有内容似乎都是关于创建保存元组的泛型类型,而不是保持通用的元组类型。
我当然可以创建一个专用的类或结构来保存列表中的元素,但我想知道是否有更简单的方法。
顺便说一下,我在C#6上。
我认为这就是你在尝试要做的:
元组< string,List< T>> TupleFactory< T>(字符串 值,列出< T>值)
{
return new 元组< string,List< T>>(价值,价值观);
}
使用:
var listOfTuples1 = TupleFactory( test value, new 列表< string>
{
测试值1,
测试值2,
测试值3,
测试值4,
});
var listOfTuples2 = TupleFactory( 测试值, new 列表< int> { 1 , 2 , 3 , 4 });
您可以尝试使用继承但是如果可以使用解决方案1可能更好
公共类MyData< T> :List< Tuple< string,List< T>>>
{
}
MyData< string> dataA = new MyData< string>();
dataA.Add(new Tuple< string,List< string>>(String 1,new List< string> {A,B,C}));
dataA.Add(new Tuple< string,List< string>>(String 2,new List< string> {X,Y,Z}));
MyData< int> dataB = new MyData< int>();
dataB.Add(new Tuple< string,List< int>>(Int 1,new List< int> {1,2,3}));
dataB.Add(new Tuple< string,List< int>>(Int 2,new List< int> {4,5,6}));
我想知道为什么没有人说明显的 - 为什么你为这种情况乱七八糟?他们不是为了复杂的案例而只是创建一个类型来保存你的信息并列出一个......
只是我的2c
I get the feeling the answer to this must either be obvious or so obscure I need to find a better way, but I have been trying to include a generic type in a Tuple. The following code, which does not compile, shows the idea of what I am trying to do:
List<Tuple<string,List<T>>> listOfTuples = null; void InitialiseList() { listOfTuples = new ... listOfTuples.Add( new ... ); ... }
What I have tried:
I've tried a fair bit of Googling, but everything I have found so far seems to be about creating generic types to hold tuples, not tuples to hold generic types.
I could of course just create a dedicated class or struct to hold the elements of the list, but I wondered if there was an easier way.
By the way, I am on C#6.
I think that this is what you are trying to do:
Tuple<string, List<T>> TupleFactory<T>(string value, List<T> values) { return new Tuple<string, List<T>>(value, values); }
To use:
var listOfTuples1 = TupleFactory("test value", new List<string> { "test value 1", "test value 2", "test value 3", "test value 4", }); var listOfTuples2 = TupleFactory("test value", new List<int> { 1, 2, 3, 4 });
You could try using inheritance but Solution 1 is probably better if it can be used
public class MyData<T> : List<Tuple<string, List<T>>> { }
MyData<string> dataA = new MyData<string>(); dataA.Add(new Tuple<string, List<string>>("String 1", new List<string> { "A", "B", "C" })); dataA.Add(new Tuple<string, List<string>>("String 2", new List<string> { "X", "Y", "Z" })); MyData<int> dataB = new MyData<int>(); dataB.Add(new Tuple<string, List<int>>("Int 1", new List<int> { 1, 2, 3 })); dataB.Add(new Tuple<string, List<int>>("Int 2", new List<int> { 4, 5, 6 }));
I'm wondering why no one said the obvious - Why you mess arround with tuples for such cases? They are not for "complicated cases" just create a type to hold your information and make a list of that....
just my 2c
这篇关于如何将泛型类型作为元组的字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!