Пример #1
0
        private object ImportDefaultValue(TypeMapping mapping, string defaultValue)
        {
            if (defaultValue == null)
            {
                return(null);
            }
            if (!(mapping is PrimitiveMapping))
            {
                return(DBNull.Value);
            }

            if (mapping is EnumMapping)
            {
                EnumMapping       em = (EnumMapping)mapping;
                ConstantMapping[] c  = em.Constants;

                if (em.IsFlags)
                {
                    Hashtable values = new Hashtable();
                    string[]  names  = new string[c.Length];
                    long[]    ids    = new long[c.Length];

                    for (int i = 0; i < c.Length; i++)
                    {
                        ids[i]   = em.IsFlags ? 1L << i : (long)i;
                        names[i] = c[i].Name;
                        values.Add(c[i].Name, ids[i]);
                    }
                    // this validates the values
                    long val = XmlCustomFormatter.ToEnum(defaultValue, values, em.TypeName, true);
                    return(XmlCustomFormatter.FromEnum(val, names, ids, em.TypeDesc.FullName));
                }
                else
                {
                    for (int i = 0; i < c.Length; i++)
                    {
                        if (c[i].XmlName == defaultValue)
                        {
                            return(c[i].Name);
                        }
                    }
                }
                throw new InvalidOperationException(string.Format(ResXml.XmlInvalidDefaultValue, defaultValue, em.TypeDesc.FullName));
            }

            // Primitive mapping
            PrimitiveMapping pm = (PrimitiveMapping)mapping;

            if (!pm.TypeDesc.HasCustomFormatter)
            {
                if (pm.TypeDesc.FormatterName == "String")
                {
                    return(defaultValue);
                }
                if (pm.TypeDesc.FormatterName == "DateTime")
                {
                    return(XmlCustomFormatter.ToDateTime(defaultValue));
                }

                Type formatter = typeof(XmlConvert);

                MethodInfo format = formatter.GetMethod("To" + pm.TypeDesc.FormatterName, new Type[] { typeof(string) });
                if (format != null)
                {
                    return(format.Invoke(formatter, new Object[] { defaultValue }));
                }
            }
            else
            {
                if (pm.TypeDesc.HasDefaultSupport)
                {
                    return(XmlCustomFormatter.ToDefaultValue(defaultValue, pm.TypeDesc.FormatterName));
                }
            }
            return(DBNull.Value);
        }
Пример #2
0
        private CodeAttributeArgument[] GetDefaultValueArguments(PrimitiveMapping mapping, object value, out CodeExpression initExpression)
        {
            initExpression = null;
            if (value == null)
            {
                return(null);
            }

            CodeExpression valueExpression = null;
            CodeExpression typeofValue     = null;
            Type           type            = value.GetType();

            CodeAttributeArgument[] arguments = null;

            if (mapping is EnumMapping)
            {
#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(string.Format(ResXml.XmlInternalErrorDetails, "Invalid enumeration type " + value.GetType().Name));
                }
#endif

                if (((EnumMapping)mapping).IsFlags)
                {
                    string[] values = ((string)value).Split(null);
                    for (int i = 0; i < values.Length; i++)
                    {
                        if (values[i].Length == 0)
                        {
                            continue;
                        }
                        CodeExpression enumRef = new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(mapping.TypeDesc.FullName), values[i]);
                        if (valueExpression != null)
                        {
                            valueExpression = new CodeBinaryOperatorExpression(valueExpression, CodeBinaryOperatorType.BitwiseOr, enumRef);
                        }
                        else
                        {
                            valueExpression = enumRef;
                        }
                    }
                }
                else
                {
                    valueExpression = new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(mapping.TypeDesc.FullName), (string)value);
                }
                initExpression = valueExpression;
                arguments      = new CodeAttributeArgument[] { new CodeAttributeArgument(valueExpression) };
            }
            else if (type == typeof(bool) ||
                     type == typeof(Int32) ||
                     type == typeof(string) ||
                     type == typeof(double))
            {
                initExpression = valueExpression = new CodePrimitiveExpression(value);
                arguments      = new CodeAttributeArgument[] { new CodeAttributeArgument(valueExpression) };
            }
            else if (type == typeof(Int16) ||
                     type == typeof(Int64) ||
                     type == typeof(float) ||
                     type == typeof(byte) ||
                     type == typeof(decimal))
            {
                valueExpression = new CodePrimitiveExpression(Convert.ToString(value, NumberFormatInfo.InvariantInfo));
                typeofValue     = new CodeTypeOfExpression(type.FullName);
                arguments       = new CodeAttributeArgument[] { new CodeAttributeArgument(typeofValue), new CodeAttributeArgument(valueExpression) };
                initExpression  = new CodeCastExpression(type.FullName, new CodePrimitiveExpression(value));
            }
            else if (type == typeof(sbyte) ||
                     type == typeof(UInt16) ||
                     type == typeof(UInt32) ||
                     type == typeof(UInt64))
            {
                // need to promote the non-CLS complient types

                value = PromoteType(type, value);

                valueExpression = new CodePrimitiveExpression(Convert.ToString(value, NumberFormatInfo.InvariantInfo));
                typeofValue     = new CodeTypeOfExpression(type.FullName);
                arguments       = new CodeAttributeArgument[] { new CodeAttributeArgument(typeofValue), new CodeAttributeArgument(valueExpression) };
                initExpression  = new CodeCastExpression(type.FullName, new CodePrimitiveExpression(value));
            }
            else if (type == typeof(DateTime))
            {
                DateTime dt = (DateTime)value;
                string   dtString;
                long     ticks;
                if (mapping.TypeDesc.FormatterName == "Date")
                {
                    dtString = XmlCustomFormatter.FromDate(dt);
                    ticks    = (new DateTime(dt.Year, dt.Month, dt.Day)).Ticks;
                }
                else if (mapping.TypeDesc.FormatterName == "Time")
                {
                    dtString = XmlCustomFormatter.FromDateTime(dt);
                    ticks    = dt.Ticks;
                }
                else
                {
                    dtString = XmlCustomFormatter.FromDateTime(dt);
                    ticks    = dt.Ticks;
                }
                valueExpression = new CodePrimitiveExpression(dtString);
                typeofValue     = new CodeTypeOfExpression(type.FullName);
                arguments       = new CodeAttributeArgument[] { new CodeAttributeArgument(typeofValue), new CodeAttributeArgument(valueExpression) };
                initExpression  = new CodeObjectCreateExpression(new CodeTypeReference(typeof(DateTime)), new CodeExpression[] { new CodePrimitiveExpression(ticks) });
            }
            else if (type == typeof(Guid))
            {
                valueExpression = new CodePrimitiveExpression(Convert.ToString(value, NumberFormatInfo.InvariantInfo));
                typeofValue     = new CodeTypeOfExpression(type.FullName);
                arguments       = new CodeAttributeArgument[] { new CodeAttributeArgument(typeofValue), new CodeAttributeArgument(valueExpression) };
                initExpression  = new CodeObjectCreateExpression(new CodeTypeReference(typeof(Guid)), new CodeExpression[] { valueExpression });
            }
            if (mapping.TypeDesc.FullName != type.ToString() && !(mapping is EnumMapping))
            {
                // generate cast
                initExpression = new CodeCastExpression(mapping.TypeDesc.FullName, initExpression);
            }
            return(arguments);
        }