예제 #1
0
        private void FillConfigWithAttribValue(Object config, XmlAttribute xmlAttrib)
        {
            Type type = config.GetType();

            PropertyInfo[] objProps = type.GetProperties();

            if (objProps != null)
            {
                for (int i = 0; i < objProps.Length; i++)
                {
                    PropertyInfo propInfo      = objProps[i];
                    Object[]     customAttribs = propInfo.GetCustomAttributes(typeof(ConfigurationAttributeAttribute), false);
                    if (customAttribs != null && customAttribs.Length > 0)
                    {
                        ConfigurationAttributeAttribute configAttrib = customAttribs[0] as ConfigurationAttributeAttribute;
                        try
                        {
                            if (configAttrib != null && String.Compare(configAttrib.AttributeName, xmlAttrib.Name, true) == 0)
                            {
                                propInfo.SetValue(config, ConvertToPrimitive(propInfo.PropertyType, xmlAttrib.Value, configAttrib.AppendedText), null);
                            }
                        }
                        catch (Exception e)
                        {
                            throw new Exception("Can not set the value for attribute " + configAttrib.AttributeName + " Errror :" + e.ToString());
                        }
                    }
                }
            }
        }
예제 #2
0
        private string GetSectionXml(Object configSection, string sectionName, int indent)
        {
            string endStr = "\r\n";
            string preStr = "".PadRight(indent * 2);

            StringBuilder sb   = new StringBuilder(preStr + "<" + sectionName);
            Type          type = configSection.GetType();

            PropertyInfo[] propertiesInfo = type.GetProperties();

            if (propertiesInfo != null && propertiesInfo.Length > 0)
            {
                for (int i = 0; i < propertiesInfo.Length; i++)
                {
                    PropertyInfo property      = propertiesInfo[i];
                    object[]     customAttribs = property.GetCustomAttributes(true);

                    if (customAttribs != null && customAttribs.Length > 0)
                    {
                        for (int j = 0; j < customAttribs.Length; j++)
                        {
                            ConfigurationAttributeAttribute attrib = customAttribs[j] as ConfigurationAttributeAttribute;
                            if (attrib != null)
                            {
                                Object propertyValue = property.GetValue(configSection, null);
                                string appendedText  = attrib.AppendedText != null ? attrib.AppendedText : "";
                                if (propertyValue != null)
                                {
                                    sb.Append(" " + attrib.AttributeName + "=\"" + propertyValue.ToString() + appendedText + "\"");
                                }
                                else
                                {
                                    sb.Append(" " + attrib.AttributeName + "=\"\"");
                                }
                            }
                        }
                    }
                }
            }
            bool          subsectionsFound = false;
            bool          firstSubSection  = true;
            StringBuilder comments         = null;

            //get xml string for sub-sections if exists
            if (propertiesInfo != null && propertiesInfo.Length > 0)
            {
                for (int i = 0; i < propertiesInfo.Length; i++)
                {
                    PropertyInfo property      = propertiesInfo[i];
                    object[]     customAttribs = property.GetCustomAttributes(true);

                    if (customAttribs != null && customAttribs.Length > 0)
                    {
                        for (int j = 0; j < customAttribs.Length; j++)
                        {
                            ConfigurationCommentAttribute commentAttrib = customAttribs[j] as ConfigurationCommentAttribute;
                            if (commentAttrib != null)
                            {
                                Object propertyValue = property.GetValue(configSection, null);
                                if (propertyValue != null)
                                {
                                    string propStr = propertyValue as string;
                                    if (!string.IsNullOrEmpty(propStr))
                                    {
                                        if (comments == null)
                                        {
                                            comments = new StringBuilder();
                                        }
                                        comments.AppendFormat("{0}<!--{1}-->{2}", preStr, propStr, endStr);
                                    }
                                }
                            }

                            ConfigurationSectionAttribute attrib = customAttribs[j] as ConfigurationSectionAttribute;
                            if (attrib != null)
                            {
                                Object propertyValue = property.GetValue(configSection, null);
                                if (propertyValue != null)
                                {
                                    subsectionsFound = true;
                                    if (firstSubSection)
                                    {
                                        sb.Append(">" + endStr);
                                        firstSubSection = false;
                                    }
                                    if (propertyValue.GetType().IsArray)
                                    {
                                        Array  array = propertyValue as Array;
                                        Object actualSectionObj;
                                        for (int k = 0; k < array.Length; k++)
                                        {
                                            actualSectionObj = array.GetValue(k);
                                            if (actualSectionObj != null)
                                            {
                                                sb.Append(GetSectionXml(actualSectionObj, attrib.SectionName, indent + 1));
                                            }
                                        }
                                    }
                                    else
                                    {
                                        sb.Append(GetSectionXml(propertyValue, attrib.SectionName, indent + 1));
                                    }
                                }
                            }
                        }
                    }
                }
            }

            if (subsectionsFound)
            {
                sb.Append(preStr + "</" + sectionName + ">" + endStr);
            }
            else
            {
                sb.Append("/>" + endStr);
            }

            string xml = string.Empty;

            if (comments != null)
            {
                xml = comments.ToString() + sb.ToString();
            }
            else
            {
                xml = sb.ToString();
            }

            return(xml);
        }