C# 泛型特化

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

转载于:https://my.oschina.net/qaqz111/blog/968477

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值