Esempio n. 1
0
        /// <summary>
        /// Gets the type of the section object.
        /// </summary>
        /// <param name="config"></param>
        /// <param name="sectionName"></param>
        /// <returns></returns>
        private Type GetConfigSectionObjectType(object config, string sectionName)
        {
            Type sectionType = null;
            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(ConfigurationSectionAttribute), false);
                    if (customAttribs != null && customAttribs.Length > 0)
                    {
                        ConfigurationSectionAttribute configSection = customAttribs[0] as ConfigurationSectionAttribute;
                        if (configSection != null && String.Compare(configSection.SectionName, sectionName, true) == 0)
                        {
                            sectionType = propInfo.PropertyType;
                            break;
                        }
                    }
                }
            }

            if (sectionType == null)
            {
                if (_dynamicSectionTypeMap.ContainsKey(sectionName.ToLower()))
                {
                    sectionType = _dynamicSectionTypeMap[sectionName.ToLower()].Type;
                }
            }

            return(sectionType);
        }
Esempio n. 2
0
        private void SetConfigSectionObject(object config, object sectionConfig, string sectionName)
        {
            Type type = config.GetType();

            PropertyInfo[] objProps = type.GetProperties();
            try
            {
                if (objProps != null)
                {
                    for (int i = 0; i < objProps.Length; i++)
                    {
                        PropertyInfo propInfo      = objProps[i];
                        Object[]     customAttribs = propInfo.GetCustomAttributes(typeof(ConfigurationSectionAttribute), false);

                        if (customAttribs != null && customAttribs.Length > 0)
                        {
                            ConfigurationSectionAttribute configSection = customAttribs[0] as ConfigurationSectionAttribute;
                            try
                            {
                                if (configSection != null && String.Compare(configSection.SectionName, sectionName, true) == 0)
                                {
                                    propInfo.SetValue(config, sectionConfig, null);
                                }
                            }
                            catch (Exception e)
                            {
                                throw new Exception("Duplicate cache entries in " + config);
                            }
                        }


                        customAttribs = propInfo.GetCustomAttributes(typeof(DynamicConfigurationSectionAttribute), false);
                        if (customAttribs != null && customAttribs.Length > 0)
                        {
                            DynamicConfigurationSectionAttribute configSection = customAttribs[0] as DynamicConfigurationSectionAttribute;
                            try
                            {
                                if (configSection != null && String.Compare(configSection.SectionName, sectionName, true) == 0)
                                {
                                    propInfo.SetValue(config, sectionConfig, null);
                                }
                            }
                            catch (Exception e)
                            {
                                throw new Exception("Duplicate cache entries in " + config.ToString());
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }
Esempio n. 3
0
        private Object GetConfigSectionObject(object config, string sectionName)
        {
            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(ConfigurationSectionAttribute), false);
                    if (customAttribs != null && customAttribs.Length > 0)
                    {
                        ConfigurationSectionAttribute configSection = customAttribs[0] as ConfigurationSectionAttribute;
                        if (configSection != null && String.Compare(configSection.SectionName, sectionName, true) == 0)
                        {
                            return(Activator.CreateInstance(propInfo.PropertyType));
                        }
                    }
                }
            }
            return(null);
        }
Esempio n. 4
0
        private string GetSectionXml(Object configSection, string sectionName, int indent)
        {
            //string strPropertyValue = "";
            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)
                                {
                                    //strPropertyValue = propertyValue.ToString();
                                    //if (sectionName == "attribute" && strPropertyValue.Contains("<"))
                                    //{
                                    //    strPropertyValue = strPropertyValue.Replace("<", "&lt;");
                                    //    strPropertyValue = strPropertyValue.Replace(">", "&gt;");
                                    //}
                                    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);
        }