/// <summary> /// Get local value /// </summary> /// <param name="obj"></param> /// <param name="offset"></param> /// <returns></returns> internal object Get(object obj, int offset = 0) { var type = obj.GetType(); if (type.IsArray) { var array = GetArray(obj); for (int i = 0; i < array.Length; i++) { var el = array.GetValue(i); array.SetValue(Get(el, offset), i); offset += TagSize.GetSizeFromObject(el); } return(array); } else { if (type == typeof(int)) { return(GetInt32(offset)); } else if (type == typeof(uint)) { return(GetUInt32(offset)); } else if (type == typeof(short)) { return(GetInt16(offset)); } else if (type == typeof(ushort)) { return(GetUInt16(offset)); } else if (type == typeof(sbyte)) { return(GetInt8(offset)); } else if (type == typeof(byte)) { return(GetUInt8(offset)); } else if (type == typeof(string)) { return(GetString(offset)); } else if (type == typeof(float) || type == typeof(double)) { return(GetFloat32(offset)); } else if (type.IsClass && !type.IsAbstract) { return(GetType(obj, offset)); } else { throw new Exception("Error data type!"); } } }
/// <summary> /// Set local valute from type /// </summary> /// <param name="obj"></param> /// <param name="offset"></param> public void SetType(object obj, int offset = 0) { foreach (var property in TagHelper.GetAccessableProperties(obj.GetType())) { var value = property.GetValue(obj); Set(value, offset); offset += TagSize.GetSizeObject(value); } }
/// <summary> /// Set local value /// </summary> /// <param name="value"></param> /// <param name="offset"></param> internal void Set(object value, int offset = 0) { var type = value.GetType(); if (type.IsArray) { foreach (var el in GetArray(value)) { Set(el, offset); offset += TagSize.GetSizeFromObject(el); } } else { if (type == typeof(int)) { SetInt32((int)value, offset); } else if (type == typeof(uint)) { SetUInt32((uint)value, offset); } else if (type == typeof(short)) { SetInt16((short)value, offset); } else if (type == typeof(ushort)) { SetUInt16((ushort)value, offset); } else if (type == typeof(sbyte)) { SetInt8((sbyte)value, offset); } else if (type == typeof(byte)) { SetUInt8((byte)value, offset); } else if (type == typeof(string)) { SetString((string)value, offset); } else if (type == typeof(float) || type == typeof(double)) { SetFloat32((float)value, offset); } else if (type.IsClass && !type.IsAbstract) { SetType(value, offset); } else { throw new Exception("Error data type!"); } } }
/// <summary> /// Get local value form type /// </summary> /// <param name="obj"></param> /// <param name="offset"></param> /// <returns></returns> public object GetType(object obj, int offset = 0) { foreach (var property in GetAccessableProperties(obj.GetType())) { var value = property.GetValue(obj); property.SetValue(obj, Get(value, offset)); offset += TagSize.GetSizeFromObject(value); } return(obj); }
/// <summary> /// Create Tag array. /// </summary> /// <param name="name">The textual name of the tag to access. The name is anything allowed by the protocol. /// E.g. myDataStruct.rotationTimer.ACC, myDINTArray[42] etc.</param> /// <param name="length">elements count: 1- single, n-array.</param> /// <typeparam name="TCustomType">Type to create</typeparam> /// <returns></returns> public Tag <TCustomType> CreateTagArray <TCustomType>(string name, int length) { var type = typeof(TCustomType); if (!type.IsArray) { throw new ArgumentException("Is not array!"); } var obj = (Array)Activator.CreateInstance(type, length); TagValueManager.FixStringNullToEmpty(obj); return(CreateTagType <TCustomType>(name, TagSize.GetSizeFromObject(obj.GetValue(0)), length)); }
/// <summary> /// Create Tag array. /// </summary> /// <param name="name">The textual name of the tag to access. The name is anything allowed by the protocol. /// E.g. myDataStruct.rotationTimer.ACC, myDINTArray[42] etc.</param> /// <param name="length">elements count: 1- single, n-array.</param> /// <typeparam name="TCustomType">Type to create</typeparam> /// <returns></returns> public Tag <TCustomType> CreateTagArray <TCustomType>(string name, int length) where TCustomType : IList { var type = typeof(TCustomType); if (!type.IsArray) { throw new ArgumentException("Is not array!"); } if (length <= 0) { throw new ArgumentException("Length > 0!"); } var obj = TagHelper.CreateObject <TCustomType>(length); return(CreateTagType <TCustomType>(name, TagSize.GetSizeObject(obj[0]), length)); }
/// <summary> /// Create Tag custom Type Class /// </summary> /// <param name="name">The textual name of the tag to access. The name is anything allowed by the protocol. /// E.g. myDataStruct.rotationTimer.ACC, myDINTArray[42] etc.</param> /// <typeparam name="TCustomType">Class to create</typeparam> /// <returns></returns> public Tag <TCustomType> CreateTagType <TCustomType>(string name) { return(CreateTagType <TCustomType>(name, TagSize.GetSizeFromObject(TagHelper.CreateObject <TCustomType>(1)))); }