Exemple #1
0
        public static string ParseForKeywords(object o, string valstr, bool literal, out Type ptype)
        {
            ptype = (Type)null;
            if (valstr == null || valstr.Length <= 0)
            {
                return((string)null);
            }
            string str1 = valstr.Trim();

            string[] strArray1 = PropertyTest.ParseString(str1, 2, "[");
            string   str2      = (string)null;

            if (strArray1.Length > 1)
            {
                str2 = PropertyTest.ParseToMatchingParen(strArray1[1], '[', ']')[0];
            }
            string[] strArray2 = strArray1[0].Trim().Split(',');
            if (str2 != null && str2.Length > 0 && (strArray2 != null && strArray2.Length > 0))
            {
                strArray2[strArray2.Length - 1] = str2;
            }
            string name = strArray2[0].Trim();
            char   ch   = str1[0];

            if ((int)ch == 46 || (int)ch == 45 || (int)ch == 43 || (int)ch >= 48 && (int)ch <= 57)
            {
                ptype = str1.IndexOf(".") < 0 ? typeof(int) : typeof(double);
                return(str1);
            }
            if ((int)ch == 34 || (int)ch == 40)
            {
                ptype = typeof(string);
                return(str1);
            }
            if ((int)ch == 35)
            {
                ptype = typeof(string);
                return(str1.Substring(1));
            }
            if (str1.ToLower() == "true" || str1.ToLower() == "false")
            {
                ptype = typeof(bool);
                return(str1);
            }
            if (!literal)
            {
                return(PropertyTest.ParseGetValue(PropertyTest.GetPropertyValue(o, name, out ptype), ptype));
            }
            ptype = typeof(string);
            return(str1);
        }
        public static string ParseForKeywords(object o, string valstr, bool literal, out Type ptype)
        {
            ptype = null;
            if (valstr == null || valstr.Length <= 0)
            {
                return(null);
            }
            string str = valstr.Trim();

            string[] strArrays     = PropertyTest.ParseString(str, 2, "[");
            string[] matchingParen = null;
            string   str1          = null;

            if ((int)strArrays.Length > 1)
            {
                matchingParen = PropertyTest.ParseToMatchingParen(strArrays[1], '[', ']');
                str1          = matchingParen[0];
            }
            string[] strArrays1 = strArrays[0].Trim().Split(new char[] { ',' });
            if (str1 != null && str1.Length > 0 && strArrays1 != null && (int)strArrays1.Length > 0)
            {
                strArrays1[(int)strArrays1.Length - 1] = str1;
            }
            string str2 = strArrays1[0].Trim();
            char   chr  = str[0];

            if (chr == '.' || chr == '-' || chr == '+' || chr >= '0' && chr <= '9')
            {
                if (str.IndexOf(".") < 0)
                {
                    ptype = typeof(int);
                }
                else
                {
                    ptype = typeof(double);
                }
                return(str);
            }
            if (chr == '\"' || chr == '(')
            {
                ptype = typeof(string);
                return(str);
            }
            if (chr == '#')
            {
                ptype = typeof(string);
                return(str.Substring(1));
            }
            if (str.ToLower() == "true" || str.ToLower() == "false")
            {
                ptype = typeof(bool);
                return(str);
            }
            if (literal)
            {
                ptype = typeof(string);
                return(str);
            }
            string propertyValue = PropertyTest.GetPropertyValue(o, str2, out ptype);

            return(PropertyTest.ParseGetValue(propertyValue, ptype));
        }
        public static string GetPropertyValue(object o, string name, out Type ptype)
        {
            string propertyValue;

            PropertyInfo[] propertyInfoArray;
            int            num;

            ptype = null;
            if (o == null || name == null)
            {
                return(null);
            }
            Type   type  = o.GetType();
            object value = null;

            PropertyInfo[] properties = type.GetProperties(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public);
            string[]       strArrays  = PropertyTest.ParseString(name, 2, ".");
            string         str        = strArrays[0];

            PropertyTest.ParseString(str, 4, ",");
            string str1 = strArrays[0];

            char[]   chrArray   = new char[] { '[' };
            string[] strArrays1 = str1.Split(chrArray);
            int      num1       = 0;

            if ((int)strArrays1.Length > 1)
            {
                str = strArrays1[0];
                string str2 = strArrays1[1];
                chrArray = new char[] { ']' };
                string[] strArrays2 = str2.Split(chrArray);
                if ((int)strArrays2.Length > 0)
                {
                    try
                    {
                        num1 = int.Parse(strArrays2[0]);
                    }
                    catch
                    {
                    }
                }
            }
            if ((int)strArrays.Length != 2)
            {
                PropertyInfo propertyInfo = PropertyTest.LookupPropertyInfo(type, str);
                if (propertyInfo != null)
                {
                    if (!propertyInfo.CanRead)
                    {
                        return("Property is write only.");
                    }
                    ptype = propertyInfo.PropertyType;
                    return(PropertyTest.InternalGetValue(o, propertyInfo, num1));
                }
                propertyInfoArray = properties;
                num = 0;
                while (num < (int)propertyInfoArray.Length)
                {
                    PropertyInfo propertyInfo1 = propertyInfoArray[num];
                    if (!PropertyTest.Insensitive.Equals(propertyInfo1.Name, str))
                    {
                        num++;
                    }
                    else if (propertyInfo1.CanRead)
                    {
                        ptype         = propertyInfo1.PropertyType;
                        propertyValue = PropertyTest.InternalGetValue(o, propertyInfo1, num1);
                        return(propertyValue);
                    }
                    else
                    {
                        propertyValue = "Property is write only.";
                        return(propertyValue);
                    }
                }
                return("Property not found.");
            }
            else
            {
                PropertyInfo propertyInfo2 = PropertyTest.LookupPropertyInfo(type, str);
                if (propertyInfo2 != null)
                {
                    if (!propertyInfo2.CanWrite)
                    {
                        return("Property is read only.");
                    }
                    ptype = propertyInfo2.PropertyType;
                    if (!ptype.IsArray)
                    {
                        value = propertyInfo2.GetValue(o, null);
                    }
                    else
                    {
                        try
                        {
                            object value1     = propertyInfo2.GetValue(o, null);
                            int    lowerBound = ((Array)value1).GetLowerBound(0);
                            int    upperBound = ((Array)value1).GetUpperBound(0);
                            if (num1 <= lowerBound && num1 <= upperBound)
                            {
                                value = ((Array)value1).GetValue(num1);
                            }
                        }
                        catch
                        {
                        }
                    }
                    return(PropertyTest.GetPropertyValue(value, strArrays[1], out ptype));
                }
                propertyInfoArray = properties;
                num = 0;
                while (num < (int)propertyInfoArray.Length)
                {
                    PropertyInfo propertyInfo3 = propertyInfoArray[num];
                    if (!PropertyTest.Insensitive.Equals(propertyInfo3.Name, str))
                    {
                        num++;
                    }
                    else if (propertyInfo3.CanWrite)
                    {
                        ptype = propertyInfo3.PropertyType;
                        if (!ptype.IsArray)
                        {
                            value = propertyInfo3.GetValue(o, null);
                        }
                        else
                        {
                            try
                            {
                                object value2      = propertyInfo3.GetValue(o, null);
                                int    lowerBound1 = ((Array)value2).GetLowerBound(0);
                                int    upperBound1 = ((Array)value2).GetUpperBound(0);
                                if (num1 <= lowerBound1 && num1 <= upperBound1)
                                {
                                    value = ((Array)value2).GetValue(num1);
                                }
                            }
                            catch
                            {
                            }
                        }
                        propertyValue = PropertyTest.GetPropertyValue(value, strArrays[1], out ptype);
                        return(propertyValue);
                    }
                    else
                    {
                        propertyValue = "Property is read only.";
                        return(propertyValue);
                    }
                }
                return("Property not found.");
            }
            return(propertyValue);
        }
