System.Xml.Serialization.XmlCustomFormatter.ToEnum C# (CSharp) Method

ToEnum() static private method

static private ToEnum ( string val, Hashtable vals, string typeName, bool validate ) : long
val string
vals System.Collections.Hashtable
typeName string
validate bool
return long
        internal static long ToEnum(string val, Hashtable vals, string typeName, bool validate)
        {
            long value = 0;
            string[] parts = val.Split(null);
            for (int i = 0; i < parts.Length; i++)
            {
                object id = vals[parts[i]];
                if (id != null)
                    value |= (long)id;
                else if (validate && parts[i].Length > 0)
                    throw new InvalidOperationException(SR.Format(SR.XmlUnknownConstant, parts[i], typeName));
            }
            return value;
        }

Usage Example

 private object ImportDefaultValue(TypeMapping mapping, string defaultValue)
 {
     if (defaultValue == null)
     {
         return(null);
     }
     if (mapping is PrimitiveMapping)
     {
         if (mapping is EnumMapping)
         {
             EnumMapping       mapping2  = (EnumMapping)mapping;
             ConstantMapping[] constants = mapping2.Constants;
             if (mapping2.IsFlags)
             {
                 Hashtable vals     = new Hashtable();
                 string[]  strArray = new string[constants.Length];
                 long[]    ids      = new long[constants.Length];
                 for (int j = 0; j < constants.Length; j++)
                 {
                     ids[j]      = mapping2.IsFlags ? (((long)1L) << j) : ((long)j);
                     strArray[j] = constants[j].Name;
                     vals.Add(constants[j].Name, ids[j]);
                 }
                 return(XmlCustomFormatter.FromEnum(XmlCustomFormatter.ToEnum(defaultValue, vals, mapping2.TypeName, true), strArray, ids, mapping2.TypeDesc.FullName));
             }
             for (int i = 0; i < constants.Length; i++)
             {
                 if (constants[i].XmlName == defaultValue)
                 {
                     return(constants[i].Name);
                 }
             }
             throw new InvalidOperationException(Res.GetString("XmlInvalidDefaultValue", new object[] { defaultValue, mapping2.TypeDesc.FullName }));
         }
         PrimitiveMapping mapping3 = (PrimitiveMapping)mapping;
         if (!mapping3.TypeDesc.HasCustomFormatter)
         {
             if (mapping3.TypeDesc.FormatterName == "String")
             {
                 return(defaultValue);
             }
             if (mapping3.TypeDesc.FormatterName == "DateTime")
             {
                 return(XmlCustomFormatter.ToDateTime(defaultValue));
             }
             Type       type   = typeof(XmlConvert);
             MethodInfo method = type.GetMethod("To" + mapping3.TypeDesc.FormatterName, new Type[] { typeof(string) });
             if (method != null)
             {
                 return(method.Invoke(type, new object[] { defaultValue }));
             }
         }
         else if (mapping3.TypeDesc.HasDefaultSupport)
         {
             return(XmlCustomFormatter.ToDefaultValue(defaultValue, mapping3.TypeDesc.FormatterName));
         }
     }
     return(DBNull.Value);
 }
All Usage Examples Of System.Xml.Serialization.XmlCustomFormatter::ToEnum