Exemplo n.º 1
0
        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);
        }
Exemplo n.º 2
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.");
        }