haoxiaobo

从C到C++又到.net, 有一些心得, 和大家交流下...
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

关于中文编程的段子的一个实现

Posted on 2012-02-06 15:03  HAL9000  阅读(5851)  评论(74编辑  收藏  举报

   网上这几天正在疯传一段用C#进行中文编程的段子,说一个程序员就职后,发现公司的大哥里把C#用中文进行了包装,不光是类,还有关键字也进行了中文化,正在苦恼是不是要继续在这个公司干下去。

   这位大哥这种精神是否可嘉还真不好评价。对于没有意义的事情执着追求,是可嘉呢还是不可嘉,估计还是要看评论的人是谁。不过,人家自己的执着,别人也确实无资格评价。

   还有所谓“意义”,恐怕也是因人而定义的。一个东西,对于为之付出了精力的人来说是有意义的,而对于其他人来说,即然与之没有交集,也就无资格置评。对于文中的小哥来说,喜欢的就留下搞搞明白,不喜欢的就走人吧。

 

    只是这段中文化的代码,很有意思,上午试着用C#的lamda实现了一下,就所看到的代码而言,基本算是都实现了,现在我也可以用中文编程了。

  

下面是中文编程的示例,基本与网上那个段子差不多。 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 中文编程
{
    class Program
    {
        static void Main(string[] args)
        {
            // 逻辑判断演示....
            判断.如果是(判断.真).则(() =>
            {
                Console.WriteLine("");
            }).否则(() => 
            {
                Console.WriteLine("");
            });
            // 遍历器演示.....
            登陆信息[] 所有登录信息 = new 登陆信息[100];
            // ....

            遍历器.计数遍历().从(0).到(99).每隔(1).执行((当前索引行) =>
            {
                所有登录信息[当前索引行] = new 登陆信息() { 姓名 = "姓名 " + 当前索引行.ToString() };
            });

            遍历器.枚举遍历<登陆信息>(所有登录信息).从(0).到(99).每隔(3).执行((当前索引行, 登录信息项) =>
            {
                Console.WriteLine(登录信息项);
            });

            数据库连接类 数据连接对象 = null;
            //异常处理........
            异常.对下列语句进行异常检测(() =>
            {
                数据连接对象 = 数据库连接类.连接到("10.1.138.35").用户名为("xxx").密码为("xxx");
                数据连接对象.打开连接();
               
                //... 
                throw new Exception("测试异常");


            })
            .发现异常则((异常对象) =>
            {
                //... 
                Console.WriteLine(异常对象);
            })
            .最终执行(() => {
                // ...
                数据连接对象.关闭连接();

            });
        }
    }

    public class 登陆信息
    {
        public string 姓名;
        public override string ToString()
        {
            return "姓名" + 姓名;
        }
    }
}

 

关键字的包装:-----------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 中文编程
{
    public class 判断
    {
        public const bool 真 = true;
        public const bool 假 = false;

        bool _b;

        public static 判断 如果是(bool 条件)
        {
            return new 判断(){_b = 条件};
        }

        public 判断 则(Action act)
        {
            if (_b)
            {
                act();
            }
            return this;
        }

        public 判断 否则(Action act)
        {
            if (!_b)
            {
                act();
            }
            return this;
        }
    }

    public class 遍历器
    {
        public static 枚举遍历器<T> 枚举遍历<T>(IEnumerable<T> 枚举集合)
        {
            return new 枚举遍历器<T>(枚举集合);
        }

        public static 计数遍历器 计数遍历()
        {
            return new 计数遍历器() { };
        }
    }

    public class 枚举遍历器<T>
    {
        protected IEnumerable<T> _set;
        protected int _iStartIndex;
        protected int _iEndIndex;
        protected int _Step;

        public 枚举遍历器(IEnumerable<T> 枚举集合)
        {
            this._set = 枚举集合;
        }

        public 枚举遍历器<T> 从(int 开始元素序号)
        {
            this._iStartIndex = 开始元素序号;
            return this;
        }

        public 枚举遍历器<T> 到(int 结束元素序号)
        {
            this._iEndIndex = 结束元素序号;
            return this;
        }

        public 枚举遍历器<T> 每隔(int 每隔步长)
        {
            this._Step = 每隔步长;
            return this;
        }

        public void 执行(Action<int, T> 循环体方法)
        {
            int i = 0;
            foreach (var e in _set)
            {
                if (i >= this._iStartIndex && i <= this._iEndIndex)
                {
                    if ((i - this._iStartIndex) % this._Step == 0)
                    {
                        循环体方法(i, e);
                    }
                }
                i++;

            }
        }
    }

    
    public class 计数遍历器
    {
        protected int _iStartIndex;
        protected int _iEndIndex;
        protected int _Step;
    
        public 计数遍历器 从(int 开始元素序号)
        {
            this._iStartIndex = 开始元素序号;
            return this;
        }

        public 计数遍历器 到(int 结束元素序号)
        {
            this._iEndIndex = 结束元素序号;
            return this;
        }

        public 计数遍历器 每隔(int 每隔步长)
        {
            this._Step = 每隔步长;
            return this;
        }

        public void 执行(Action<int> 循环体方法)
        {
            for (int i = this._iStartIndex; i <= this._iEndIndex; i += this._Step)
            {
                循环体方法(i);
            }
        }
    }

    public class 异常
    {
        Exception _ex = null;

        public static 异常 对下列语句进行异常检测(Action 正常执行程序)
        {
            try
            {
                正常执行程序();
                return new 异常() { _ex = null};
            }
            catch (Exception ex)
            {
                return new 异常() { _ex = ex};
            }
            
        }

        public 异常 发现异常则(Action<Exception> 异常处理程序)
        {
            if (this._ex != null)
            {
                异常处理程序(this._ex);
            }
            return this;
        }

        public 异常 最终执行(Action 最终处理程序)
        {
            最终处理程序();
            return this;
        }
    }
}

 

数据库连接的包装: 

 

using System;

 

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace 中文编程
{
    public class 数据库连接类
    {
        private string _sServer;
        private string _sUID;
        private string _sPassword;
        private string _sDBName;
        SqlConnection _sqlconn = null;

        public static 数据库连接类 连接到(string 服务器名)
        {
            return new 数据库连接类() { _sServer = 服务器名 };
        }

        public 数据库连接类 用户名为(string 用户名)
        {
            _sUID = 用户名;
            return this;
        }

        public 数据库连接类 密码为(string 密码)
        {
            _sPassword = 密码;
            return this;
        }

        public 数据库连接类 数据库为(string 数据库名)
        {
            _sDBName = 数据库名;
            return this;
        }

        public void 打开连接()
        {
            this._sqlconn = new SqlConnection(string.Format("Data Source={0};Initial Catalog={1};User ID={2};Password={3}"this._sServer, this._sDBName, this._sUID, this._sPassword));
            this._sqlconn.Open();
        }

        public void 关闭连接()
        {
            this._sqlconn.Close();
        }

    }
}

 

说实话,感觉很奇怪。