/// <summary> /// 创建对象,如果存在,则直接返回;如果不存在,创建后返回 /// </summary> /// <param name="letter"></param> /// <returns></returns> public static IViewAPI CreateInstance(Letter letter) { string assemblyName = "Song.ViewData"; string classFullName = String.Format("{0}.Methods.{1}", assemblyName, letter.ClassName); IViewAPI obj = null; //由缓存中查找,是否存在 ExecuteMethod curr = ExecuteMethod.GetInstance(); foreach (KeyValuePair <string, object> kv in curr._objects) { if (classFullName.Trim().Equals(kv.Key, StringComparison.CurrentCultureIgnoreCase)) { obj = (IViewAPI)kv.Value; break; } } if (obj != null) { return(obj); } //如果之前未创建,则重新创建 Type type = null; Assembly assembly = Assembly.Load(assemblyName); Type[] types = assembly.GetExportedTypes() .Where(t => t.GetInterfaces().Contains(typeof(IViewAPI))) .ToArray(); foreach (Type info in types) { if (info.FullName.Equals(classFullName, StringComparison.CurrentCultureIgnoreCase)) { type = info; break; } } if (type == null) { throw new Exception( string.Format("调用的接口'{0}'不存在, 可能是'{1}'拼写错误", letter.API_PATH, letter.ClassName)); } obj = (IViewAPI)System.Activator.CreateInstance(type); //创建对象 //记录到缓存 if (!ExecuteMethod.GetInstance()._objects.ContainsKey(type.FullName)) { ExecuteMethod.GetInstance()._objects.Add(type.FullName, obj); } return(obj); }
/// <summary> /// 创建对象,如果存在,则直接返回;如果不存在,创建后返回 /// </summary> /// <param name="letter"></param> /// <returns></returns> public static object CreateInstance(Letter letter) { string assemblyName = "Song.ViewData"; string classFullName = String.Format("{0}.Methods.{1}", assemblyName, letter.ClassName); object obj = null; //由缓存中查找,是否存在 ExecuteMethod curr = ExecuteMethod.GetInstance(); foreach (KeyValuePair <string, object> kv in curr._objects) { if (classFullName.Trim().Equals(kv.Key, StringComparison.CurrentCultureIgnoreCase)) { obj = kv.Value; break; } } if (obj != null) { return(obj); } //如果之前未创建,则重新创建 Type type = null; Assembly assembly = Assembly.Load(assemblyName); foreach (Type info in assembly.GetTypes()) { if (info.FullName.Equals(classFullName, StringComparison.CurrentCultureIgnoreCase)) { type = info; break; } } if (type == null) { throw new Exception( string.Format("调用的对象'{0}'不存在, 可能是'{1}'拼写错误", classFullName, letter.ClassName)); } obj = System.Activator.CreateInstance(type); //创建对象 ExecuteMethod.GetInstance()._objects.Add(type.FullName, obj); //记录到缓存 return(obj); }