/[xmltv_parser]/trunk/libxmltv/Core/XMLTV.cs
ViewVC logotype

Diff of /trunk/libxmltv/Core/XMLTV.cs

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

--- trunk/libxmltv/Core/XMLTV.cs	2013/03/08 06:39:56	51
+++ trunk/libxmltv/Core/XMLTV.cs	2013/03/08 07:01:37	52
@@ -5,6 +5,8 @@
 using libxmltv.Interfaces;
 using System.ComponentModel;
 using System.IO;
+using System.Reflection;
+using System.Globalization;
 
 namespace libxmltv.Core
 {
@@ -17,8 +19,8 @@
 
         static IXMLTV<IXMLTVRuntimeInstance, XMLTVRuntimeInstance> instance;
         public static IXMLTVRuntimeInstance GetInstance() { return instance.GetInstance(); }
-        public static void CreateInstance(string xml_file) { CreateInstance(xml_file, null); }
-        public static void CreateInstance(string xml_file, EventHandler<CancelEventArgs> t) { instance = new XMLTV<IXMLTVRuntimeInstance, XMLTVRuntimeInstance>(xml_file, t); }
+        public static void CreateInstance(params object[] args) { instance = new XMLTV<IXMLTVRuntimeInstance, XMLTVRuntimeInstance>(args); }
+        //public static void CreateInstance(string xml_file, EventHandler<CancelEventArgs> t) { instance = new XMLTV<IXMLTVRuntimeInstance, XMLTVRuntimeInstance>(xml_file, t); }
         //public static IXMLTVSerializer<IXMLTVRuntimeInstance> GetSerializer() {return new XMLTVSerializer<IXMLTVRuntimeInstance>(InternalGetInstance()); }
 
         //private static IXMLTVSerializer<T> CreateSerializer<T>() where T : class { return new XMLTVSerializer<T>(InternalGetInstance() as T); }
@@ -33,30 +35,50 @@
 
     internal class XMLTV<INTERFACE, CLASS> : IXMLTV<INTERFACE, CLASS> where CLASS : class,INTERFACE
     {
-        public XMLTV(string xml_file) : this(xml_file, null) { }
-        public XMLTV(string xml_file, EventHandler<CancelEventArgs> t)
+        public XMLTV(params object[] args)
         {
-            //instance = new CLASS(xml_file, t); 
-            instance = null;
+            ////instance = new CLASS(xml_file, t); 
+            //instance = null;
 
-            Type type = typeof(CLASS);
-            var ctors = type.GetConstructors();
-            foreach (var ctor in ctors)
+            //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<CancelEventArgs>))
+            //            {
+            //                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;
+            CultureInfo culture = CultureInfo.CurrentCulture;
+            try
             {
-                var ctor_params = ctor.GetParameters();
-                if (ctor_params.Count() == 2)
+                object raw_instance = Activator.CreateInstance(typeof(CLASS), flags, null, args, culture);
+                instance = (CLASS)Convert.ChangeType(raw_instance, typeof(CLASS));
+            }
+            catch (Exception ex)
+            {
+                xmltv_logger.Log.Error.WriteLine(ex.ToString());
+
+                StringBuilder parameter_builder = new StringBuilder();
+                foreach (object arg in args)
                 {
-                    if (ctor_params[0].ParameterType == typeof(string))
-                    {
-                        if (ctor_params[1].ParameterType == typeof(EventHandler<CancelEventArgs>))
-                        {
-                            object o = ctor.Invoke(new object[] { xml_file, t });
-                            instance = (CLASS)Convert.ChangeType(o, typeof(CLASS));
-                            break;
-                        }
-                    }
+                    Type type = arg.GetType();
+                    parameter_builder.AppendFormat("({0}), ", type.FullName);
                 }
-                else { continue; }
+
+                string type_parameters = parameter_builder.ToString().TrimEnd(new char[] { ',', ' ' });
+                throw new Exception(string.Format("Unable to create instance of: '{0}' with parameters: '{1}'", typeof(CLASS).Name, type_parameters), ex);
             }
         }
 

 

  ViewVC Help
Powered by ViewVC 1.1.22