コード例 #1
0
        T GetReturnValue <T> (ScriptParmCategory parmCategory, object result)
        {
            Type type = typeof(T);

            if (result == null)
            {
                return(default(T));
            }

            else if (parmCategory == ScriptParmCategory.ScriptObjectCollection)
            {
                var objArray = result as object[];
                if (objArray == null)
                {
                    return(default(T));
                }

                try
                {
                    var array = Array.CreateInstance(type.GetGenericArguments()[0], objArray.Length);
                    for (int a = 0; a < array.Length; a++)
                    {
                        array.SetValue(Activator.CreateInstance(type.GetGenericArguments()[0], BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, new object[] { objArray[a] }, null), a);
                    }
                    return((T)Activator.CreateInstance(type, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, new object[] { array }, null));
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    return(default(T));
                }
            }
            else if (parmCategory == ScriptParmCategory.ScriptableType)
            {
                return(ScriptObjectHelper.AnonymousObjectToScriptableType <T>(result));
            }
            else if (parmCategory == ScriptParmCategory.ScriptableTypeArray)
            {
                try
                {
                    var objArray  = result as object[];
                    var arrayType = type.GetElementType();
                    var array     = Array.CreateInstance(arrayType, objArray.Length);
                    for (int a = 0; a < array.Length; a++)
                    {
                        array.SetValue(ScriptObjectHelper.AnonymousObjectToScriptableType(arrayType, objArray[a]), a);
                    }
                    return((T)(object)array);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                return((T)Activator.CreateInstance(type, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, new object[] { result }, null));
            }
            else
            {
                return((T)Activator.CreateInstance(type, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, new object[] { result }, null));
            }
        }
コード例 #2
0
        internal static object MapToType(ScriptParmCategory category, int arrayFlag, object toMap, Type type = null)
        {
            if (toMap == null)
            {
                return(null);
            }

            switch (category)
            {
            case ScriptParmCategory.ScriptableType:
                return(ScriptObjectHelper.AnonymousObjectToScriptableType(type, toMap));

            case ScriptParmCategory.ScriptObject:
                return(ScriptObjectHelper.AnonymousObjectToScriptObjectProxy(toMap));

            default:
                return(toMap);
            }
        }
コード例 #3
0
        internal static object MapToType(ScriptParmCategory category, int arrayFlag, object toMap, Type type = null, Action preventDefaultAction = null)
        {
            if (toMap == null)
            {
                return(null);
            }

            switch (category)
            {
            case ScriptParmCategory.ScriptableType:
                return(ScriptObjectHelper.AnonymousObjectToScriptableType(type, toMap, preventDefaultAction));

            case ScriptParmCategory.ScriptObject:
                return(ScriptObjectHelper.AnonymousObjectToScriptObjectType(type, toMap));

            case ScriptParmCategory.ScriptableTypeArray:
                try
                {
                    var objArray  = toMap as object[];
                    var arrayType = type.GetElementType();
                    var array     = Array.CreateInstance(arrayType, objArray.Length);
                    for (int a = 0; a < array.Length; a++)
                    {
                        array.SetValue(ScriptObjectHelper.AnonymousObjectToScriptableType(arrayType, objArray[a]), a);
                    }
                    return(array);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                return(toMap);

            default:
                return(toMap);
            }
        }
コード例 #4
0
        public static bool DictionaryToScriptableType(IDictionary <string, object> parm, object obj)
        {
            var parmType = obj.GetType();
            var success  = false;

            if (parmType.IsAttributeDefined <ScriptableTypeAttribute>(false))
            {
                var mappings = GetScriptMemberMappings(parmType, ScriptMemberMappingDirection.ScriptAliasToMember);

                foreach (var key in parm.Keys)
                {
                    if (mappings.ContainsKey(key))
                    {
                        var pi = parmType.GetProperty(mappings[key]);
                        if (pi.SetMethod != null)
                        {
                            if (pi.PropertyType.IsEnum)
                            {
                                // Try to map enum string values to their enum types.
                                var lowerKey = parm[key].ToString().ToLower();
                                foreach (var ev in Enum.GetNames(pi.PropertyType))
                                {
                                    if (ev.ToLower() == lowerKey)
                                    {
                                        pi.SetValue(obj, Enum.Parse(pi.PropertyType, ev));
                                    }
                                }
                            }
                            else
                            {
                                var scriptObject = parm[key] as IDictionary <string, object>;
                                if (scriptObject != null)
                                {
                                    if (scriptObject.ContainsKey("websharp_id"))
                                    {
                                        pi.SetValue(obj, AnonymousObjectToScriptObjectType(pi.PropertyType, parm[key]));
                                    }
                                    else
                                    {
                                        pi.SetValue(obj, AnonymousObjectToScriptableType(pi.PropertyType, parm[key]));
                                    }
                                }
                                else
                                {
                                    if (ScriptObjectUtilities.IsTypeScriptableTypeArray(pi.PropertyType))
                                    {
                                        var objArray = parm[key] as object[];
                                        if (objArray != null)
                                        {
                                            var arrayType = pi.PropertyType.GetElementType();
                                            var array     = Array.CreateInstance(arrayType, objArray.Length);
                                            for (int a = 0; a < array.Length; a++)
                                            {
                                                array.SetValue(ScriptObjectHelper.AnonymousObjectToScriptableType(arrayType, objArray[a]), a);
                                            }
                                            pi.SetValue(obj, array);
                                        }
                                    }
                                    else
                                    {
                                        pi.SetValue(obj, parm[key]);
                                    }
                                }
                            }
                            success = true;
                        }
                    }
                }
            }
            return(success);
        }