Exemple #1
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.");
        }