5 |
using libxmltv.Interfaces; |
using libxmltv.Interfaces; |
6 |
using System.ComponentModel; |
using System.ComponentModel; |
7 |
using System.IO; |
using System.IO; |
8 |
|
using System.Reflection; |
9 |
|
using System.Globalization; |
10 |
|
|
11 |
namespace libxmltv.Core |
namespace libxmltv.Core |
12 |
{ |
{ |
19 |
|
|
20 |
static IXMLTV<IXMLTVRuntimeInstance, XMLTVRuntimeInstance> instance; |
static IXMLTV<IXMLTVRuntimeInstance, XMLTVRuntimeInstance> instance; |
21 |
public static IXMLTVRuntimeInstance GetInstance() { return instance.GetInstance(); } |
public static IXMLTVRuntimeInstance GetInstance() { return instance.GetInstance(); } |
22 |
public static void CreateInstance(string xml_file) { CreateInstance(xml_file, null); } |
public static void CreateInstance(params object[] args) { instance = new XMLTV<IXMLTVRuntimeInstance, XMLTVRuntimeInstance>(args); } |
23 |
public static void CreateInstance(string xml_file, EventHandler<CancelEventArgs> t) { instance = new XMLTV<IXMLTVRuntimeInstance, XMLTVRuntimeInstance>(xml_file, t); } |
//public static void CreateInstance(string xml_file, EventHandler<CancelEventArgs> t) { instance = new XMLTV<IXMLTVRuntimeInstance, XMLTVRuntimeInstance>(xml_file, t); } |
24 |
//public static IXMLTVSerializer<IXMLTVRuntimeInstance> GetSerializer() {return new XMLTVSerializer<IXMLTVRuntimeInstance>(InternalGetInstance()); } |
//public static IXMLTVSerializer<IXMLTVRuntimeInstance> GetSerializer() {return new XMLTVSerializer<IXMLTVRuntimeInstance>(InternalGetInstance()); } |
25 |
|
|
26 |
//private static IXMLTVSerializer<T> CreateSerializer<T>() where T : class { return new XMLTVSerializer<T>(InternalGetInstance() as T); } |
//private static IXMLTVSerializer<T> CreateSerializer<T>() where T : class { return new XMLTVSerializer<T>(InternalGetInstance() as T); } |
35 |
|
|
36 |
internal class XMLTV<INTERFACE, CLASS> : IXMLTV<INTERFACE, CLASS> where CLASS : class,INTERFACE |
internal class XMLTV<INTERFACE, CLASS> : IXMLTV<INTERFACE, CLASS> where CLASS : class,INTERFACE |
37 |
{ |
{ |
38 |
public XMLTV(string xml_file) : this(xml_file, null) { } |
public XMLTV(params object[] args) |
|
public XMLTV(string xml_file, EventHandler<CancelEventArgs> t) |
|
39 |
{ |
{ |
40 |
//instance = new CLASS(xml_file, t); |
////instance = new CLASS(xml_file, t); |
41 |
instance = null; |
//instance = null; |
42 |
|
|
43 |
Type type = typeof(CLASS); |
//Type type = typeof(CLASS); |
44 |
var ctors = type.GetConstructors(); |
//var ctors = type.GetConstructors(); |
45 |
foreach (var ctor in ctors) |
//foreach (var ctor in ctors) |
46 |
|
//{ |
47 |
|
// var ctor_params = ctor.GetParameters(); |
48 |
|
// if (ctor_params.Count() == 2) |
49 |
|
// { |
50 |
|
// if (ctor_params[0].ParameterType == typeof(string)) |
51 |
|
// { |
52 |
|
// if (ctor_params[1].ParameterType == typeof(EventHandler<CancelEventArgs>)) |
53 |
|
// { |
54 |
|
// object o = ctor.Invoke(new object[] { xml_file, t }); |
55 |
|
// instance = (CLASS)Convert.ChangeType(o, typeof(CLASS)); |
56 |
|
// break; |
57 |
|
// } |
58 |
|
// } |
59 |
|
// } |
60 |
|
// else { continue; } |
61 |
|
//} |
62 |
|
BindingFlags flags = BindingFlags.NonPublic | BindingFlags.Instance; |
63 |
|
CultureInfo culture = CultureInfo.CurrentCulture; |
64 |
|
try |
65 |
{ |
{ |
66 |
var ctor_params = ctor.GetParameters(); |
object raw_instance = Activator.CreateInstance(typeof(CLASS), flags, null, args, culture); |
67 |
if (ctor_params.Count() == 2) |
instance = (CLASS)Convert.ChangeType(raw_instance, typeof(CLASS)); |
68 |
|
} |
69 |
|
catch (Exception ex) |
70 |
|
{ |
71 |
|
xmltv_logger.Log.Error.WriteLine(ex.ToString()); |
72 |
|
|
73 |
|
StringBuilder parameter_builder = new StringBuilder(); |
74 |
|
foreach (object arg in args) |
75 |
{ |
{ |
76 |
if (ctor_params[0].ParameterType == typeof(string)) |
Type type = arg.GetType(); |
77 |
{ |
parameter_builder.AppendFormat("({0}), ", type.FullName); |
|
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; |
|
|
} |
|
|
} |
|
78 |
} |
} |
79 |
else { continue; } |
|
80 |
|
string type_parameters = parameter_builder.ToString().TrimEnd(new char[] { ',', ' ' }); |
81 |
|
throw new Exception(string.Format("Unable to create instance of: '{0}' with parameters: '{1}'", typeof(CLASS).Name, type_parameters), ex); |
82 |
} |
} |
83 |
} |
} |
84 |
|
|