C#中的各種各樣的索引器

iDotNetSpace發表於2009-01-04
 對上一篇的擴充套件,索引器不但可以以字串為索引引數還以各種資料型別做為索引引數,例如Type,委託,下面是對上一篇文章中的擴充套件,由於類中只有一個bool型別的變數,所以可以這樣使用索引

Parameter parameter = new Parameter();

parameter[typeof(bool)] = ture;

bool b = parameter[typeof(bool)];

還可以這樣呼叫函式:

parameter[typeof(CalcWithDotType),"Get_Hf"](ExtractDotType.FiveDot);

  #region Custom's ValueType and Delegate
    ///


    /// 體溫型別
    ///

    public enum BodyTemperatureType
    {LowTemperature,NormalTemperature,HighTemperature,type};
    ///
    /// 抽點型別
    ///

    public enum ExtractDotType{FiveDot,NineDot,type};
    #region Custom's Delegates
    public delegate double CalcWithDotType(ExtractDotType edType);
    public delegate double CalcWithTempType(BodyTemperatureType btType);
    public delegate double CalcWithDouble(double type);
    public delegate double CalcWithNone();
    #endregion
    #endregion
    #region Parameter Class
    public class Parameter
    {
        #region Fields
        private double Hf1;
        private double Hf2;
        private double Hf3;
        private double Hf4;
        private double Hf5;
        private double Hf6;
        private double Hf7;
        private double Hf8;
        private double Hf9;

        private double Ts1;
        private double Ts2;
        private double Ts3;
        private double Ts4;
        private double Ts5;
        private double Ts6;
        private double Ts7;
        private double Ts8;
        private double Ts9;

        private double Tr;

        private double Tcl1;
        private double Tcl2;
        private double Tcl3;
        private double Tcl4;
        private double Tcl5;
        private double Tcl6;
        private double Tcl7;
        private double Tcl8;
        private double Tcl9;

        private double Icl;
        private double _Ts;
        private double _Tcl;
        private double _Hf;
        private double s;
        private double _Tb;
        private double _s;

        private BodyTemperatureType _btType = default(BodyTemperatureType);
        private ExtractDotType _edType = default(ExtractDotType);
        private  double _weight;//體重
        private  double _area;//體表面積

        private double _delta_Tb;
        private  bool  _Calculates_s = false;
        #endregion
        #region Members
        ///


        /// 加權平均熱流
        ///

        ///
        ///
       

        public double Get_Hf(ExtractDotType edType)
        {
            if (edType == ExtractDotType.FiveDot)
            {
                _Hf = 0.07 * Hf1 + 0.50 * Hf2 + 0.05 * Hf6 + 0.18 * Hf7 + 0.20 * Hf8;
            }
            else if (edType == ExtractDotType.NineDot)
            {
                _Hf = 0.07 * Hf1 + 0.18 * Hf2 + 0.18 * Hf3 + 0.07 * Hf4 + 0.07 * Hf5 + 0.05 * Hf6 + 0.19 * Hf7 + 0.13 * Hf8 + 0.06 * Hf9;
            }
            else
                _Hf =  default(double);
            return _Hf;

        }
        ///


        /// 加權平均皮膚溫度
        ///

        ///
        ///
        public double Get_Ts(ExtractDotType edType)
        {
            if (edType == ExtractDotType.FiveDot)
            {
                _Ts =  (0.07 * Ts1 + 0.50 * Ts2 + 0.05 * Ts6 + 0.18 * Ts7 + 0.20 * Ts8);
            }
            else if (edType == ExtractDotType.NineDot)
            {
                _Ts =  (0.07 * Ts1 + 0.18 * Ts2 + 0.18 * Ts3 + 0.07 * Ts4 + 0.07 * Ts5 + 0.05 * Ts6 + 0.19 * Ts7 + 0.13 * Ts8 + 0.06 * Ts9);
            }
            else
                _Ts =  default(double);
            return _Ts;

        }
        ///


        /// 加權平均請外表溫度
        ///

        ///
        ///
        public double Get_Tcl(ExtractDotType edType)
        {
            if (edType == ExtractDotType.FiveDot)
            {
               _Tcl =  (0.07 * Tcl1 + 0.50 * Tcl2 + 0.05 * Tcl6 + 0.18 * Tcl7 + 0.20 * Tcl8);
            }
            else if (edType == ExtractDotType.NineDot)
            {
                _Tcl =  (0.07 * Tcl1 + 0.18 * Tcl2 + 0.18 * Tcl3 + 0.07 * Tcl4 + 0.07 * Tcl5 + 0.05 * Tcl6 + 0.19 * Tcl7 + 0.13 * Tcl8 + 0.06 * Tcl9);
            }
            else
                _Tcl =  default(double);
            return _Tcl;

        }
        ///


        /// 服裝總隔熱值
        ///

        ///
        public double GetIcl()
        {
            Icl = 6.45 * (_Ts - _Tcl)/_Hf;
            return Icl;
        }
        ///
        /// 加權平均體溫
        ///

        ///
        ///
        public double Get_Tb(BodyTemperatureType btType)
        {
            if (btType == BodyTemperatureType.LowTemperature)
            {
                _Tb = 0.50 * _Ts + 0.50 * Tr;
            }
            else if (btType == BodyTemperatureType.NormalTemperature)
            {
                _Tb = 0.33 * _Ts + 0.67 * Tr;
            }
            else if (btType == BodyTemperatureType.HighTemperature)
            {
                _Tb = 0.20 * _Ts + 0.80 * Tr;
            }
            else
                _Tb = default(double);
            return _Tb;
           
        }
        ///
        /// 熱積或熱債
        ///

        ///
        ///
        ///
        public double Gets(double delta_Tb)
        {
            s = 3.473 * delta_Tb * _weight;
            return s;
        }
        public double Get_s(double delta_Tb)
        {
            _s = 3.474 * delta_Tb * _weight / _area;
            return _s;
        }
        public double Calculate()
        {
            Get_Ts(_edType);
            Get_Tcl(_edType);
            Get_Hf(_edType);
            Get_Tb(_btType);
            GetIcl();
            s = default(double);
            _s = default(double);
            if (_Calculates_s)
            {
                Gets(_delta_Tb);
                Get_s(_delta_Tb);
            }
            return 0.0;
        }
 
        /// //////
        CalcWithDotType  GHf;
        CalcWithDotType  GTs;
        CalcWithDotType  GTcl;
        CalcWithNone     GIcl;
        CalcWithTempType GTb;
        CalcWithDouble   Gs;
        CalcWithDouble   G_s;
        public CalcWithNone     Calc;
        #endregion
        #region Construction
        public Parameter()
        {
            GHf = Get_Hf;
            GTs = Get_Ts;
            GTcl = Get_Tcl;
            GIcl = GetIcl;
            GTb = Get_Tb;
            Gs = Gets;
            G_s = Get_s;
            Calc = Calculate;
        }
        #endregion
        #region Indexers
        public CalcWithDotType this[Type type, string delname]
        {
            get
            {
                if (type == typeof(CalcWithDotType) && (delname.ToLower() == "Get_Hf"))            
                    return  GHf;
                else if (type == typeof(CalcWithDotType) && (delname.ToLower() == "Get_Ts"))
                    return GTs;
                else if (type == typeof(CalcWithDotType) && (delname.ToLower() == "Get_Tcl"))
                    return GTcl;
                else
                {
                    MessageBox.Show(this.ToString() + "中不存在這樣的成員:" + type.ToString(), "成員不存在", MessageBoxButtons.OK);
                    return null;
                }
            }
            set
            {
                if (type == typeof(CalcWithDotType) && (delname.ToLower() == "Get_Hf"))
                {
                    GHf = value;
                }
                else if (type == typeof(CalcWithDotType) && (delname.ToLower() == "Get_Ts"))
                {
                    GTs = value;
                }
                else if (type == typeof(CalcWithDotType) && (delname.ToLower() == "Get_Tcl"))
                {
                    GTcl = value;
                }
                else
                {
                    MessageBox.Show(this.ToString() + "中不存在這樣的成員:" + type.ToString(), "成員不存在", MessageBoxButtons.OK);
                }
            }

        }
        public bool this[Type type]
        {
            get
            {
                if (type == typeof(bool))
                    return _Calculates_s;
                else
                    return Convert.ToBoolean( MessageBox.Show(this.ToString() + "中不存在這樣的成員:" + type.ToString(), "成員不存在", MessageBoxButtons.OK).GetHashCode());
            }
            set
            {
                if (type == typeof(bool))
                    _Calculates_s = value;
                else
                    MessageBox.Show(this.ToString() + "中不存在這樣的成員:" + type.ToString(), "成員不存在", MessageBoxButtons.OK);
            }
        }
        public Enum this[Enum type]
        {
            get
            {
                if (type.GetType() == typeof(BodyTemperatureType))
                    return _btType;
                else if (type.GetType() == typeof(ExtractDotType))
                    return _edType;
                else
                    return (Enum)MessageBox.Show(this.ToString() + "中不存在這樣的成員:" + type.ToString(), "成員不存在", MessageBoxButtons.OK) ;
            }
            set
            {
                if (type.GetType() == typeof(BodyTemperatureType))
                    _btType = (BodyTemperatureType)value;
                else if (type.GetType() == typeof(ExtractDotType))
                    _edType = (ExtractDotType)value;
                else
                    MessageBox.Show(this.ToString() + "中不存在這樣的成員:" + type.ToString(), "成員不存在", MessageBoxButtons.OK).GetHashCode();

            }
        }
        public double this[string paraid]
        {
            get
            {
                if (paraid == "Hf1")
                    return Hf1;
                else if (paraid == "Hf2")
                    return Hf2;
                else if (paraid == "Hf3")
                    return Hf3;
                else if (paraid == "Hf4")
                    return Hf4;
                else if (paraid == "Hf5")
                    return Hf5;
                else if (paraid == "Hf6")
                    return Hf6;
                else if (paraid == "Hf7")
                    return Hf7;
                else if (paraid == "Hf8")
                    return Hf8;
                else if (paraid == "Hf9")
                    return Hf9;

                else if (paraid == "Ts1")
                    return Ts1;
                else if (paraid == "Ts2")
                    return Ts2;
                else if (paraid == "Ts3")
                    return Ts3;
                else if (paraid == "Ts4")
                    return Ts4;
                else if (paraid == "Ts5")
                    return Ts5;
                else if (paraid == "Ts6")
                    return Ts6;
                else if (paraid == "Ts7")
                    return Ts7;
                else if (paraid == "Ts8")
                    return Ts8;
                else if (paraid == "Ts9")
                    return Ts9;
                else if (paraid == "Tr")
                    return Tr;

                else if (paraid == "Tcl1")
                    return Tcl1;
                else if (paraid == "Tcl2")
                    return Tcl2;
                else if (paraid == "Tcl3")
                    return Tcl3;
                else if (paraid == "Tcl4")
                    return Tcl4;
                else if (paraid == "Tcl5")
                    return Tcl5;
                else if (paraid == "Tcl6")
                    return Tcl6;
                else if (paraid == "Tcl7")
                    return Tcl7;
                else if (paraid == "Tcl8")
                    return Tcl8;
                else if (paraid == "Tcl9")
                    return Tcl9;

                else if (paraid == "Icl")
                    return Icl;
                else if (paraid == "_Ts")
                    return _Ts;
                else if (paraid == "_Tcl")
                    return _Tcl;
                else if (paraid == "_Hf")
                    return _Hf;
                else if (paraid == "s")
                    return s;
                else if (paraid == "_Tb")
                    return _Tb;
                else if (paraid == "_s")
                    return _s;
                else if (paraid == "delta_Tb")
                    return _delta_Tb;
                else if (paraid.ToLower() == "weight")
                    return _weight;
                else if (paraid.ToLower() == "area")
                    return _area;
                else
                    return MessageBox.Show(this.ToString() + "中不存在這樣的成員:" + paraid, "成員不存在", MessageBoxButtons.OK).GetHashCode();
            }
            set
            {
                if (paraid == "Hf1")
                    Hf1 = value;
                else if (paraid == "Hf2")
                    Hf2 = value;
                else if (paraid == "Hf3")
                    Hf3 = value;
                else if (paraid == "Hf4")
                    Hf4 = value;
                else if (paraid == "Hf5")
                    Hf5 = value;
                else if (paraid == "Hf6")
                    Hf6 = value;
                else if (paraid == "Hf7")
                    Hf7 = value;
                else if (paraid == "Hf8")
                    Hf8 = value;
                else if (paraid == "Hf9")
                    Hf9 = value;

                else if (paraid == "Ts1")
                    Ts1 = value;
                else if (paraid == "Ts2")
                    Ts2 = value;
                else if (paraid == "Ts3")
                    Ts3 = value;
                else if (paraid == "Ts4")
                    Ts4 = value;
                else if (paraid == "Ts5")
                    Ts5 = value;
                else if (paraid == "Ts6")
                    Ts6 = value;
                else if (paraid == "Ts7")
                    Ts7 = value;
                else if (paraid == "Ts8")
                    Ts8 = value;
                else if (paraid == "Ts9")
                    Ts9 = value;
                else if (paraid == "Tr")
                    Tr = value;

                else if (paraid == "Tcl1")
                    Tcl1 = value;
                else if (paraid == "Tcl2")
                    Tcl2 = value;
                else if (paraid == "Tcl3")
                    Tcl3 = value;
                else if (paraid == "Tcl4")
                    Tcl4 = value;
                else if (paraid == "Tcl5")
                    Tcl5 = value;
                else if (paraid == "Tcl6")
                    Tcl6 = value;
                else if (paraid == "Tcl7")
                    Tcl7 = value;
                else if (paraid == "Tcl8")
                    Tcl8 = value;
                else if (paraid == "Tcl9")
                    Tcl9 = value;

                else if (paraid == "Icl")
                    Icl = value;
                else if (paraid == "_Ts")
                    _Ts = value;
                else if (paraid == "_Tcl")
                    _Tcl = value;
                else if (paraid == "_Hf")
                    _Hf = value;
                else if (paraid == "s")
                    s = value;
                else if (paraid == "_Tb")
                    _Tb = value;
                else if (paraid == "_s")
                    _s = value;
                else if (paraid == "delta_Tb")
                    _delta_Tb = value;
                else if (paraid == "weight")
                    _weight =  value;
                else if (paraid == "area")
                    _area =  value;
                else
                    MessageBox.Show(this.ToString() + "中不存在這樣的成員:" + paraid, "成員不存在", MessageBoxButtons.OK);
            }
        }     
        #endregion

    }
    #endregion

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-526504/,如需轉載,請註明出處,否則將追究法律責任。

相關文章