Exemple #4
0
        public static string GetPropertyValue(object o, string name, out Type ptype)
        {
            ptype = (Type)null;
            if (o == null || name == null)
            {
                return((string)null);
            }
            Type   type = o.GetType();
            object o1   = (object)null;

            PropertyInfo[] properties = type.GetProperties(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public);
            string[]       strArray1  = PropertyTest.ParseString(name, 2, ".");
            string         str        = strArray1[0];

            PropertyTest.ParseString(str, 4, ",");
            string[] strArray2 = strArray1[0].Split('[');
            int      index     = 0;

            if (strArray2.Length > 1)
            {
                str = strArray2[0];
                string[] strArray3 = strArray2[1].Split(']');
                if (strArray3.Length > 0)
                {
                    try
                    {
                        index = int.Parse(strArray3[0]);
                    }
                    catch
                    {
                    }
                }
            }
            if (strArray1.Length == 2)
            {
                PropertyInfo propertyInfo1 = PropertyTest.LookupPropertyInfo(type, str);
                if (propertyInfo1 != null)
                {
                    if (!propertyInfo1.CanWrite)
                    {
                        return("Property is read only.");
                    }
                    ptype = propertyInfo1.PropertyType;
                    if (ptype.IsArray)
                    {
                        try
                        {
                            object obj        = propertyInfo1.GetValue(o, (object[])null);
                            int    lowerBound = ((Array)obj).GetLowerBound(0);
                            int    upperBound = ((Array)obj).GetUpperBound(0);
                            if (index <= lowerBound)
                            {
                                if (index <= upperBound)
                                {
                                    o1 = ((Array)obj).GetValue(index);
                                }
                            }
                        }
                        catch
                        {
                        }
                    }
                    else
                    {
                        o1 = propertyInfo1.GetValue(o, (object[])null);
                    }
                    return(PropertyTest.GetPropertyValue(o1, strArray1[1], out ptype));
                }
                foreach (PropertyInfo propertyInfo2 in properties)
                {
                    if (PropertyTest.Insensitive.Equals(propertyInfo2.Name, str))
                    {
                        if (!propertyInfo2.CanWrite)
                        {
                            return("Property is read only.");
                        }
                        ptype = propertyInfo2.PropertyType;
                        if (ptype.IsArray)
                        {
                            try
                            {
                                object obj        = propertyInfo2.GetValue(o, (object[])null);
                                int    lowerBound = ((Array)obj).GetLowerBound(0);
                                int    upperBound = ((Array)obj).GetUpperBound(0);
                                if (index <= lowerBound)
                                {
                                    if (index <= upperBound)
                                    {
                                        o1 = ((Array)obj).GetValue(index);
                                    }
                                }
                            }
                            catch
                            {
                            }
                        }
                        else
                        {
                            o1 = propertyInfo2.GetValue(o, (object[])null);
                        }
                        return(PropertyTest.GetPropertyValue(o1, strArray1[1], out ptype));
                    }
                }
            }
            else
            {
                PropertyInfo p1 = PropertyTest.LookupPropertyInfo(type, str);
                if (p1 != null)
                {
                    if (!p1.CanRead)
                    {
                        return("Property is write only.");
                    }
                    ptype = p1.PropertyType;
                    return(PropertyTest.InternalGetValue(o, p1, index));
                }
                foreach (PropertyInfo p2 in properties)
                {
                    if (PropertyTest.Insensitive.Equals(p2.Name, str))
                    {
                        if (!p2.CanRead)
                        {
                            return("Property is write only.");
                        }
                        ptype = p2.PropertyType;
                        return(PropertyTest.InternalGetValue(o, p2, index));
                    }
                }
            }
            return("Property not found.");
        }