Microsoft.Web.Administration.ConfigurationAttributeSchema.Parse C# (CSharp) Method

Parse() private method

private Parse ( string value ) : object
value string
return object
        internal object Parse(string value)
        {
            if (value == null)
            {
                return null;
            }

            object result = null;

            // bool|enum|flags|uint|int|int64|string|timeSpan
            if (Type == "bool")
            {
                bool b;
                bool.TryParse(value, out b);
                result = b;
            }
            else if (Type == "uint")
            {
                uint u;
                uint.TryParse(value, out u);
                result = u;
            }
            else if (Type == "int")
            {
                int i;
                int.TryParse(value, out i);
                result = i;
            }
            else if (Type == "int64")
            {
                long l;
                long.TryParse(value, out l);
                result = l;
            }
            else if (Type == "string")
            {
                result = value;
            }
            else if (Type == "enum")
            {
                var enum1 = GetEnumValues()[value];
                if (enum1 == null)
                {
                    throw new COMException(string.Format("Enum must be one of {0}\r\n", this.GetEnumValues().FormattedString));
                }

                result = enum1.Value;
            }
            else if (Type == "flags")
            {
                if (value == "None" || value == string.Empty)
                {
                    // IMPORTANT: workaround for Ftp_Schema.xml bug on validationFlags defaultValue=string.Empty.
                    result = 0L;
                }
                else
                {
                    var parts = value.Split(',');
                    long flags = 0;
                    foreach (var part in parts)
                    {
                        var values = GetEnumValues();
                        var item = values[part.Trim()];
                        if (item == null)
                        {
                            throw new COMException(string.Format("Flags must be some combination of {0}\r\n", this.GetEnumValues().FormattedString));
                        }

                        flags |= item.Value;
                    }

                    result = flags;
                }
            }
            else if (Type == "timeSpan")
            {
                if (AllowInfinite && value == "Infinite")
                {
                    result = Timeout.InfiniteTimeSpan;
                }
                else if (TimeSpanFormat == "string")
                {
                    TimeSpan t;
                    TimeSpan.TryParse(value, out t);
                    result = t;
                }
                else if (TimeSpanFormat == "minutes")
                {
                    result = new TimeSpan(0, int.Parse(value), 0);
                }
                else if (TimeSpanFormat == "seconds")
                {
                    result = new TimeSpan(0, 0, int.Parse(value));
                }
            }

            Validate(result);

            return result;
        }

Usage Example

 /// <summary>
 /// Constructs instances from config file.
 /// </summary>
 /// <param name="name"></param>
 /// <param name="schema"></param>
 /// <param name="value"></param>
 /// <param name="element"></param>
 internal ConfigurationAttribute(string name, ConfigurationAttributeSchema schema, string value, ConfigurationElement element)
 {
     _element = element;
     Name = name;
     Schema = schema;
     IsProtected = schema?.IsEncrypted ?? false;
     var clear = Decrypt(value).ToString();
     var raw = Schema == null ? clear : Schema.Parse(clear);
     var result = TypeMatch(raw);
     IsInheritedFromDefaultValue = (Schema == null || !Schema.IsRequired)
                                             && result.Equals(ExtractDefaultValue());
     SetValue(raw);
     _element.InnerEntity.SetAttributeValue(Name, value);
 }
All Usage Examples Of Microsoft.Web.Administration.ConfigurationAttributeSchema::Parse