• 给系统类(如本例中的Type类)添加扩展方法的写法: 南京酷得软件


    本例中:columnInfo.DisplayMemberDataType是一个Type类型的变量,而系统的Type类型本身并没有IsNumericType()这个方法。
    那么如果创建这么一个方法而使我们根据现有需求通过对系统类的扩展来方便程序的开发呢?
    很简单,我们可以建立一个静态类,如:

     1 Public static class TypeExtensions
     2 {
     3  Public static bool IsNumericeType(this Type type)
     4 {
     5 return GetNumericTypeKind(type) != 0;
     6 }
     7      Public static int GetNumericTypeKind(this Type type)
     8         {
     9             type = GetNonNullableType(type);
    10             if (type.IsEnum) return 0;
    11             switch (Type.GetTypeCode(type))
    12             {
    13                 case TypeCode.Char:
    14                 case TypeCode.Single:
    15                 case TypeCode.Double:
    16                 case TypeCode.Decimal:
    17                     return 1;
    18                 case TypeCode.SByte:
    19                 case TypeCode.Int16:
    20                 case TypeCode.Int32:
    21                 case TypeCode.Int64:
    22                     return 2;
    23                 case TypeCode.Byte:
    24                 case TypeCode.UInt16:
    25                 case TypeCode.UInt32:
    26                 case TypeCode.UInt64:
    27                     return 3;
    28                 default:
    29                     return 0;
    30             }
    31         }
    32 }

     南京酷得软件

    公司网站: http://www.codersoft.cn 专业开发: 气象软件、监狱网上购物系统、两法衔接平台
  • 相关阅读:
    在Magento产品分类页面创建推荐产品
    任意两个时间之间的星期几的次数纵.sql
    SQL 日期格式化处理.sql
    系统应用程序域
    在ubuntu12.04中,apc_cache_find()
    buildertheory.cs
    复杂年月处理.sql
    CurrentAccounts.cs
    CLR和Windows加载器
    应用程序域
  • 原文地址:https://www.cnblogs.com/sucsy/p/2243894.html
Copyright © 2020-2023  润新知