public void CreateNewItem(IEventLog log) { if (log.GetType() == typeof(EventLogWaterDamage)) { Instantiate(waterDamageLogItem, transform).Init(log as EventLogWaterDamage); } else if (log.GetType() == typeof(EventLogTradeDealSold)) { Instantiate(tradeDealSoldItem, transform).Init(log as EventLogTradeDealSold); } else if (log.GetType() == typeof(EventLogResourcesUnavailable)) { Instantiate(resourceUnavailableItem, transform).Init(log as EventLogResourcesUnavailable); } else if (log.GetType() == typeof(EventLogTemperatureEventTriggered)) { Instantiate(temperatureEventItem, transform).Init(log as EventLogTemperatureEventTriggered); } if (transform.childCount > maxItemsShowing) { Destroy(transform.GetChild(0).gameObject); } }
/// <summary> /// Iterates through the object collection and assigns all values that /// have the correct custom attributes /// </summary> /// <param name="eventLog">The object for saving the event information</param> /// <param name="ob">Array of objects to set the parameters for on the event log object</param> private static void SetAttributeValues(IEventLog eventLog, object[] ob) { foreach (object o in ob) { // Can't use auto mapper because of the serialized properties and labeling PropertyInfo[] props = o.GetType().GetProperties(); foreach (var prop in props) { try { var attributes = prop.GetCustomAttributes(false); foreach (var attribute in attributes) { // Find the same named property on the eventLog as the ob and set the value equal if (attribute.GetType() == typeof(ValueLogAttribute)) { //I am sure there is a more elegant way to do this if (prop.GetType() == eventLog.GetType().GetProperty(prop.Name).GetType()) { PropertyInfo propInfo = eventLog.GetType().GetProperty(prop.Name); if (propInfo != null) { eventLog.GetType().GetProperty(prop.Name).SetValue(eventLog, prop.GetValue(o, null), null); } } } else if (attribute.GetType() == typeof(SerializeXMLLogAttribute)) { //Make sure that there is a property with that name in the eventLog PropertyInfo propInfo = eventLog.GetType().GetProperty(prop.Name); if (propInfo != null) { //TODO: Serialize the property. This is assumed because it is a complex type such as a class eventLog.GetType().GetProperty(prop.Name).SetValue(eventLog, prop.GetValue(o, null), null); System.IO.StringWriter writer = new System.IO.StringWriter(); //XmlSerializer serializer = new XmlSerializer(typeof(prop.GetType())); //serializer.Serialize(writer, prop.GetValue()); } /// Notes: /// XML serialization only serializes public fields and properties. /// XML serialization does not include any type information. /// We need to have a default/ non - parameterized constructor in order to serialize an object. /// ReadOnly properties are not serialized. } else if (attribute.GetType() == typeof(SerializeJSONLogAttribute)) { //Make sure that there is a property with that name in the eventLog PropertyInfo propInfo = eventLog.GetType().GetProperty(prop.Name); if (propInfo != null) { //serialize using JSON //only store as string with same name string output = Newtonsoft.Json.JsonConvert.SerializeObject(prop.GetValue(o, null)); eventLog.GetType().GetProperty(prop.Name).SetValue(eventLog, output, null); } } } } catch (Exception exp) { //exp.Message; //throw; } } } }