Ejemplo n.º 1
0
 /// <summary>
 /// 清理所有缓存
 /// </summary>
 private static void ClearAllCache()
 {
     foreach (KeyValuePair <string, InjectorItem> item in BusinessInters)
     {
         CacheInjector.RemoveCatche(item.Key);
     }
 }
Ejemplo n.º 2
0
        /// <summary>
        /// 获取接口实例
        /// </summary>
        /// <typeparam name="T">业务接口</typeparam>
        /// <param name="args">参数集合</param>
        /// <returns>返回配置中中实现类的实例</returns>
        public static T GetInstance <T>(object[] args)
        {
            string       interfacetype = typeof(T).ToString();
            InjectorItem item          = InjectorConfigManager.GetInjectorItem(interfacetype);

            if (item != null)
            {
                object obj = CacheInjector.GetFromCache(interfacetype);
                //object obj = null;
                if (obj == null)
                {
                    string path = AppDomain.CurrentDomain.BaseDirectory + InjectorConfigManager.InjectorReflectPath + item.File;
                    obj = (item.IsGlobal ? Assembly.LoadFrom(path).CreateInstance(item.Instance, true, BindingFlags.CreateInstance, null, args, null, null) : Assembly.LoadFrom(path).GetType(item.Instance));
                    CacheInjector.SaveToCache(interfacetype, obj);
                }

                return(item.IsGlobal ? (T)obj : (T)Activator.CreateInstance((Type)obj, args));
            }

            return(default(T));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 重新加载配置信息
        /// </summary>
        private static void ReloadConfigFile()
        {
            try
            {
                if (File.Exists(ConfigFilePath))
                {
                    XmlDocument doc = new XmlDocument();
                    doc.Load(ConfigFilePath);

                    // Modified by Michael Feng at 2013-05-08
                    XmlNodeList injectNodesList = doc.DocumentElement.SelectNodes("./Injector");
                    foreach (XmlNode node in injectNodesList)
                    {
                        bool isGlobal = true;
                        if (node.Attributes["global"] != null)
                        {
                            isGlobal = bool.Parse(node.Attributes["global"].Value);
                        }

                        if (node.Attributes["interface"] != null &&
                            node.Attributes["instance"] != null &&
                            node.Attributes["file"] != null &&
                            !string.IsNullOrEmpty(node.Attributes["interface"].Value) &&
                            !string.IsNullOrEmpty(node.Attributes["instance"].Value) &&
                            !string.IsNullOrEmpty(node.Attributes["file"].Value) &&
                            !string.IsNullOrEmpty(node.Attributes["file"].Value))
                        {
                            if (BusinessInters.ContainsKey(node.Attributes["interface"].Value) &&
                                (!BusinessInters[node.Attributes["interface"].Value].Instance.Equals(node.Attributes["instance"].Value) ||
                                 !BusinessInters[node.Attributes["interface"].Value].File.Equals(node.Attributes["file"].Value)))
                            {
                                BusinessInters[node.Attributes["interface"].Value].Instance = node.Attributes["instance"].Value;
                                BusinessInters[node.Attributes["interface"].Value].File     = node.Attributes["file"].Value;
                                BusinessInters[node.Attributes["interface"].Value].IsGlobal = isGlobal;
                                CacheInjector.RemoveCatche(node.Attributes["interface"].Value);
                            }

                            if (!BusinessInters.ContainsKey(node.Attributes["interface"].Value))
                            {
                                InjectorItem item = new InjectorItem();
                                item.InterFace = node.Attributes["interface"].Value;
                                item.Instance  = node.Attributes["instance"].Value;
                                item.File      = node.Attributes["file"].Value;
                                item.IsGlobal  = isGlobal;
                                BusinessInters.Add(node.Attributes["interface"].Value, item);
                            }
                        }
                    }

                    string[] keys = new string[BusinessInters.Count];
                    BusinessInters.Keys.CopyTo(keys, 0);

                    foreach (string key in keys)
                    {
                        bool isExist = false;
                        foreach (XmlNode node in doc.DocumentElement.ChildNodes)
                        {
                            if (node.Attributes["interface"] != null &&
                                key.Equals(node.Attributes["interface"].Value))
                            {
                                isExist = true;
                                break;
                            }
                        }
                        if (isExist == false)
                        {
                            BusinessInters.Remove(key);
                            CacheInjector.RemoveCatche(key);
                        }
                    }
                }
            }
            catch
            {
            }
        }