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
 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);
 }
Ejemplo n.º 2
0
 protected static long ToEnum(string value, Hashtable h, string typeName)
 {
     return(XmlCustomFormatter.ToEnum(value, h, typeName, true));
 }
Ejemplo n.º 3
0
 protected static long ToEnum(string value, IDictionary h, string typeName)
 {
     return(XmlCustomFormatter.ToEnum(value, (Hashtable)h, typeName, true));
 }
Ejemplo n.º 4
0
        static internal string ExportDefaultValue(TypeMapping mapping, object value)
        {
            #if DEBUG
            // use exception in the place of Debug.Assert to avoid throwing asserts from a server process such as aspnet_ewp.exe
            if (!(mapping is PrimitiveMapping))
            {
                throw new InvalidOperationException(Res.GetString(Res.XmlInternalErrorDetails, "Mapping " + mapping.GetType() + ", should not have Default"));
            }
            else if (mapping.IsList)
            {
                throw new InvalidOperationException(Res.GetString(Res.XmlInternalErrorDetails, "Mapping " + mapping.GetType() + ", should not have Default"));
            }
            #endif


            if (mapping is EnumMapping)
            {
                EnumMapping em = (EnumMapping)mapping;

                #if DEBUG
                // use exception in the place of Debug.Assert to avoid throwing asserts from a server process such as aspnet_ewp.exe
                if (value.GetType() != typeof(string))
                {
                    throw new InvalidOperationException(Res.GetString(Res.XmlInternalErrorDetails, Res.GetString(Res.XmlInvalidDefaultValue, value.ToString(), value.GetType().FullName)));
                }
                #endif

                // check the validity of the value
                ConstantMapping[] c = em.Constants;
                if (em.IsFlags)
                {
                    string[]  names  = new string[c.Length];
                    long[]    ids    = new long[c.Length];
                    Hashtable values = new Hashtable();
                    for (int i = 0; i < c.Length; i++)
                    {
                        names[i] = c[i].XmlName;
                        ids[i]   = 1 << i;
                        values.Add(c[i].Name, ids[i]);
                    }
                    long val = XmlCustomFormatter.ToEnum((string)value, values, em.TypeName, false);
                    return(val != 0 ? XmlCustomFormatter.FromEnum(val, names, ids) : null);
                }
                else
                {
                    for (int i = 0; i < c.Length; i++)
                    {
                        if (c[i].Name == (string)value)
                        {
                            return(c[i].XmlName);
                        }
                    }
                    return(null); // unknown value
                }
            }

            PrimitiveMapping pm = (PrimitiveMapping)mapping;

            if (!pm.TypeDesc.HasCustomFormatter)
            {
                #if DEBUG
                // use exception in the place of Debug.Assert to avoid throwing asserts from a server process such as aspnet_ewp.exe
                if (pm.TypeDesc.Type == null)
                {
                    throw new InvalidOperationException(Res.GetString(Res.XmlInternalErrorDetails, "Mapping for " + pm.TypeDesc.Name + " missing type property"));
                }
                #endif

                if (pm.TypeDesc.FormatterName == "String")
                {
                    return((string)value);
                }

                Type formatter = typeof(XmlConvert);
                System.Reflection.MethodInfo format = formatter.GetMethod("ToString", new Type[] { pm.TypeDesc.Type });
                if (format != null)
                {
                    return((string)format.Invoke(formatter, new Object[] { value }));
                }
            }
            else
            {
                string defaultValue = XmlCustomFormatter.FromDefaultValue(value, pm.TypeDesc.FormatterName);
                if (defaultValue == null)
                {
                    throw new InvalidOperationException(Res.GetString(Res.XmlInvalidDefaultValue, value.ToString(), pm.TypeDesc.Name));
                }
                return(defaultValue);
            }
            throw new InvalidOperationException(Res.GetString(Res.XmlInvalidDefaultValue, value.ToString(), pm.TypeDesc.Name));
        }