Esempio n. 1
0
        /// <summary>
        /// Set property value.
        /// </summary>
        /// <param name="instance">Instance where value is set.</param>
        /// <param name="target">Property type.</param>
        /// <param name="value">New value.</param>
        public static void SetValue(object instance, object target, object value)
        {
            PropertyInfo pi = target as PropertyInfo;

            if (pi != null)
            {
                if (value is IEnumerable)
                {
                    if (!pi.PropertyType.IsAssignableFrom(value.GetType()))
                    {
                        if (pi.PropertyType.IsArray)
                        {
                            int   pos   = 0;
                            Array items = Array.CreateInstance(GXInternal.GetPropertyType(pi.PropertyType), ((IList)value).Count);
                            foreach (object it in (IList)value)
                            {
                                items.SetValue(it, pos);
                                ++pos;
                            }
                            value = items;
                        }
#if !__MOBILE__
                        else if (pi.PropertyType.IsGenericType && pi.PropertyType.GetGenericTypeDefinition() == typeof(System.Data.Linq.EntitySet <>))
                        {
                            Type  listT = typeof(System.Data.Linq.EntitySet <>).MakeGenericType(new[] { GXInternal.GetPropertyType(pi.PropertyType) });
                            IList list  = (IList)GXJsonParser.CreateInstance(listT);
                            foreach (object it in (IList)value)
                            {
                                list.Add(it);
                            }
                            value = list;
                        }
#endif //__MOBILE__
                        else
                        {
                            Type  listT = typeof(List <>).MakeGenericType(new[] { GXInternal.GetPropertyType(pi.PropertyType) });
                            IList list  = (IList)GXJsonParser.CreateInstance(listT);
                            foreach (object it in (IList)value)
                            {
                                list.Add(it);
                            }
                            value = list;
                        }
                    }
                }
                pi.SetValue(instance, value, null);
            }
            else
            {
                FieldInfo fi = target as FieldInfo;
                fi.SetValue(instance, value);
            }
        }
Esempio n. 2
0
 /// <summary>
 /// Change DB value type.
 /// </summary>
 /// <param name="value"></param>
 /// <param name="type"></param>
 /// <returns></returns>
 static public object ChangeType(object value, Type type)
 {
     if (value == null || value is DBNull)
     {
         return(null);
     }
     if (type == typeof(byte[]))
     {
         if (value is string)
         {
             return(GXCommon.HexToBytes((string)value, false));
         }
         return(GXCommon.HexToBytes(ASCIIEncoding.ASCII.GetString((byte[])value), false));
     }
     //Date times are saved in UTC format.
     if (type == typeof(DateTime))
     {
         DateTime dt = (DateTime)Convert.ChangeType(value, type);
         if (dt == DateTime.MinValue)
         {
             return(dt);
         }
         //Milliseconst are not saved.
         if (dt.Ticks == DateTime.MaxValue.AddTicks(-9999999).Ticks)
         {
             return(DateTime.MaxValue);
         }
         dt = DateTime.SpecifyKind(dt, DateTimeKind.Utc);
         return(dt.ToLocalTime());
     }
     if (value.GetType() == type)
     {
         return(value);
     }
     if (type == typeof(Guid))
     {
         if (value is string)
         {
             Guid g = new Guid((string)value);
             return(g);
         }
     }
     else if (type.IsEnum)
     {
         if (value is string)
         {
             return(Enum.Parse(type, (string)value));
         }
         return(Enum.Parse(type, value.ToString()));
     }
     else if (type == typeof(System.Decimal))
     {
         if (Convert.ToDouble(value) == -7.9228162514264338E+28)
         {
             return(System.Decimal.MinValue);
         }
         if (Convert.ToDouble(value) == 7.9228162514264338E+28)
         {
             return(System.Decimal.MaxValue);
         }
         Convert.ToDecimal(value);
     }
     else if (type == typeof(Int64))
     {
         if (value is double)
         {
             if ((double)value == 9.2233720368547758E+18)
             {
                 return(Int64.MaxValue);
             }
         }
     }
     else if (type == typeof(UInt64))
     {
         if (value is double)
         {
             if ((double)value == 1.8446744073709552E+19)
             {
                 return(UInt64.MaxValue);
             }
         }
     }
     else if (type == typeof(TimeSpan))
     {
         return(new TimeSpan(Convert.ToInt64(value) * 10000));
     }
     else if (type == typeof(DateTimeOffset))
     {
         DateTime dt = (DateTime)Convert.ChangeType(value, typeof(DateTime));
         dt = DateTime.SpecifyKind(dt, DateTimeKind.Utc);
         return(new DateTimeOffset(dt.ToLocalTime()));
     }
     //If nullable.
     if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable <>))
     {
         if (value == null)
         {
             return(null);
         }
         Type tp = Nullable.GetUnderlyingType(type);
         //Date times are saved in UTC format.
         if (tp == typeof(DateTime))
         {
             DateTime dt = (DateTime)Convert.ChangeType(value, tp);
             dt = DateTime.SpecifyKind(dt, DateTimeKind.Utc);
             return(dt.ToLocalTime());
         }
         if (value.GetType() != tp)
         {
             return(ChangeType(value, tp));
         }
         return(value);
     }
     if (type.IsArray)
     {
         Type     pt    = GXInternal.GetPropertyType(type);
         string[] tmp   = ((string)value).Split(new char[] { ';' });
         Array    items = Array.CreateInstance(pt, tmp.Length);
         int      pos2  = -1;
         foreach (string it in tmp)
         {
             items.SetValue(GXInternal.ChangeType(it, pt), ++pos2);
         }
         return(items);
     }
     if (type == typeof(Type))
     {
         return(Type.GetType(value.ToString()));
     }
     return(Convert.ChangeType(value, type));
 }
