示例#1
0
        private void DisableReadonly(object value)
        {
            if (value == null)
            {
                return;
            }

            Type type = value.GetType();

            PropertyInfo[] propertyInfos = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);

            foreach (PropertyInfo pi in propertyInfos)
            {
                Type pType = pi.PropertyType;

                ParameterInfo[] parInfo = pi.GetIndexParameters(); //if represents a array
                if (parInfo != null && parInfo.Length > 0)
                {
                    continue;
                }

                //Set Read Only
                ReadOnlyAttribute[] attribute2 = pi.GetCustomAttributes(typeof(ReadOnlyAttribute), false) as ReadOnlyAttribute[];
                if (attribute2 != null && attribute2.Length > 0)
                {
                    FieldInfo ftoChange = attribute2[0].GetType().GetField("isReadOnly",
                                                                           BindingFlags.NonPublic |
                                                                           BindingFlags.Instance);
                    if (ftoChange != null)
                    {
                        ftoChange.SetValue(attribute2[0], false);
                    }
                }

                if (pType.IsPrimitive || pType.IsValueType || pType == typeof(string)) //enums are value types too
                {
                    return;
                }
                else
                {
                    //go deeper
                    object pValue = pi.GetValue(value, null);

                    if (SettingsConverter.IsListType(pType))
                    {
                        IList pList = (IList)pValue;

                        foreach (object pListValue in pList)
                        {
                            DisableReadonly(pList);
                        }
                    }
                    else
                    {
                        DisableReadonly(pValue);
                    }
                }
            }
        }