Exemple #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="ctrl"></param>
        /// <param name="propertyName"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static long GetLong(Control ctrl, string propertyName, long defaultValue = default(long))
        {
            if (ctrl == null)
            {
                ArgumentNullException ane = new ArgumentNullException(cNE);
                log.Error(ane.Output());
                throw ane;
            }

            if (propertyName.IsNullOrWhiteSpace())
            {
                ArgumentNullException ane = new ArgumentNullException(pNNE);
                log.Error(ane.Output());
                throw ane;
            }

            log.Debug($"{MethodBase.GetCurrentMethod().Name} : {ctrl.Uid}.{ctrl.Name}.{propertyName}");

            var val = GetBindingProperty(ctrl, propertyName, defaultValue).Value;

            log.Debug($"{MethodBase.GetCurrentMethod().Name} : {val.GetType()} => {val}");

            // Value is long so return it.
            if (val.GetType() == typeof(long))
            {
                return((long)val);
            }

            // Value is int so convert it before return.
            if (val.GetType() == typeof(int))
            {
                return(Convert.ToInt64(val));
            }

            // Value is int so parse it before return.
            if (val.GetType() == typeof(string))
            {
                return(long.Parse(val.ToString()));
            }

            // Invalid object to convert as long.
            InvalidCastException e = new InvalidCastException($"Invalid cast conversion type : {val.GetType()} => long");

            log.Error(e.Output());
            log.Error($"{MethodBase.GetCurrentMethod().Name} : {ctrl.Uid}.{ctrl.Name}.{propertyName} => {val.GetType()}");
            MessageBoxs.DebugFatal(e);
            throw e;
        }