コード例 #1
0
        public static T ToObject <T> (this PFObject pfObj) where T : new ()
        {
            T   obj  = new T();
            var type = obj.GetType();

            var keys = pfObj.AllKeys;

            foreach (var prop in type.GetProperties())
            {
                string keyName = Attribute.IsDefined(prop, typeof(DataMemberAttribute)) ? ((DataMemberAttribute)Attribute.GetCustomAttribute(prop, typeof(DataMemberAttribute))).Name : prop.Name;
                if (!keys.Contains(keyName) && keyName != "UpdatedAt" && keyName != "CreatedAt" && keyName != "ObjectId")
                {
                    continue;
                }
                if (keyName == "UpdatedAt")
                {
                    prop.SetValue(obj, (DateTime)(pfObj.UpdatedAt ?? DateTime.MinValue), null);
                    continue;
                }
                else if (keyName == "CreatedAt")
                {
                    prop.SetValue(obj, (DateTime)(pfObj.CreatedAt ?? DateTime.MinValue), null);
                    continue;
                }
                else if (keyName == "ObjectId")
                {
                    prop.SetValue(obj, pfObj.ObjectId, null);
                    continue;
                }
                if (prop.PropertyType == typeof(DateTime))
                {
                    prop.SetValue(obj, DateTime.ParseExact(pfObj.ObjectForKey(keyName).ToString(), "s", CultureInfo.InvariantCulture), null);
                }
                else if (prop.PropertyType.IsEnum)
                {
                    prop.SetValue(obj, Enum.Parse(prop.PropertyType, pfObj.ObjectForKey(keyName).ToString()), null);
                }
                else if (prop.PropertyType == typeof(String))
                {
                    prop.SetValue(obj, pfObj.ObjectForKey(keyName).ToString(), null);
                }
                else
                {
                    prop.SetValue(obj, Convert.ChangeType(pfObj.ObjectForKey(keyName).ToString(), prop.PropertyType), null);
                }
            }

            return(obj);
        }