示例#1
0
        private HashSet <Type> GetCustomProperties()
        {
            var list = new HashSet <Type>();

            // get all types
            var types = BindingEditorUtility.GetScriptTypeList();

            foreach (var type in types)
            {
                if (!type.IsClass)
                {
                    continue;
                }

                // check all properties
                var properties = type.GetProperties();
                foreach (var property in properties)
                {
                    // check if the BindablePropertyAttribute is defined
                    if (!property.IsDefined(typeof(BindablePropertyAttribute), false))
                    {
                        continue;
                    }

                    var aotType = GetAotType(property.PropertyType);
                    if (!list.Contains(aotType))
                    {
                        // add it
                        list.Add(aotType);
                    }
                }
            }

            return(list);
        }
示例#2
0
        public void GenerateCode()
        {
            buildArgs = new BuildArgs();

            // load target type
            var classType = BindingEditorUtility.GetClassByName(settings.className).FirstOrDefault();

            if (classType == null)
            {
                Debug.LogErrorFormat("Invalid class name {0}", settings.className);
                return;
            }

            // set target class
            buildArgs.type = classType;

            // get bindable properties
            var outputList = CodeTool.GetBindableProperties();

            if (outputList == null)
            {
                Debug.LogError("Get bindable properties failed.");
                return;
            }

            SetupBuildArgs(outputList);

            WriteCode();
        }
示例#3
0
        public void LoadClass()
        {
            if (string.IsNullOrEmpty(typeName))
            {
                Debug.LogError("Type name is null");
                return;
            }

            // reset config
            buildArgs = null;

            var typeList = BindingEditorUtility.GetClassByName(typeName);

            if (typeList.Count == 0)
            {
                Debug.LogErrorFormat("Failed to get type {0}", typeName);
                return;
            }
            else if (typeList.Count > 1)
            {
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < typeList.Count; i++)
                {
                    sb.Append(typeList[i].FullName);
                    sb.Append(",\n");
                }

                Debug.LogErrorFormat("{0} types are found for name {1}, type list:\n{2}You need input more specific name.", typeList.Count, typeName, sb.ToString());
                return;
            }

            // get the only one matched
            var type = typeList.First();

            if (type == null)
            {
                Debug.LogErrorFormat("Failed to get type {0}", typeName);
                return;
            }

            // check type
            if (!IsBindableType(type))
            {
                Debug.LogErrorFormat("Class {0} is not sub class of BindableClass", type.Name);
                return;
            }

            // create new config
            buildArgs      = new BuildArgs();
            buildArgs.type = type;

            // get class info
            classInfo = GetClassInfo(type);

            UpdateMemberList();
        }
示例#4
0
        private static void CacheEnumType()
        {
            if (cachedTypeList != null)
            {
                return;
            }

            var types = BindingEditorUtility.GetAllTypeList();

            cachedTypeList = types.Where(x => x.IsEnum).ToList();
        }
        public void LoadTypeList()
        {
            // create build args
            buildArgs = new BuildArgs();

            // get types
            var types = BindingEditorUtility.GetImplicitOperatorTypes(true);

            foreach (var item in types)
            {
                var newItem = new BuildArgs.Item()
                {
                    type = item,
                };

                // add it
                buildArgs.itemList.Add(newItem);
            }
        }
示例#6
0
        public void UpdateMemberList()
        {
            if (buildArgs == null)
            {
                return;
            }

            // clear member list
            buildArgs.memberList.Clear();

            var type = buildArgs.type;

            var flags = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.DeclaredOnly;

            if (includePublic)
            {
                // add public
                flags |= BindingFlags.Public;
            }

            // get all fields
            var fields = type.GetFields(flags).Where(x => !x.Name.EndsWith("k__BackingField"));

            foreach (var item in fields)
            {
                // create member
                var member = new BuildArgs.Item()
                {
                    type       = item.FieldType,
                    name       = item.Name,
                    memberType = MemberTypes.Field,
                };

                member.propertyName = BindingEditorUtility.GetPropertyName(item.Name);

                // add it
                buildArgs.memberList.Add(member);
            }

            if (!includeProperty)
            {
                return;
            }

            // get all properties
            var properties = type.GetProperties(flags);

            foreach (var item in properties)
            {
                // create member
                var member = new BuildArgs.Item()
                {
                    type       = item.PropertyType,
                    name       = item.Name,
                    memberType = MemberTypes.Property,
                };

                member.propertyName = BindingEditorUtility.GetPropertyName(item.Name);

                // add it
                buildArgs.memberList.Add(member);
            }
        }
