Пример #1
0
        public Hyundai GetCar(HyundaiCars carType)
        {
            Hyundai hyundai = null;

            /* Use Reflection to make job easy */
            var memberInfo = typeof(HyundaiCars).GetMember(carType.ToString()).FirstOrDefault();

            if (memberInfo != null)
            {
                var whatType = (WhatType)memberInfo
                               .GetCustomAttributes(typeof(WhatType), true)
                               .FirstOrDefault();

                if (null != whatType)
                {
                    if (!_carMapDictionary.TryGetValue(carType, out hyundai))
                    {
                        hyundai = (Hyundai)Activator.CreateInstance(whatType.TypeOf);
                        _carMapDictionary.Add(carType, hyundai);
                    }
                    return(hyundai);
                }
            }
            //--- Alternate Approach (Simple Switch Case)---
            switch (carType)
            {
            case HyundaiCars.SantroCar:
                hyundai = new Santro();
                break;

            case HyundaiCars.I10Car:
                hyundai = new I10();
                break;

            case HyundaiCars.I20Car:
                hyundai = new I20();
                break;

            case HyundaiCars.VernaCar:
                hyundai = new Verna();
                break;

            case HyundaiCars.SonataCar:
                hyundai = new Sonata();
                break;
            }
            return(hyundai);
        }
Пример #2
0
    public Hyundai GetCar( HyundaiCars carType )
    {
      Hyundai hyundai = null;

      /* Use Reflection to make job easy */
      var memberInfo = typeof( HyundaiCars ).GetMember( carType.ToString() ).FirstOrDefault();
      if ( memberInfo != null )
      {
        var whatType = (WhatType)memberInfo
          .GetCustomAttributes( typeof( WhatType ), true )
          .FirstOrDefault();

        if ( null != whatType )
        {
          if ( !_carMapDictionary.TryGetValue( carType, out hyundai ) )
          {
            hyundai = (Hyundai)Activator.CreateInstance( whatType.TypeOf );
            _carMapDictionary.Add( carType, hyundai );
          }
          return hyundai;
        }
      }
      //--- Alternate Approach (Simple Switch Case)---
      switch ( carType )
      {
        case HyundaiCars.SantroCar:
          hyundai = new Santro();
          break;
        case HyundaiCars.I10Car:
          hyundai = new I10();
          break;
        case HyundaiCars.I20Car:
          hyundai = new I20();
          break;
        case HyundaiCars.VernaCar:
          hyundai = new Verna();
          break;
        case HyundaiCars.SonataCar:
          hyundai = new Sonata();
          break;
      }
      return hyundai;
    }