using System; using System.Collections.Generic; using System.Linq; using System.Text; using libxmltv.Interfaces; using System.Xml.Linq; using System.Reflection; namespace libxmltv.Core { [Serializable] internal class XMLTVChannel : XMLTVBase, IXMLTVChannel { public XMLTVChannel() : base(null,XMLTVConstants.CHANNEL_ELEMENT) { Id = string.Empty; Number = 0; CallSign = string.Empty; Name = string.Empty; } public XMLTVChannel(XMLTVRuntimeInstance instance, XElement node) : base(instance, XMLTVConstants.CHANNEL_ELEMENT) { xmltv_logger.Verbose.Debug.WriteLine("Creating Instance of XMLTVChannel"); Create(node); xmltv_logger.Verbose.Debug.WriteLine("Created Instance of XMLTVChannel"); UpdateInstance(); } #region IXMLTVChannel members public string Id { get; private set; } public int Number { get; private set; } public string CallSign { get; private set; } public string Name { get; private set; } #endregion public override string ToString() { return string.Format("{0}: {1} {2} ({3})", Id, Number, Name, CallSign); } private void UpdateInstance() { bool found_field = false; var instance_type = this.GetInstance().GetType(); var fields = instance_type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); foreach (var field in fields) { if (field.FieldType == typeof(List)) { found_field = true; try { xmltv_logger.Verbose.Debug.WriteLine("Updating instance with channel information: {0}", this.ToString()); //field.SetValue(this.GetInstance(), new List() { this }); var list = (List)field.GetValue(this.GetInstance()); list.Add(this); field.SetValue(this.GetInstance(), list); break; } catch (Exception ex) { xmltv_logger.Verbose.Error.WriteLine("Unable to update instance with channel information."); xmltv_logger.Verbose.Error.WriteLine(ex.ToString()); } } } if (!found_field) { xmltv_logger.Verbose.Error.WriteLine("Unable to update instance with channel information."); } } private void Create(XElement node) { // get the channel id Id = node.Attribute(XMLTVConstants.Channels.ChannelId).Value; xmltv_logger.Verbose.Debug.WriteLine("\tchannel_id: {0}", Id); var names = node.Descendants(XMLTVConstants.Channels.ChannelDisplayName).ToList(); Number = Convert.ToInt32(names[1].Value); xmltv_logger.Verbose.Debug.WriteLine("\tchannel_number: {0}", Number); CallSign = names[2].Value; xmltv_logger.Verbose.Debug.WriteLine("\tchannel_callsign: {0}", CallSign); Name = names[3].Value; xmltv_logger.Verbose.Debug.WriteLine("\tchannel_name: {0}", Name); } } }