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

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

一、泛型方法的特化

使用一个非泛型 helper 类和一个内嵌的泛型类可以实现对泛型方法的特化。

     internal static class IndexerImpl //non-generic static helper class
{
private static T IndexerDefaultImpl<T>(int i) => default(T); //default implementation private static T IndexerImpl2<T>(int i) => default(T); //another implementation for short/int/long private static string IndexerForString(int i) => (i * i).ToString(); //specialization for T=string
private static DateTime IndexerForDateTime(int i) => new DateTime(i * i * i); //specialization for T=DateTime static IndexerImpl() //install the specializations
{
Specializer<string>.Fun = IndexerForString;
Specializer<DateTime>.Fun = IndexerForDateTime; Specializer<short>.Fun = IndexerImpl2<short>;
Specializer<int>.Fun = IndexerImpl2<int>;
Specializer<long>.Fun = IndexerImpl2<long>;
} internal static class Specializer<T> //specialization dispatcher
{
internal static Func<int, T> Fun;
internal static T Call(int i)
=> null != Fun
? Fun(i)
: IndexerDefaultImpl<T>(i);
}
} public class YourClass<T>
{
public T this[int i] => IndexerImpl.Specializer<T>.Call(i);
}

如果需要传入实例对返回结果进行计算,可以增加一个参数:

     internal static class IndexerImpl //non-generic static helper class
{
private static T IndexerDefaultImpl<T>(int i, YourClass<T> yourClass) => default(T); //default implementation private static T IndexerImpl2<T>(int i, YourClass<T> yourClass) => default(T); //another implementation for short/int/long private static string IndexerForString<T>(int i, YourClass<T> yourClass) => (i * i).ToString(); //specialization for T=string
private static DateTime IndexerForDateTime<T>(int i, YourClass<T> yourClass) => new DateTime(i * i * i); //specialization for T=DateTime static IndexerImpl() //install the specializations
{
Specializer<string>.Fun = IndexerForString;
Specializer<DateTime>.Fun = IndexerForDateTime; Specializer<short>.Fun = IndexerImpl2;
Specializer<int>.Fun = IndexerImpl2;
Specializer<long>.Fun = IndexerImpl2;
} internal static class Specializer<T> //specialization dispatcher
{
internal static Func<int, YourClass<T>, T> Fun;
internal static T Call(int i, YourClass<T> yourClass)
=> null != Fun
? Fun(i, yourClass)
: IndexerDefaultImpl(i, yourClass);
}
} public class YourClass<T>
{
public T this[int i] => IndexerImpl.Specializer<T>.Call(i, this);
}

二、泛型方法的偏特化

偏特化也是差不多的做法,只不过帮助类变成了以不需要特化的类型构成的泛型类:

     internal static class GetValueImpl<R, S>
{
private static T DefImpl<T>(R r, S s) => default(T);
private static int IntRet(R r, S s) => int.MaxValue; internal static class Specializer<T>
{
internal static Func<R, S, T> Fun;
internal static T Call(R r, S s) => null != Fun ? Fun(r, s) : DefImpl<T>(r, s);
} static GetValueImpl()
{
Specializer<int>.Fun = IntRet;
}
} public class TestClass
{
public T GetValue<R, S, T>(R r, S s) => GetValueImpl<R, S>.Specializer<T>.Call(r, s);
}

以上代码片段中,被偏特化的是 GetValue 方法中的 T 类型参数,当 T=int 的时候,实际被调用的方法就是 GetValueImpl.IntRet 方法,其他情况是 GetValueImpl.DefImpl 方法。

三、泛型类的特化

泛型类的特化没有什么好的方法,只能采用继承特化类型泛型类的方式间接实现,并且将要特化处理的成员采用虚方法或者用 new 隐藏基类方法。

偏特化泛型类也可以采用差不多的方式实现。

具体做法可以参考 stackoverflow 上的这个答案:A: C# specialize generic class

