• 设计一个Enum Class


    最近项目中需要将一个枚举类型设计成为一个对应的类,设计结果如下:

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

    namespace CompanyName.ProductName.Model.Agency
    {
        
    /// <summary>
        
    /// 区域,Design as an Enum Class
        
    /// </summary>
        [Serializable]
        
    public class LocationInfo
        { 
            
    /// <summary>
            
    /// 区域:本地
            
    /// </summary>
            public static readonly LocationInfo Local = new LocationInfo("本地");

            
    /// <summary>
            
    /// 区域:外埠
            
    /// </summary>
            public static readonly LocationInfo Other = new LocationInfo("外埠");

            
    /// <summary>
            
    /// 私有区域名称字段
            
    /// </summary>
            private string locationTypeName;

            
    /// <summary>
            
    /// Initializes a new instance of the <see cref="LocationInfo"/> class.
            
    /// 私有构造函数保证Enum值域的封闭性
            
    /// </summary>
            
    /// <param name="typeName">Name of the type.</param>
            private LocationInfo(string typeName)
            {
                
    this.locationTypeName = typeName;
            }

            
    /// <summary>
            
    /// 根据一个符合指定格式的字符串返回类型实例。
            
    /// </summary>
            
    /// <param name="typeName">Name of the type.</param>
            
    /// <returns>LocationInfo</returns>
            public static LocationInfo Parse(string typeName)
            {
                
    switch (typeName.Trim())
                {
                    
    case "Local"
                    
    case "本地":
                        
    return Local;
                    
    case "Other":
                    
    case "外埠":
                        
    return Other;
                    
    default:
                        
    return Other;
                }
            }

            
    /// <summary>
            
    /// 根据一个符合指定格式的整数返回类型实例
            
    /// </summary>
            
    /// <param name="typeId">The type id.</param>
            
    /// <returns>LocationInfo</returns>
            public static LocationInfo Parse(int typeId)
            {
                
    switch (typeId)
                {
                    
    case 0:
                        
    return Local;
                    
    case 1:
                        
    return Other;
                    
    default:
                        
    return Other;
                }
            }

            
    /// <summary>
            
    /// 将Enum Class 转换成存入数据库中的整数值
            
    /// </summary>
            
    /// <returns>ToDBValue</returns>
            public int ToDBValue()
            {
                
    return (this.locationTypeName == "本地"? 0 : 1;
            }

            
    /// <summary>
            
    /// 提供重载的"=="操作符,使用locationTypeName来判断是否是相同的LocationInfo类型 
            
    /// </summary>
            
    /// <param name="li1">The li1.</param>
            
    /// <param name="li2">The li2.</param>
            
    /// <returns>The result of the operator.</returns>
            public static bool operator ==(LocationInfo li1, LocationInfo li2)
            {
                
    if (Object.Equals(li1, null))
                {
                    
    return Object.Equals(li2, null);
                }

                
    return li1.Equals(li2);
            }

            
    /// <summary>
            
    /// Implements the operator !=.
            
    /// </summary>
            
    /// <param name="li1">The li1.</param>
            
    /// <param name="li2">The li2.</param>
            
    /// <returns>The result of the operator.</returns>
            public static bool operator !=(LocationInfo li1, LocationInfo li2)
            {
                
    return !(li1 == li2);
            }

            
    /// <summary>
            
    /// Determines whether the specified <see cref="System.Object"/> is equal to this instance.
            
    /// </summary>
            
    /// <param name="obj">The <see cref="System.Object"/> to compare with this instance.</param>
            
    /// <returns>
            
    ///     <c>true</c> if the specified <see cref="System.Object"/> is equal to this instance; otherwise, <c>false</c>.
            
    /// </returns>
            
    /// <exception cref="T:System.NullReferenceException">
            
    /// The <paramref name="obj"/> parameter is null.
            
    /// </exception>
            public override bool Equals(object obj)
            {
                LocationInfo li 
    = obj as LocationInfo;
                
    if (obj == null)
                {
                    
    return false;
                }

                
    return this.locationTypeName == li.locationTypeName;
            }

            
    /// <summary>
            
    /// Returns a hash code for this instance.
            
    /// </summary>
            
    /// <returns>
            
    /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. 
            
    /// </returns>
            public override int GetHashCode()
            {
                
    //return this.locationTypeName.GetHashCode();
                return this.ToDBValue();
            }

            
    /// <summary>
            
    /// Returns a <see cref="System.String"/> that represents this instance.
            
    /// </summary>
            
    /// <returns>
            
    /// A <see cref="System.String"/> that represents this instance.
            
    /// </returns>
            public override string ToString()
            {
                
    return this.locationTypeName;
            }
        }
    }
  • 相关阅读:
    Unity5 GI与PBS渲染从用法到着色代码
    Unity场景渲染相关实现的猜想
    Ogre2.1 Hlms与渲染流程
    Ogre2.1 灯光与阴影
    Ogre2.1 结合OpenGL3+高效渲染
    Ogre2.0 全新功能打造新3D引擎
    Ogre 编辑器三(自动生成与更新Ogre对象编辑界面)
    Ogre 编辑器二(用Ogre的地形组件加载天龙八部地形)
    一个简单的旋转控制器与固定屏幕位置
    sql 触发器里,发生错误,回滚提示错误语句
  • 原文地址:https://www.cnblogs.com/jiaruistone/p/1621101.html
Copyright © 2020-2023  润新知