--- trunk/libxmltv/Core/XMLTV.cs 2013/03/08 07:01:37 52 +++ trunk/libxmltv/Core/XMLTV.cs 2013/03/09 09:29:40 72 @@ -15,15 +15,27 @@ /// public static class XMLTV { - static XMLTV() { xmltv_logger.Initialize(); } + static XMLTV() + { + xmltv_logger.Initialize(); + if (instance == null) + { + instance = new XMLTV(); + } + } static IXMLTV instance; public static IXMLTVRuntimeInstance GetInstance() { return instance.GetInstance(); } - public static void CreateInstance(params object[] args) { instance = new XMLTV(args); } - //public static void CreateInstance(string xml_file, EventHandler t) { instance = new XMLTV(xml_file, t); } - //public static IXMLTVSerializer GetSerializer() {return new XMLTVSerializer(InternalGetInstance()); } + public static void CreateInstance(params object[] args) { instance = new XMLTV(args); } - //private static IXMLTVSerializer CreateSerializer() where T : class { return new XMLTVSerializer(InternalGetInstance() as T); } + public static void CreateFromInstance(object raw_instance, EventHandler handler) + { + instance = new XMLTV(raw_instance,handler); + if (OnInstanceCreated != null) + { + OnInstanceCreated.Invoke(null, new EventArgs()); + } + } #region IXMLTVSerializer members public static bool Serialize(string file) { return instance.Serialize(file); } @@ -31,35 +43,39 @@ public static IXMLTVRuntimeInstance DeSerialize(string file, out bool status) { return instance.DeSerialize(file, out status); } public static IXMLTVRuntimeInstance DeSerialize(Stream stream, out bool status) { return instance.DeSerialize(stream, out status); } #endregion + + public static void DestroyInstance() { instance.DestroyInstance(); } + + public static EventHandler OnInstanceCreated + { + get { return instance.OnInstanceCreated; } + set { instance.OnInstanceCreated = value; } + } } - internal class XMLTV : IXMLTV where CLASS : class,INTERFACE + internal class XMLTV : IDestroyInstance, IXMLTV where CLASS : class,INTERFACE,new() { + public XMLTV() + { + instance = new CLASS(); + } + public XMLTV(object raw_instance, EventHandler handler) + { + instance = (CLASS)Convert.ChangeType(raw_instance, typeof(CLASS)); + if (instance != null) + { + IRuntimeInstanceLoader loader = (instance as IRuntimeInstanceLoader); + if (loader != null) + { + SetOnInstanceCreated(handler); + instance = loader.LoadFromInstance(instance); + } + } + } public XMLTV(params object[] args) { - ////instance = new CLASS(xml_file, t); - //instance = null; - - //Type type = typeof(CLASS); - //var ctors = type.GetConstructors(); - //foreach (var ctor in ctors) - //{ - // var ctor_params = ctor.GetParameters(); - // if (ctor_params.Count() == 2) - // { - // if (ctor_params[0].ParameterType == typeof(string)) - // { - // if (ctor_params[1].ParameterType == typeof(EventHandler)) - // { - // object o = ctor.Invoke(new object[] { xml_file, t }); - // instance = (CLASS)Convert.ChangeType(o, typeof(CLASS)); - // break; - // } - // } - // } - // else { continue; } - //} - BindingFlags flags = BindingFlags.NonPublic | BindingFlags.Instance; + + BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance; CultureInfo culture = CultureInfo.CurrentCulture; try { @@ -68,7 +84,7 @@ } catch (Exception ex) { - xmltv_logger.Log.Error.WriteLine(ex.ToString()); + xmltv_logger.Error.WriteLine(ex.ToString()); StringBuilder parameter_builder = new StringBuilder(); foreach (object arg in args) @@ -109,6 +125,34 @@ public INTERFACE DeSerialize(string file, out bool status) { return CreateSerializer().DeSerialize(file, out status); } public INTERFACE DeSerialize(Stream stream, out bool status) { return CreateSerializer().DeSerialize(stream, out status); } #endregion + public void DestroyInstance() + { + IDestroyInstance destoyer = (instance as IDestroyInstance); + if (destoyer != null) + { + destoyer.DestroyInstance(); + } + else + { + xmltv_logger.Error.WriteLine("Unable to call DestroyInstance() on type: '{0}'", instance.GetType().Name); + } + } + private void SetOnInstanceCreated(EventHandler event_instance) + { + ISetCreatedInstanceEvent setter = (instance as ISetCreatedInstanceEvent); + if (setter != null) { setter.SetOnInstanceCreated(event_instance); } + } + private EventHandler GetOnInstanceCreated() + { + IGetCreatedInstanceEvent getter = (instance as IGetCreatedInstanceEvent); + if (getter != null) { return getter.GetOnInstanceCreated(); } + return null; + } + public EventHandler OnInstanceCreated + { + get { return GetOnInstanceCreated(); } + set { SetOnInstanceCreated(value); } + } } }