您不能像其他人所说的那样,但您几乎可以做到。
所以所有Tuple<...> 类都有这样的签名:
public class Tuple<T1, ...> :
IStructuralEquatable,
IStructuralComparable,
IComparable,
ITuple
除了ITuple 之外的所有这些接口都是公共的(ITuple 是一个内部接口),所以你可以尝试制作类似这样的东西:
public interface ITupleKey<TKey>
where TKey : IStructuralEquatable, IStructuralComparable, IComparable
{
}
“但是等等!”,你说,“我怎么能确定没有其他人正在实现这些接口?”
嗯,你不能。但就像我说的,这只是一种几乎的方式——幸运的是,IStructuralEquatable 和 IStructuralComparable 仅(自然地在框架级别)用于以下类型:
System.Array
System.Tuple<T1>
System.Tuple<T1,T2>
System.Tuple<T1,T2,T3>
System.Tuple<T1,T2,T3,T4>
System.Tuple<T1,T2,T3,T4,T5>
System.Tuple<T1,T2,T3,T4,T5,T6>
System.Tuple<T1,T2,T3,T4,T5,T6,T7>
System.Tuple<T1,T2,T3,T4,T5,T6,T7,TRest>
所以它非常接近。将此与运行时检查相结合,以确保 TKey 实际上是 Tuple 的某个变体,您可能拥有所需的内容。
编辑:
一些基本用法:
public class Class1<TKey>
where TKey : IStructuralEquatable, IStructuralComparable, IComparable
{
}
// will compile
var classTup1 = new Class1<Tuple<int>>();
var classTup2 = new Class1<Tuple<int,int>>();
var classTup3 = new Class1<Tuple<int,int,int>>();
var classTup4 = new Class1<Tuple<int,int,int,int>>();
var classTup5 = new Class1<Tuple<int,int,int,int,int>>();
// won't compile
var badclassTup1 = new Class1<int>();
var badclassTup2 = new Class1<string>();
var badclassTup3 = new Class1<object>();
而且,因为我显然已经疯了,让我们看看这里有什么可能:
public class Class1<TKey>
where TKey : IStructuralEquatable, IStructuralComparable, IComparable
{
public Class1(TKey key)
{
Key = key;
TupleRank = typeof(TKey).GetGenericArguments().Count();
TupleSubtypes = typeof(TKey).GetGenericArguments();
Console.WriteLine("Key type is a Tuple (I think) with {0} elements", TupleRank);
TupleGetters =
Enumerable.Range(1, TupleRank)
.Select(i => typeof(TKey).GetProperty(string.Concat("Item",i.ToString())))
.Select(pi => pi.GetGetMethod())
.Select(getter => Delegate.CreateDelegate(
typeof(Func<>).MakeGenericType(getter.ReturnType),
this.Key,
getter))
.ToList();
}
public int TupleRank {get; private set;}
public IEnumerable<Type> TupleSubtypes {get; private set;}
public IList<Delegate> TupleGetters {get; private set;}
public TKey Key {get; private set;}
public object this[int rank]
{
get { return TupleGetters[rank].DynamicInvoke(null);}
}
public void DoSomethingUseful()
{
for(int i=0; i<TupleRank; i++)
{
Console.WriteLine("Key value for {0}:{1}", string.Concat("Item", i+1), this[i]);
}
}
}
测试台:
var classTup1 = new Class1<Tuple<int>>(Tuple.Create(1));
var classTup2 = new Class1<Tuple<int,int>>(Tuple.Create(1,2));
var classTup3 = new Class1<Tuple<int,int,int>>(Tuple.Create(1,2,3));
var classTup4 = new Class1<Tuple<int,int,int,int>>(Tuple.Create(1,2,3,4));
var classTup5 = new Class1<Tuple<int,int,int,int,int>>(Tuple.Create(1,2,3,4,5));
classTup1.DoSomethingUseful();
classTup2.DoSomethingUseful();
classTup3.DoSomethingUseful();
classTup4.DoSomethingUseful();
classTup5.DoSomethingUseful();
输出:
Key type is a Tuple (I think) with 1 elements
Key type is a Tuple (I think) with 2 elements
Key type is a Tuple (I think) with 3 elements
Key type is a Tuple (I think) with 4 elements
Key type is a Tuple (I think) with 5 elements
Key value for Item1:1
Key value for Item1:1
Key value for Item2:2
Key value for Item1:1
Key value for Item2:2
Key value for Item3:3
Key value for Item1:1
Key value for Item2:2
Key value for Item3:3
Key value for Item4:4
Key value for Item1:1
Key value for Item2:2
Key value for Item3:3
Key value for Item4:4
Key value for Item5:5