Example #1
0
        public static MemberInfo GetMemberInfoRecursively(Type searchType, string path,
                                                          BindingFlags bindingFlags = BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
        {
            if (searchType == null)
            {
                return(null);
            }

            try
            {
                PathWrapper  pathWrapper       = new PathWrapper(path);
                MemberInfo   currentMemberInfo = null;
                MemberInfo[] memberInfoGroup;

                while (pathWrapper.CurrentPathPoint != null)
                {
                    memberInfoGroup = searchType.GetMember(pathWrapper.CurrentPathPoint, bindingFlags);
                    if (memberInfoGroup == null || memberInfoGroup.Length == 0)
                    {
                        return(null);
                    }
                    currentMemberInfo = memberInfoGroup[0];



                    if (currentMemberInfo is FieldInfo)
                    {
                        searchType = (currentMemberInfo as FieldInfo).FieldType;
                        if (searchType.IsGenericType)
                        {
                            searchType = searchType.GetGenericArguments()[0];
                            pathWrapper.Next(); //Skip "Array"
                            pathWrapper.Next(); //Skip "data[n]"
                        }
                    }
                    else
                    {
                        break;//Terminate iterating when encounter non-field member
                    }
                    pathWrapper.Next();
                }
                if (pathWrapper.Next())
                {
                    //Not finished yet
                    return(null);
                }

                return(currentMemberInfo);
            }
            catch (Exception e)
            {
                Debug.LogError(e.Message);
                return(null);
            }
        }
Example #2
0
        public static object GetFieldObjectRecursively(object target, string path)
        {
            if (target == null)
            {
                return(null);
            }

            Type searchType = target.GetType();

            if (searchType == null)
            {
                return(null);
            }

            try
            {
                PathWrapper pathWrapper      = new PathWrapper(path);
                FieldInfo   currentFieldInfo = null;

                while (pathWrapper.CurrentPathPoint != null)
                {
                    if (searchType.IsGenericType)
                    {
                        searchType = searchType.GetGenericArguments()[0];

                        pathWrapper.Next();//Skip "Array"
                        string ppoint = pathWrapper.CurrentPathPoint;
                        ppoint = ppoint.Replace("data[", "");
                        ppoint = ppoint.Replace("]", "");
                        int index = int.Parse(ppoint);
                        target = (target as IList)[index];
                        pathWrapper.Next();//Skip "data[n]"

                        if (string.IsNullOrEmpty(pathWrapper.CurrentPathPoint))
                        {
                            return(target);
                        }
                        else
                        {
                            currentFieldInfo = searchType.GetField(pathWrapper.CurrentPathPoint);
                            if (currentFieldInfo == null)
                            {
                                return(null);
                            }

                            target = currentFieldInfo.GetValue((target as IList)[index]);
                        }
                    }
                    else
                    {
                        currentFieldInfo = searchType.GetField(pathWrapper.CurrentPathPoint);
                        if (currentFieldInfo == null)
                        {
                            return(null);
                        }

                        target = currentFieldInfo.GetValue(target);
                    }

                    if (target == null)
                    {
                        return(null);
                    }

                    searchType = target.GetType();

                    pathWrapper.Next();
                }

                return(target);
            }
            catch (Exception e)
            {
                Debug.LogError(e.Message);
                return(null);
            }
        }