예제 #1
0
 static int GetClassInfo(IntPtr L)
 {
     try
     {
         ToLua.CheckArgsCount(L, 1);
         System.Type arg0           = ToLua.CheckMonoType(L, 1);
         LuaPerfect.ClassInfoItem o = LuaPerfect.ObjectFormater.GetClassInfo(arg0);
         ToLua.PushObject(L, o);
         return(1);
     }
     catch (Exception e)
     {
         return(LuaDLL.toluaL_exception(L, e));
     }
 }
예제 #2
0
        public static ClassInfoItem GetClassInfo(Type type)
        {
            ClassInfoItem classInfoItem;

            if (s_classInfoDirectionary.TryGetValue(type, out classInfoItem))
            {
                return(classInfoItem);
            }
            else
            {
                classInfoItem                 = new ClassInfoItem();
                classInfoItem.fullName        = GetClassFullName(type);
                classInfoItem.fullName2       = String.Format("<C# Object> ({0})", classInfoItem.fullName);
                classInfoItem.fields          = GetAllFields(type);
                classInfoItem.properties      = GetAllProperties(type);
                s_classInfoDirectionary[type] = classInfoItem;
                return(classInfoItem);
            }
        }
예제 #3
0
        public static ObjectItem InnerFormatObject(System.Object obj, bool collectChildren)
        {
            ObjectItem objectItem = new ObjectItem();

            if (obj == null)
            {
                objectItem.SetValue("null");
                return(objectItem);
            }
            Type type = obj.GetType();

            if (IsBasicType(type))
            {
                if (type == typeof(System.Boolean))
                {
                    objectItem.SetValue((bool)obj ? "true" : "false");
                }
                else if (type == typeof(System.String))
                {
                    objectItem.SetValue("\"" + (string)obj + "\"");
                }
                else
                {
                    objectItem.SetValue(obj.ToString());
                }
            }
            else if (type.IsEnum)
            {
                objectItem.SetValue(obj.ToString());
            }
            else if (type == typeof(ArrayList))
            {
                ArrayList arrayList = (ArrayList)obj;
                objectItem.SetValue(String.Format("<C# ArrayList> Count = {0}", arrayList.Count));
                if (collectChildren)
                {
                    int i = 0;
                    foreach (object obj1 in arrayList)
                    {
                        string name = i.ToString();
                        objectItem.AddChild(name, obj1);
                        i++;
                    }
                }
            }
            else if (type == typeof(Hashtable))
            {
                Hashtable hashtable = (Hashtable)obj;
                objectItem.SetValue(String.Format("<C# Hashtable> Count = {0}", hashtable.Count));
                if (collectChildren)
                {
                    foreach (object key in hashtable.Keys)
                    {
                        string name = key.ToString();
                        objectItem.AddChild(name, hashtable[key]);
                    }
                }
            }
            else if (type.IsArray)
            {
                Array array = (Array)obj;
                objectItem.SetValue(String.Format("<C# Array> Length = {0}", array.Length));
                if (collectChildren)
                {
                    int i = 0;
                    foreach (object obj1 in array)
                    {
                        string name = i.ToString();
                        objectItem.AddChild(name, obj1);
                        i++;
                    }
                }
            }
            else
            {
                ClassInfoItem classInfoItem = GetClassInfo(type);
                if (classInfoItem != null)
                {
                    objectItem.SetValue(classInfoItem.fullName2);
                    if (collectChildren)
                    {
                        if (type == typeof(GameObject))
                        {
                            GameObject  gameObject = (GameObject)obj;
                            Component[] components = gameObject.GetComponents(typeof(Component));
                            int         i          = 0;
                            foreach (Component component in components)
                            {
                                Type   componentType = component.GetType();
                                bool   isScript      = componentType.IsSubclassOf(typeof(MonoBehaviour));
                                string name          = String.Format("Component_{0}_{1}{2}", i, componentType.Name, isScript ? "_Script" : "");
                                objectItem.AddChild(name, component);
                                i++;
                            }
                            List <GameObject> children = GetChildrenGameObjects(gameObject);
                            i = 0;
                            foreach (GameObject childGameObject in children)
                            {
                                string name = String.Format("Child_{0}_{1}", i, childGameObject.name);
                                objectItem.AddChild(name, childGameObject);
                                i++;
                            }
                        }
                        foreach (FieldInfo field in classInfoItem.fields)
                        {
                            object value = field.GetValue(obj);
                            string name  = field.Name;
                            objectItem.AddChild(name, value);
                        }
                        foreach (PropertyInfo property in classInfoItem.properties)
                        {
                            object value = null;
                            try
                            {
                                value = property.GetValue(obj, null);
                            }
                            catch (Exception)
                            {
                            }
                            string name = property.Name;
                            objectItem.AddChild(name, value);
                        }
                    }
                }
            }
            return(objectItem);
        }