C# 泛型特化的更多相关文章

  1. Java增强的泛型

    尽管Java 8是2014年年初才发布的,而Java 9要等到2016年年中,但是目前有一些计划放到某个未来版本(希望是Java 10)中的特性已经合并了进来. 具体而言,有两个比较大的特性已经开始原 ...

  2. Uber使用Swift重写APP的踩坑经历及解决方案(转载)

    本文出自Uber移动架构和框架组负责人托马斯·阿特曼于2016年在湾区Swift峰会上的演讲,分享了使用Swfit重写Uber的好与坏.以下为译文: 我是托马斯·阿特曼,目前是Uber移动架构和框架组 ...

  3. 【基本功】深入剖析Swift性能优化

    简介 2014年,苹果公司在WWDC上发布Swift这一新的编程语言.经过几年的发展,Swift已经成为iOS开发语言的“中流砥柱”,Swift提供了非常灵活的高级别特性,例如协议.闭包.泛型等,并且 ...

  4. 深入剖析Swift性能优化

    简介 2014年,苹果公司在WWDC上发布Swift这一新的编程语言.经过几年的发展,Swift已经成为iOS开发语言的“中流砥柱”,Swift提供了非常灵活的高级别特性,例如协议.闭包.泛型等,并且 ...

  5. C#泛型-模板特化

    class TClass<T, K> { } class SubTClass<T, C, K> : TClass<Char, K> { } class SubTCl ...

  6. STL迭代器之一:偏特化

    在stl的算法中运用容器的迭代器时,很可能经常会用到迭代器相应型别(例如迭代器所指物的型别),假设算法中有必要声明一个变量,以"迭代器所指对象的型别"为类型,如何是好,例如我们写一 ...

  7. template_12特化与重载

    1,重载函数模板f<int*>((int*)pi);//1f<int>((int*)pi);//2上面用int*替换第一个模板的T,用int来替换第二个模板的T.那么将得到两个 ...

  8. [转]Traits 编程技法+模板偏特化+template参数推导+内嵌型别编程技巧

    STL中,traits编程技法得到了很大的应用,了解这个,才能一窥STL奥妙所在. 先将自己所理解的记录如下: Traits技术可以用来获得一个 类型 的相关信息的. 首先假如有以下一个泛型的迭代器类 ...

  9. C++ Primer 学习笔记_84_模板与泛型编程 --模板特化

    模板与泛型编程 --模板特化 引言: 我们并不总是能够写出对全部可能被实例化的类型都最合适的模板.某些情况下,通用模板定义对于某个类型可能是全然错误的,通用模板定义或许不能编译或者做错误的事情;另外一 ...

随机推荐

  1. Python3爬虫之爬取某一路径的所有html文件

    要离线下载易百教程网站中的所有关于Python的教程,需要将Python教程的首页作为种子url:http://www.yiibai.com/python/,然后按照广度优先(广度优先,使用队列:深度 ...

  2. C#获得文件夹下文件名

    String path = @"X:\xxx\xxx"; //第一种方法 var files = Directory.GetFiles(path, "*.txt" ...

  3. OpenCascade Sweep Algorithm

    OpenCascade Sweep Algorithm eryar@163.com Abstract. Sweeps are the objects you obtain by sweeping a ...

  4. elasticsearch index 之 create index(二)

    创建索引需要创建索引并且更新集群index matedata,这一过程在MetaDataCreateIndexService的createIndex方法中完成.这里会提交一个高优先级,AckedClu ...

  5. POJ 2437 贪心+priority_queue

    题意: 思路: 贪心 能不覆盖的就不盖 写得很乱 左闭右开的 temp //By SiriusRen #include <queue> #include <cstdio> #i ...

  6. OpenCV —— 图像局部与分割(二)

    分水岭算法 将图像中的边缘转化成“山脉”,将均匀区域转化为“山谷” 分水岭算法首先计算灰度图像的梯度,这对山谷或没有纹理的盆地(亮度值低的点)的形成有效,也对山头或图像中没有主导线段的山脉(山脊对应的 ...

  7. [log4j]Slf4j的包冲突

    Caused by: java.lang.NoClassDefFoundError: Could not initialize class org.apache.log4j.Log4jLoggerFa ...

  8. Python 3 与"Hello World!"

    Python 3 版本 Python的3.0版本,常被称为Python 3000,或简称Py3k.相对于Python的早期版本,这是一个较大的升级.为了不带入过多的累赘,Python 3.0在设计的时 ...

  9. 洛谷 P3505 [POI2010]TEL-Teleportation

    P3505 [POI2010]TEL-Teleportation 题目描述 King Byteasar is the ruler of the whole solar system that cont ...

  10. Matlab piecelin

    function v = piecelin(x,y,u) %PIECELIN Piecewise linear interpolation. % v = piecelin(x,y,u) finds t ...