示例#7
0
        public static List <Type> GetImplicitOperatorTypes(bool twoWay)
        {
            var dictionary = new Dictionary <Tuple <Type, Type>, MethodInfo>();

            // search all types
            var types = BindingEditorUtility.GetAllTypeList();

            foreach (var type in types)
            {
                if (!type.IsPublic)
                {
                    continue;
                }

                if (type.IsGenericTypeDefinition)
                {
                    continue;
                }

                var methods = type.GetMethods(BindingFlags.Public | BindingFlags.Static);

                foreach (var method in methods)
                {
                    if (method.Name != "op_Implicit")
                    {
                        continue;
                    }

                    var parameters = method.GetParameters();
                    if (parameters.Length != 1)
                    {
                        // with 1 parameter
                        continue;
                    }

                    var parameter = parameters[0];

                    // add operator
                    var key = Tuple.Create(parameter.ParameterType, method.ReturnType);
                    dictionary.Add(key, method);
                }
            }

            var resultList = new List <Type>();

            foreach (var item in dictionary)
            {
                var key = item.Key;

                if (twoWay)
                {
                    var pairedKey = Tuple.Create(key.Item2, key.Item1);

                    // check convert back
                    MethodInfo convertBackMethod = null;
                    dictionary.TryGetValue(pairedKey, out convertBackMethod);

                    if (convertBackMethod == null)
                    {
                        // skip one-way
                        continue;
                    }
                }

                var type = item.Value.DeclaringType;

                if (!resultList.Contains(type))
                {
                    // add current type
                    resultList.Add(type);
                }
            }

            return(resultList);
        }
示例#8
0
        private void SetupBuildArgs(List <KeyValuePair <string, string> > nameList)
        {
            // add common types
            var commonTypeList  = GetCommonTypes();
            var propertyTypeSet = new HashSet <Type>(commonTypeList);

            // load type
            foreach (var item in nameList)
            {
                // convert to .net type
                var typeName = item.Key.Replace('/', '+');

                // get type
                var type = BindingEditorUtility.GetClassByName(typeName).FirstOrDefault();
                if (type == null)
                {
                    Debug.LogWarningFormat("Invalid type name {0}", typeName);
                    continue;
                }

                // get property
                var property = type.GetProperty(item.Value);
                if (property == null)
                {
                    Debug.LogWarningFormat("Invalid property name {0}", item.Value);
                    continue;
                }

                var aotType = GetAotType(property.PropertyType);
                if (!propertyTypeSet.Contains(aotType))
                {
                    // add it
                    propertyTypeSet.Add(aotType);
                }
            }

            // combine with custom properties
            Combine(propertyTypeSet, GetCustomProperties());

            // sort type set
            var sortedList = GetSortedTypeList(propertyTypeSet);

            // set property list
            foreach (var item in sortedList)
            {
                var newItem = new BuildArgs.PropertyItem()
                {
                    propertyType = item,
                };

                buildArgs.propertyList.Add(newItem);
            }

            // get implicit operators
            var types = ImplicitConverter.GetTypes();
            var operatorDictionary = BindingUtility.GetImplicitOperators(types);

            // set implicit operator list
            foreach (var item in operatorDictionary)
            {
                var newItem = new BuildArgs.ImplicitOperatorItem()
                {
                    sourceType = item.Key.Item1,
                    targetType = item.Key.Item2,
                };

                buildArgs.implicitOperatorList.Add(newItem);
            }
        }