/// <summary> /// This function serializes object to frame group /// </summary> /// <typeparam name="T">Data type</typeparam> /// <param name="data">Data object</param> /// <param name="reflection">reflection info for this data type</param> /// <param name="PackageGroupAttribute">Current group attribute</param> /// <returns>Group | null</returns> private static PackageGroupItem InternalSerializeV2ToGroup <T>(T data, ReflectionType reflection, PackageGroupAttribute PackageGroupAttribute) { //variables Object value = null; //create group PackageGroupItem group = new PackageGroupItem(PackageGroupAttribute.Address); //set property foreach (ReflectionProperty item in reflection.PropertyCollection.Values) { //check property type if (item.Property.CanRead) { //get current attribute PackageItemAttribute attribute = item.GetCustomAttribute <PackageItemAttribute>(); if (attribute != null) { try { value = item.Property.GetValue(data, null); if (value != null) { IPackageItem frameItem = Package.CreatePackageItem(attribute.Type, attribute.Address, value); if (frameItem != null) { group.Add(frameItem); } } } catch (Exception ex) { throw new Exception(String.Format("Error type: {0} -> {1} [{2}]", attribute.Type, item.Property.Name, value), ex); } } } } //return current group return(group); }
/// <summary> /// Deserializes the Frame to a .NET object. /// </summary> /// <typeparam name="T">The type of the object to deserialize to.</typeparam> /// <param name="frame">The Frame to deserialize.</param> /// <param name="reflection">Reflection information for the Object to deserialize</param> /// <returns>The deserialized object from the Frame.</returns> private static T InternalDeserialize <T>(Package frame, ReflectionType reflection) { //intiailize object T result = (T)Activator.CreateInstance(typeof(T)); //set property foreach (ReflectionProperty item in reflection.PropertyCollection.Values) { //check property type if (item.Property.CanWrite) { //get current attribute PackageItemAttribute attribute = item.GetCustomAttribute <PackageItemAttribute>(); if (attribute != null) { Object value = null; try { //get vale from frame value = frame.GetValue <Object>(attribute.Address); //update value value = PackageHelper.InternalUpdateValue(attribute.Type, item.Property.PropertyType, value); //set value to property item.Property.SetValue(result, value, null); } catch (Exception ex) { throw new Exception(String.Format("Error type: {0} -> {1}", (value == null ? "NULL" : value.ToString()), item.Property.Name), ex); } } } } //return result return(result); }
/// <summary> /// Serializes the specified Object to Frame /// </summary> /// <typeparam name="T">The type of the object to serialize to.</typeparam> /// <param name="obj">The object to serialize.</param> /// <param name="frame">Frame</param> /// <param name="reflection">Reflection information for the Object to deserialize</param> /// <returns>Frame | null</returns> private static Package InternalSerialize <T>(T obj, Package frame, ReflectionType reflection) { //set property foreach (ReflectionProperty item in reflection.PropertyCollection.Values) { //check property type if (item.Property.CanRead) { //get current attribute PackageItemAttribute attribute = item.GetCustomAttribute <PackageItemAttribute>(); if (attribute != null) { Object value = null; try { value = item.Property.GetValue(obj, null); if (value != null) { IPackageItem frameItem = Package.CreatePackageItem(attribute.Type, attribute.Address, value); if (frameItem != null) { frame.Add(frameItem); } } } catch (Exception ex) { throw new Exception(String.Format("Error type: {0} -> {1} [{2}]", attribute.Type, item.Property.Name, value), ex); } } } } //return result return(frame); }