1. 程式人生 > >C# 泛型特化

C# 泛型特化

但是 data load max fim not str wrap lock

C# 泛型不是 C++ 的模板類,並不支持特化和偏特化,但是使用一些技巧可以在一定程度上達到相同的目的。

原文是 po 在 stackoverflow 上的一個回答:A: Generic indexer overload specialization

一、泛型方法的特化

使用一個非泛型 helper 類和一個內嵌的泛型類可以實現對泛型方法的特化。

 1     internal static class IndexerImpl //non-generic static helper class
 2     {
 3         private static T IndexerDefaultImpl<T>(int
i) => default(T); //default implementation 4 5 private static T IndexerImpl2<T>(int i) => default(T); //another implementation for short/int/long 6 7 private static string IndexerForString(int i) => (i * i).ToString(); //specialization for T=string 8 private static
DateTime IndexerForDateTime(int i) => new DateTime(i * i * i); //specialization for T=DateTime 9 10 static IndexerImpl() //install the specializations 11 { 12 Specializer<string>.Fun = IndexerForString; 13 Specializer<DateTime>.Fun = IndexerForDateTime;
14 15 Specializer<short>.Fun = IndexerImpl2<short>; 16 Specializer<int>.Fun = IndexerImpl2<int>; 17 Specializer<long>.Fun = IndexerImpl2<long>; 18 } 19 20 internal static class Specializer<T> //specialization dispatcher 21 { 22 internal static Func<int, T> Fun; 23 internal static T Call(int i) 24 => null != Fun 25 ? Fun(i) 26 : IndexerDefaultImpl<T>(i); 27 } 28 } 29 30 public class YourClass<T> 31 { 32 public T this[int i] => IndexerImpl.Specializer<T>.Call(i); 33 }

如果需要傳入實例對返回結果進行計算,可以增加一個參數:

 1     internal static class IndexerImpl //non-generic static helper class
 2     {
 3         private static T IndexerDefaultImpl<T>(int i, YourClass<T> yourClass) => default(T); //default implementation
 4 
 5         private static T IndexerImpl2<T>(int i, YourClass<T> yourClass) => default(T); //another implementation for short/int/long
 6 
 7         private static string IndexerForString<T>(int i, YourClass<T> yourClass) => (i * i).ToString(); //specialization for T=string
 8         private static DateTime IndexerForDateTime<T>(int i, YourClass<T> yourClass) => new DateTime(i * i * i); //specialization for T=DateTime
 9 
10         static IndexerImpl() //install the specializations
11         {
12             Specializer<string>.Fun = IndexerForString;
13             Specializer<DateTime>.Fun = IndexerForDateTime;
14 
15             Specializer<short>.Fun = IndexerImpl2;
16             Specializer<int>.Fun = IndexerImpl2;
17             Specializer<long>.Fun = IndexerImpl2;
18         }
19 
20         internal static class Specializer<T> //specialization dispatcher
21         {
22             internal static Func<int, YourClass<T>, T> Fun;
23             internal static T Call(int i, YourClass<T> yourClass)
24                 => null != Fun
25                     ? Fun(i, yourClass)
26                     : IndexerDefaultImpl(i, yourClass);
27         }
28     }
29 
30     public class YourClass<T>
31     {
32         public T this[int i] => IndexerImpl.Specializer<T>.Call(i, this);
33     }

二、泛型方法的偏特化

偏特化也是差不多的做法,只不過幫助類變成了以不需要特化的類型構成的泛型類:

 1     internal static class GetValueImpl<R, S>
 2     {
 3         private static T DefImpl<T>(R r, S s) => default(T);
 4         private static int IntRet(R r, S s) => int.MaxValue;
 5 
 6         internal static class Specializer<T>
 7         {
 8             internal static Func<R, S, T> Fun;
 9             internal static T Call(R r, S s) => null != Fun ? Fun(r, s) : DefImpl<T>(r, s);
10         }
11 
12         static GetValueImpl()
13         {
14             Specializer<int>.Fun = IntRet;
15         }
16     }
17 
18     public class TestClass
19     {
20         public T GetValue<R, S, T>(R r, S s) => GetValueImpl<R, S>.Specializer<T>.Call(r, s);
21     }

以上代碼片段中,被偏特化的是 GetValue 方法中的 T 類型參數,當 T=int 的時候,實際被調用的方法就是 GetValueImpl.IntRet 方法,其他情況是 GetValueImpl.DefImpl 方法。

三、泛型類的特化

泛型類的特化沒有什麽好的方法,只能采用繼承特化類型泛型類的方式間接實現,並且將要特化處理的成員采用虛方法或者用 new 隱藏基類方法。

偏特化泛型類也可以采用差不多的方式實現。

具體做法可以參考 stackoverflow 上的這個答案:A: C# specialize generic class

C# 泛型特化