Example #1
0
        public static IConfigFile GetConfig(string name = null)
        {
            XFrmWorkAttribute first = PluginProvider.GetFirstPlugin(typeof(IConfigFile), name);

            if (first == null)
            {
                return(null);
            }
            IConfigFile instance = null;

            if (configFiles.TryGetValue(first.Name, out instance))
            {
                return(instance);
            }


            try
            {
                instance = PluginProvider.GetObjectInstance <IConfigFile>(first.Name);

                instance.ReadConfig(instance.SavePath);
            }
            catch (Exception ex)
            {
                if (instance == null)
                {
                    throw new Exception("不包含任何有关配置文件的任何信息,请检查");
                }
                instance.RebuildConfig();
                instance.SaveConfig();
            }
            configFiles.Add(first.Name, instance);
            return(instance);
        }
Example #2
0
        /// <summary>
        /// 在集合中获取一个实例,若无该实例,将搜索插件,并自动添加之
        /// </summary>
        /// <param name="name"></param>
        /// <param name="isAddToList">是否要加入列表,被托管 </param>
        /// <returns></returns>
        public static T Get <T>(this ICollection <T> collection, string name, bool?isAddToList = true) where  T : class, IProcess
        {
            var process = collection.FirstOrDefault((d) => name == d.TypeName) as T;

            if (process != null)
            {
                return(process);
            }
            XFrmWorkAttribute newProcess =
                PluginProvider.GetPluginCollection(typeof(T)).FirstOrDefault((d) => d.Name == name);

            if (newProcess == null)
            {
                // throw new Exception(string.Format("要获取的插件{0}无法在插件集合中找到"));
                return(null);
            }

            if (isAddToList == true)
            {
                collection.Add(newProcess.MyType);

                var newone = collection.FirstOrDefault((d) => name == d.TypeName);

                return(newone);
            }
            else
            {
                var plugin = PluginProvider.GetObjectInstance(newProcess.MyType) as T;
                return(plugin);
            }
        }
Example #3
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)
                    {
                        object index = Convert.ChangeType(data, typeof(int));
                        return(index);
                    }
                    else
                    {
                        string item  = data.ToString();
                        object index = Enum.Parse(type, item);
                        return(index);
                    }
                }
                if (type == typeof(XFrmWorkAttribute))
                {
                    var item = (string)data;
                    XFrmWorkAttribute newone = PluginProvider.GetPlugin(item);
                    return(newone);
                }

                if (data == null)
                {
                    return(oldValue);
                }
                try
                {
                    value = Convert.ChangeType(data, type);
                }
                catch (Exception ex)
                {
                    XLogSys.Print.Error("字典序列化失败" + ex);
                    return(null);
                }

                return(value);
            }

            return(oldValue);
        }