Beispiel #1
0
        public static object ConvertObject(object obj, object defaultValue, Type destinationType)
        {
            object result = null;

            if (destinationType == typeof(Type))
            {
                if (obj is Type)
                {
                    result = obj;
                }
                else if (!String.IsNullOrEmpty(Convert.ToString(obj)))
                {
                    result = ScriptMgr.GetType(Convert.ToString(obj));
                }
            }
            else if (destinationType == typeof(Int32))
            {
                result = Convert.ToInt32(obj);
            }
            else if (destinationType == typeof(Nullable <Int32>))
            {
                result = new Nullable <Int32>(Convert.ToInt32(obj));
            }
            else if (destinationType == typeof(Int64))
            {
                result = Convert.ToInt64(obj);
            }
            else if (destinationType == typeof(eEmote))
            {
                if (obj is eEmote)
                {
                    result = (eEmote)obj;
                }
                else
                {
                    result = (eEmote)Enum.Parse(typeof(eEmote), Convert.ToString(obj), true);
                }
            }
            else if (destinationType == typeof(GameLiving))
            {
                result = QuestMgr.ResolveLiving(obj);
            }
            else if (destinationType == typeof(ItemTemplate))
            {
                if (obj is ItemTemplate)
                {
                    result = obj;
                }
                else
                {
                    result = GameServer.Database.FindObjectByKey <ItemTemplate>(Convert.ToString(obj));
                }
            }
            else if (destinationType == typeof(CustomDialogResponse))
            {
                result = (CustomDialogResponse)obj;
            }
            else if (destinationType == typeof(GameLocation))
            {
                result = (GameLocation)obj;
            }
            else if (destinationType == typeof(GameObject))
            {
                result = (GameObject)obj;
            }
            else if (destinationType == typeof(PathPoint))
            {
                result = (PathPoint)obj;
            }
            else
            {
                result = obj;
            }

            // handle default value if result == null
            if (result == null && defaultValue != null)
            {
                result = defaultValue;
            }

            return(result);
        }