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. 解决spring-boot启动中碰到的问题:Cannot determine embedded database driver class for database type NONE(转)

    问题 如下: 2017-07-16 08:50:57.436  INFO 13524 --- [           main] c.p.p.web.PointshopWebApplication   ...

  2. Uva 10081 Tight words (概率DP)

    Time limit: 3.000 seconds Given is an alphabet {0, 1, ... , k}, 0 <= k <= 9 . We say that a wo ...

  3. [Python] Python list slice syntax fun

    # Python's list slice syntax can be used without indices # for a few fun and useful things: # You ca ...

  4. cgroups

    CGROUPS官方解析,用户空间怎样监控 http://blog.chinaunix.net/uid-16763274-id-2103750.html cgroups概念 fr=aladdin&quo ...

  5. Deep Networks : Overview

    Overview In the previous sections, you constructed a 3-layer neural network comprising an input, hid ...

  6. Web开发中,使用表格来展示每个角色对应的权限

    通过表格这种方式,来展示角色和权限之间的关系,挺好的.还有很多场景,都可以用这种方式. 角色权限表 权限 系统管理员 文章管理员 相册管理员 留言管理员 个人信息管理 查看个人信息 √ √ √ √ 编 ...

  7. Input/output subsystem having an integrated advanced programmable interrupt controller for use in a personal computer

    A computer system is described having one or more host processors, a host chipset and an input/outpu ...

  8. RecyclerView下拉刷新和载入很多其它

    之前一直写的是ListVIew下拉刷新,可是好多朋友都说要RecycleView的下拉刷新和滑动载入.事实上,这个原理都是几乎相同.抽出时间,我就写了下RecycleView的下拉刷新和滑动载入很多其 ...

  9. iOS 基于第三方QQ授权登录

    基于iOS实现APP的第三方QQ登陆.接入第三方SDK时的一个主要的步骤: 1,找到相关的开放平台.QQ互联平台,http://connect.qq.com/: 2,注冊成功后创建自己的APP.填写一 ...

  10. 互联网+时代IT管理者的转型

    最近,大众创业万众创新的热潮真是一浪接着一浪,它实际上是一次政府和企事业的自我改革,利用互联网+的思维与技术对生产模式.流通模式与运营模式进行全新的变革,商业的本质是没有变的,仅仅是穿了个马甲来表演. ...