文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>浅谈c#泛型类型变量作为操作数使用的通用解决方法

浅谈c#泛型类型变量作为操作数使用的通用解决方法

时间:2010-11-08  来源:Jeff Wong

        internal static Int32 Sum(int num)
        {
            Int32 sum = 0;
            for (Int32 i = 0; i < num; i++)
            {
                sum += i;
            }
            return sum;
        }

同理,如果我们需要返回类型和输入类型为其他基元类型,如Decimal,Int64等等,正常情况下我们都会修改上述代码中返回和输入参数类型,比如可以这样对Int64进行求和:

        internal static Int64 Sum(Int64 num)
        {
            Int64 sum = 0;
            for (Int64 i = 0; i < num; i++)
            {
                sum += i;
            }
            return sum;
        }

但是这样有多少种不同的基元数字类型是不是就要写多少个这样的方法(这里不考虑某些数字型基元类型之间的相互转换),造成方法膨胀,代码臃肿?大家分析上面的代码,发现参数个数,运算格式非常类似,毫无疑问会想到抽象出一个公共的泛型方法来解决数字型基元类型的运算问题。

在<<CLR Via C#>>一书中,Jeffrey Richter就举例并“千方百计”的想实现下面的泛型方法来实现泛型类型变量作为操作数的通用解决方案:

internal static class UsingGenericTypeVariablesAsOperands
{
    private static T Sum<T>(T num) where T : struct
    {
        T sum = default(T);
        for (T n = default(T); n < num; n++)
            sum += n;
        return sum;
    }
}

但是,编译器无情地给出了如下错误报告:

错误    9    运算符“++”无法应用于“T”类型的操作数   

错误    10    运算符“+=”无法应用于“T”和“T”类型的操作数   

错误    8    运算符“<”无法应用于“T”和“T”类型的操作数  

Jeffrey Richter在书中希望MS在CLR和编译器未来的版本中解决这个问题,也就是说通过泛型对当前的这个问题还无解。书中还说,解决这个方法可能需要通过性能较差的反射或者是操作符重载,我自己没有实现过,网上google未果,自己动手尝试了一种最笨的方法:

internal static class UsingGenericTypeVariablesAsOperands
{
    private static T Sum<T>(T num) where T : struct
    {
        T sum = default(T);
        Type type = num.GetType();
        switch (type.Name)
        {
            default:
                break;

            case "Int32":
                Int32 result = 0;
                for (Int32 i = 0; i < (Int32)(object)num; i++)
                {
                    result += i;
                }
                sum = (T)(Object)result;
                break;
            /*......还有其他类型不一一列举......*/
        }
        return sum;
    }
}

估计肯定不是大牛口中的反射实现方法,不知道大家有没有好的替代方案。老实说,上面的代码看着真他大爷的,虽然看起来很简单,可是实现起来真是费力不讨好,还是不用为好。

 

附:关于sealed接口方法在派生类中的实现

下面的代码摘录自<<CLR via C#>>。

internal static class InterfaceReimplementation
{
    public static void Main()
    {
        /************************* First Example *************************/
        Base b = new Base();

        // Call's Dispose by using b's type: "Base's Dispose"
        b.Dispose();

        // Call's Dispose by using b's object's type: "Base's Dispose"
        ((IDisposable)b).Dispose();


        /************************* Second Example ************************/
        Derived d = new Derived();

        // Call's Dispose by using d's type: "Derived's Dispose"
        d.Dispose();

        // Call's Dispose by using d's object's type: "Derived's Dispose"
        ((IDisposable)d).Dispose();


        /************************* Third Example *************************/
        b = new Derived();

        // Call's Dispose by using b's type: "Base's Dispose"
        b.Dispose();

        // Call's Dispose by using b's object's type: "Derived's Dispose"
        ((IDisposable)b).Dispose();
    }

    // This class is derived from Object and it implements IDisposable
    internal class Base : IDisposable
    {
        // This method is implicitly sealed and cannot be overridden
        public void Dispose()
        {
            Console.WriteLine("Base's Dispose");
        }
    }

    // This class is derived from Base and it re-implements IDisposable
    internal class Derived : Base, IDisposable
    {
        // This method cannot override Base's Dispose. 'new' is used to indicate 
        // that this method re-implements IDisposable's Dispose method
        new public void Dispose()
        {
            Console.WriteLine("Derived's Dispose");

            // NOTE: The next line shows how to call a base class's implementation (if desired)
            // base.Dispose();
        }
    }
}

解释:如果一个接口方法是sealed的,派生类不能重写它,在上述代码中,Base类的Dispose方法是隐式密封的,不能被派生类Derived 重写。那么Derived 类如何实现IDisposable接口的Dispose方法呢?原来派生类可以重新继承同一个接口,并且可为该接口的方法提供它自己的实现。在一个对象上调用一个接口的方法时,将调用该方法在该对象的类型中的实现。

相关阅读 更多 +
排行榜 更多 +
云漫

云漫

生活实用 下载
证照签

证照签

生活实用 下载
云漫文学

云漫文学

生活实用 下载