예제 #1
0
        public static T Set <T>(this IDictionary <string, object> dict, string key, T oldValue)
        {
            T      value;
            object data = null;

            if (dict == null)
            {
                return(oldValue);
            }
            if (dict.TryGetValue(key, out data))
            {
                var type = typeof(T);
                if (type.IsEnum)
                {
                    if (data is int)
                    {
                        var index = (T)Convert.ChangeType(data, typeof(int));
                        return(index);
                    }
                    else
                    {
                        var item  = data.ToString();
                        var index = (T)Enum.Parse(typeof(T), item);
                        return(index);
                    }
                }
                if (type == typeof(XFrmWorkAttribute))
                {
                    var item   = (string)data;
                    var newone = PluginProvider.GetPlugin(item);
                    return((T)Convert.ChangeType(newone, typeof(XFrmWorkAttribute)));
                }

                if (data == null)
                {
                    return(oldValue);
                }
                try
                {
                    value = (T)Convert.ChangeType(data, typeof(T));
                }
                catch (Exception ex)
                {
                    XLogSys.Print.Error(GlobalHelper.Get("key_112") + ex);
                    return(default(T));
                }

                return(value);
            }

            return(oldValue);
        }
예제 #2
0
        public static object Set(this IDictionary <string, object> dict, string key, object oldValue, Type type = null)
        {
            object value;
            object data = null;

            if (dict == null)
            {
                return(oldValue);
            }
            if (type == null)
            {
                if (oldValue != null)
                {
                    type = oldValue.GetType();
                }
            }
            if (type == null)
            {
                return(null);
            }
            if (dict.TryGetValue(key, out data))
            {
                if (type.IsEnum)
                {
                    if (data is int)
                    {
                        var index = Convert.ChangeType(data, typeof(int));
                        return(index);
                    }
                    else
                    {
                        var item  = data.ToString();
                        var index = Enum.Parse(type, item);
                        return(index);
                    }
                }
                if (type == typeof(XFrmWorkAttribute))
                {
                    var item   = (string)data;
                    var newone = PluginProvider.GetPlugin(item);
                    return(newone);
                }

                if (data == null)
                {
                    return(oldValue);
                }
                try
                {
                    value = Convert.ChangeType(data, type);
                }
                catch (Exception ex)
                {
                    XLogSys.Print.Error(GlobalHelper.Get("key_112") + ex);
                    return(null);
                }

                return(value);
            }

            return(oldValue);
        }