Esempio n. 3
0
        /// <summary>
        /// Get serialized property and field values.
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        internal static IDictionary <string, GXSerializedItem> GetValues(Type type, bool sorted, UpdateAttributes attributeUpdater)
        {
            GXSerializedItem s;

            if (type.IsPrimitive || type == typeof(string) || type == typeof(Guid))
            {
                return(null);
            }
            IDictionary <string, GXSerializedItem> list;

            if (sorted)
            {
                list = new SortedDictionary <string, GXSerializedItem>(StringComparer.InvariantCultureIgnoreCase);
            }
            else
            {
                list = new Dictionary <string, GXSerializedItem>(StringComparer.InvariantCultureIgnoreCase);
            }
            bool         all = type.GetCustomAttributes(typeof(DataContractAttribute), true).Length == 0;
            BindingFlags flags;

            //If DataContractAttribute is not set get only public property values.
            if (all)
            {
                flags = BindingFlags.Instance | BindingFlags.Public;
            }
            else
            {
                flags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
            }
            //Save properties.
            foreach (PropertyInfo it in type.GetProperties(flags))
            {
                DataMemberAttribute[] attr = (DataMemberAttribute[])it.GetCustomAttributes(typeof(DataMemberAttribute), true);
                //If value is not marked as ignored.
                if (((all && it.CanWrite) || attr.Length != 0) &&
                    it.GetCustomAttributes(typeof(IgnoreDataMemberAttribute), true).Length == 0)
                {
                    if (!list.ContainsKey(it.Name) && it.Name != "Capacity")
                    {
                        s        = new GXSerializedItem();
                        s.Target = it;
                        s.Type   = it.PropertyType;
                        string name;
                        if (attr.Length == 0 || string.IsNullOrEmpty(attr[0].Name))
                        {
                            name = it.Name;
                        }
                        else
                        {
                            name = attr[0].Name;
                        }
                        if (attributeUpdater != null)
                        {
                            attributeUpdater(type, it.GetCustomAttributes(true), s);
                        }
                        if ((s.Attributes & Attributes.Ignored) == 0)
                        {
                            if (!it.PropertyType.IsArray)
                            {
                                s.Get = GXInternal.CreateGetHandler(it.PropertyType, it);
                                s.Set = GXInternal.CreateSetHandler(it.PropertyType, it);
                            }
                            list.Add(name, s);
                        }
                    }
                }
            }
            if (!all)
            {
                //Save data members.
                foreach (FieldInfo it in type.GetFields(flags))
                {
                    DataMemberAttribute[] attr = (DataMemberAttribute[])it.GetCustomAttributes(typeof(DataMemberAttribute), true);
                    if (attr.Length != 0)
                    {
                        if (!list.ContainsKey(it.Name))
                        {
                            s        = new GXSerializedItem();
                            s.Target = it;
                            s.Type   = it.FieldType;
                            string name;
                            if (attr.Length == 0 || string.IsNullOrEmpty(attr[0].Name))
                            {
                                name = it.Name;
                            }
                            else
                            {
                                name = attr[0].Name;
                            }
                            if (attributeUpdater != null)
                            {
                                attributeUpdater(type, it.GetCustomAttributes(true), s);
                            }
                            if ((s.Attributes & Attributes.Ignored) == 0)
                            {
                                if (!it.FieldType.IsArray)
                                {
                                    s.Get = GXInternal.CreateGetHandler(it.FieldType, it);
                                    s.Set = GXInternal.CreateSetHandler(it.FieldType, it);
                                }
                                list.Add(name, s);
                            }
                        }
                    }
                }
            }
            return(list);
        }