public static bool TryGetString(this JobDataMap map, string key, out string returnVal)
        {
            if (null == map)
            {
                throw new ArgumentNullException("map");
            }

            object output;

            if (map.TryGetValue(key, out output))
            {
                returnVal = (output as string); // Try cast to string, if success returnVal is not null

                // if cast failed, try calling ToString()
                if (null == returnVal)
                {
                    returnVal = (output == null) ? null : output.ToString();
                }

                return(returnVal != null);
            }
            else
            {
                returnVal = null;
                return(false);
            }
        }
Beispiel #2
0
        public static T GetAs <T>(this JobDataMap map, string key)
        {
            if (map is null)
            {
                throw new ArgumentNullException(nameof(map));
            }

            if (string.IsNullOrEmpty(key))
            {
                throw new ArgumentException($"“{nameof(key)}”不能是 Null 或为空。", nameof(key));
            }

            if (map.TryGetValue(key, out var oprovider) &&
                oprovider is T provider)
            {
                return(provider);
            }
            return(default);
        public static bool TryGetShort(this JobDataMap map, string key, out short returnVal)
        {
            if (null == map)
            {
                throw new ArgumentNullException("map");
            }

            object output;

            if (map.TryGetValue(key, out output))
            {
                return(Int16.TryParse(Convert.ToString(output, CultureInfo.InvariantCulture), out returnVal));
            }
            else
            {
                returnVal = 0;
                return(false);
            }
        }
        public static bool TryGet <T>(this JobDataMap map, string key, out T returnVal) where T : class, new()
        {
            if (null == map)
            {
                throw new ArgumentNullException("map");
            }

            object output;

            if (map.TryGetValue(key, out output))
            {
                returnVal = output as T;
                return(returnVal != null);
            }
            else
            {
                returnVal = null;
                return(false);
            }
        }