Пример #1
0
        /// <summary>
        /// Packs the data and into a response object and sends it to the callback
        /// </summary>
        /// <typeparam name="T"></typeparam>
        private T PackAndSendResponse <T>(string rawData) where T : AsanaObject
        {
            var u = AsanaObject.Create(typeof(T));

            Parsing.Deserialize(GetDataAsDict(rawData), u, _host);
            return((T)u);
        }
Пример #2
0
        /// <summary>
        /// Packs the data and into a response object and sends it to the callback
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="callback"></param>
        internal void PackAndSendResponse <T>(string rawData, AsanaResponseEventHandler callback) where T : AsanaObject
        {
            var u = AsanaObject.Create(typeof(T));

            Parsing.Deserialize(GetDataAsDict(rawData), u, this);
            callback(u);
        }
Пример #3
0
        /// <summary>
        /// Creates a new T without requiring a public constructor
        /// </summary>
        /// <param name="t"></param>
        /// <param name="ID"></param>
        /// <returns></returns>
        internal static AsanaObject Create(Type t, Int64 ID)
        {
            AsanaObject o = Create(t);

            o.ID = ID;
            return(o);
        }
Пример #4
0
 static public Task Save(this AsanaObject obj, AsanaFunction function)
 {
     if (obj.Host == null)
     {
         throw new NullReferenceException("This AsanaObject does not have a host associated with it so you must specify one when saving.");
     }
     return(obj.Host.Save(obj, function));
 }
Пример #5
0
        private T PackOriginalContent <T>(string rawData) where T : AsanaObject
        {
            var data = Json.Deserialize(rawData) as Dictionary <string, object>;
            var u    = AsanaObject.Create(typeof(T));

            Parsing.Deserialize(data, u, _host);
            return((T)u);
        }
Пример #6
0
 public static Task Delete(this AsanaObject obj)
 {
     if (obj.Host == null)
     {
         throw new NullReferenceException("This AsanaObject does not have a host associated with it so you must specify one when saving.");
     }
     return(obj.Host.Delete(obj));
 }
Пример #7
0
 static public void Save(this AsanaObject obj)
 {
     if (obj.Host == null)
     {
         throw new NullReferenceException("This AsanaObject does not have a host associated with it so you must specify one when saving.");
     }
     obj.Host.Save(obj, null);
 }
Пример #8
0
 /// <summary>
 /// Creates a new T without requiring a public constructor
 /// </summary>
 /// <param name="t"></param>
 internal static AsanaObject Create(Type t)
 {
     try
     {
         AsanaObject o = (AsanaObject)Activator.CreateInstance(t, true);
         return(o);
     }
     catch (System.Exception)
     {
         return(null);
     }
 }
Пример #9
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="source"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        static private AsanaObject SafeAssignAsanaObject <T>(Dictionary <string, object> source, string name, Asana host) where T : AsanaObject
        {
            T value = null;

            if (source.ContainsKey(name) && source[name] != null)
            {
                var obj = source[name] as Dictionary <string, object>;
                value = (T)AsanaObject.Create(typeof(T));
                Parsing.Deserialize(obj, (value as AsanaObject), host);
            }

            return(value);
        }
Пример #10
0
        /// <summary>
        /// Packs the data and into a collection object and sends it to the callback
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="callback"></param>
        internal void PackAndSendResponseCollection <T>(string rawData, AsanaCollectionResponseEventHandler callback) where T : AsanaObject
        {
            var k = GetDataAsDictArray(rawData);
            AsanaObjectCollection collection = new AsanaObjectCollection();

            foreach (var j in k)
            {
                var t = AsanaObject.Create(typeof(T));
                Parsing.Deserialize(j, t, this);
                collection.Add(t);
            }
            callback(collection);
        }
Пример #11
0
        /// <summary>
        /// Packs the data and into a collection object and sends it to the callback
        /// </summary>
        /// <typeparam name="T"></typeparam>
        private IAsanaObjectCollection <T> PackAndSendResponseCollection <T>(string rawData) where T : AsanaObject
        {
            var k = GetDataAsDictArray(rawData);
            AsanaObjectCollection <T> collection = new AsanaObjectCollection <T>();

            foreach (var j in k)
            {
                var t = (T)AsanaObject.Create(typeof(T));
                Parsing.Deserialize(j, t, _host);
                collection.Add(t);
            }

            return(collection);
        }
Пример #12
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="source"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        static private T[] SafeAssignAsanaObjectArray <T>(Dictionary <string, object> source, string name, Asana host) where T : AsanaObject
        {
            T[] value = null;

            if (source.ContainsKey(name) && source[name] != null)
            {
                var list = source[name] as List <object>;

                value = new T[list.Count];
                for (int i = 0; i < list.Count; ++i)
                {
                    T newObj = (T)AsanaObject.Create(typeof(T));
                    Parsing.Deserialize(list[i] as Dictionary <string, object>, (newObj as AsanaObject), host);
                    value[i] = newObj;
                }
            }

            return(value);
        }
Пример #13
0
 static public void Save(this AsanaObject obj, Asana host, AsanaFunction function)
 {
     host.Save(obj, function);
 }
Пример #14
0
 public static Task Delete(this AsanaObject obj, Asana host)
 {
     return(host.Delete(obj));
 }
Пример #15
0
 public static Task Save(this AsanaObject obj, Asana host)
 {
     return(host.Save(obj, null));
 }
Пример #16
0
 public static Task Save(this AsanaObject obj, Asana host, AsanaFunction function)
 {
     return(host.Save(obj, function));
 }
Пример #17
0
        //static internal T SetValueOnBaseType<T>

        /// <summary>
        /// Deserializes a dictionary based on AsanaDataAttributes
        /// </summary>
        /// <param name="data"></param>
        /// <param name="obj"></param>
        static internal Dictionary <string, object> Serialize(AsanaObject obj, bool asString, bool dirtyOnly)
        {
            var dict = new Dictionary <string, object>();

            foreach (var p in obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance))
            {
                try
                {
                    var cas = p.GetCustomAttributes(typeof(AsanaDataAttribute), false);
                    if (cas.Length == 0)
                    {
                        continue;
                    }

                    AsanaDataAttribute ca = cas[0] as AsanaDataAttribute;

                    if (ca.Flags.HasFlag(SerializationFlags.Omit))
                    {
                        continue;
                    }

                    bool required = ca.Flags.HasFlag(SerializationFlags.Required);

                    object value = p.GetValue(obj, new object[] { });

                    if (dirtyOnly && !obj.IsDirty(ca.Name, value))
                    {
                        continue;
                    }

                    bool present = ValidateSerializableValue(ref value, ca, p);

                    if (present == false)
                    {
                        if (!required)
                        {
                            continue;
                        }
                        else
                        {
                            throw new MissingFieldException("Couldn't save object because it was missing a required field: " + p.Name);
                        }
                    }

                    if (value.GetType().IsArray)
                    {
                        int count = 0;
                        foreach (var x in (object[])value)
                        {
                            dict.Add(ca.Name + "[" + count + "]", asString ? x.ToString() : x);
                            count++;
                        }
                    }
                    else
                    {
                        dict.Add(ca.Name, asString ? value.ToString() : value);
                    }
                }
                catch (Exception)
                {
                }
            }

            return(dict);
        }
Пример #18
0
 static public void Save(this AsanaObject obj, Asana host)
 {
     host.Save(obj, null);
 }
Пример #19
0
        /// <summary>
        /// Deserializes a dictionary based on AsanaDataAttributes
        /// </summary>
        /// <param name="data"></param>
        /// <param name="obj"></param>
        static internal void Deserialize(Dictionary <string, object> data, AsanaObject obj, Asana host)
        {
            foreach (var p in obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance))
            {
                if (p.Name == "Host")
                {
                    if (obj.Host != host)
                    {
                        p.SetValue(obj, host, new object[] { });
                    }
                    continue;
                }

                try
                {
                    var cas = p.GetCustomAttributes(typeof(AsanaDataAttribute), false);
                    if (cas.Length == 0)
                    {
                        continue;
                    }

                    AsanaDataAttribute ca = cas[0] as AsanaDataAttribute;

                    if (!data.ContainsKey(ca.Name))
                    {
                        continue;
                    }

                    if (p.PropertyType == typeof(string))
                    {
                        p.SetValue(obj, SafeAssignString(data, ca.Name), null);
                    }
                    else
                    {
                        Type       t      = p.PropertyType.IsArray ? p.PropertyType.GetElementType() : p.PropertyType;
                        MethodInfo method = null;
                        if (typeof(AsanaObject).IsAssignableFrom(t))
                        {
                            method = typeof(Parsing).GetMethod(p.PropertyType.IsArray ? "SafeAssignAsanaObjectArray" : "SafeAssignAsanaObject", BindingFlags.NonPublic | BindingFlags.Static).MakeGenericMethod(new Type[] { t });
                        }
                        else
                        {
                            method = typeof(Parsing).GetMethod(p.PropertyType.IsArray ? "SafeAssignArray" : "SafeAssign", BindingFlags.NonPublic | BindingFlags.Static).MakeGenericMethod(new Type[] { t });
                        }

                        var methodResult = method.Invoke(null, new object[] { data, ca.Name, host });

                        // this check handle base-class properties
                        if (p.DeclaringType != obj.GetType())
                        {
                            var p2 = p.DeclaringType.GetProperty(p.Name);
                            p2.SetValue(obj, methodResult, null);
                        }
                        else
                        {
                            p.SetValue(obj, methodResult, null);
                        }
                    }
                }
                catch (Exception)
                {
                }
            }
        }