FromDateTime() static private method

static private FromDateTime ( System.DateTime value ) : string
value System.DateTime
return string
Esempio n. 1
0
        void AddDefaultValueAttribute(CodeMemberField field, CodeAttributeDeclarationCollection metadata, object value, TypeMapping 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 (!(mapping is PrimitiveMapping))
            {
                throw new InvalidOperationException(Res.GetString(Res.XmlInternalErrorDetails, "Default value is invalid for " + mapping.GetType().Name));
            }
            else if (mapping.IsList)
            {
                throw new InvalidOperationException(Res.GetString(Res.XmlInternalErrorDetails, "Default value is invalid for " + mapping.GetType().Name));
            }
            #endif

            if (value == null)
            {
                return;
            }

            CodeExpression valueExpression = null;
            CodeExpression initExpression  = null;
            CodeExpression typeofValue     = null;
            string         typeName        = mapping.TypeDesc.FullName;
            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(Res.GetString(Res.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(typeName), values[i]);
                        if (valueExpression != null)
                        {
                            valueExpression = new CodeBinaryOperatorExpression(valueExpression, CodeBinaryOperatorType.BitwiseOr, enumRef);
                        }
                        else
                        {
                            valueExpression = enumRef;
                        }
                    }
                }
                else
                {
                    valueExpression = new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeName), (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(value.ToString());
                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) });
            }
            if (arguments != null)
            {
                if (field != null)
                {
                    field.InitExpression = initExpression;
                }
                AddCustomAttribute(metadata, typeof(DefaultValueAttribute), arguments);
            }
        }
 /// <include file='doc\XmlSerializationWriter.uex' path='docs/doc[@for="XmlSerializationWriter.FromDateTime"]/*' />
 protected static string FromDateTime(DateTime value)
 {
     return(XmlCustomFormatter.FromDateTime(value));
 }
        private CodeAttributeArgument[] GetDefaultValueArguments(PrimitiveMapping mapping, object value, out CodeExpression initExpression)
        {
            initExpression = null;
            if (value == null)
            {
                return(null);
            }
            CodeExpression left        = null;
            CodeExpression expression2 = null;
            Type           type        = value.GetType();

            CodeAttributeArgument[] argumentArray = null;
            if (mapping is EnumMapping)
            {
                if (((EnumMapping)mapping).IsFlags)
                {
                    string[] strArray = ((string)value).Split(null);
                    for (int i = 0; i < strArray.Length; i++)
                    {
                        if (strArray[i].Length != 0)
                        {
                            CodeExpression right = new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(mapping.TypeDesc.FullName), strArray[i]);
                            if (left != null)
                            {
                                left = new CodeBinaryOperatorExpression(left, CodeBinaryOperatorType.BitwiseOr, right);
                            }
                            else
                            {
                                left = right;
                            }
                        }
                    }
                }
                else
                {
                    left = new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(mapping.TypeDesc.FullName), (string)value);
                }
                initExpression = left;
                argumentArray  = new CodeAttributeArgument[] { new CodeAttributeArgument(left) };
            }
            else if (((type == typeof(bool)) || (type == typeof(int))) || ((type == typeof(string)) || (type == typeof(double))))
            {
                initExpression = left = new CodePrimitiveExpression(value);
                argumentArray  = new CodeAttributeArgument[] { new CodeAttributeArgument(left) };
            }
            else if (((type == typeof(short)) || (type == typeof(long))) || (((type == typeof(float)) || (type == typeof(byte))) || (type == typeof(decimal))))
            {
                left           = new CodePrimitiveExpression(Convert.ToString(value, NumberFormatInfo.InvariantInfo));
                expression2    = new CodeTypeOfExpression(type.FullName);
                argumentArray  = new CodeAttributeArgument[] { new CodeAttributeArgument(expression2), new CodeAttributeArgument(left) };
                initExpression = new CodeCastExpression(type.FullName, new CodePrimitiveExpression(value));
            }
            else if (((type == typeof(sbyte)) || (type == typeof(ushort))) || ((type == typeof(uint)) || (type == typeof(ulong))))
            {
                value          = CodeExporter.PromoteType(type, value);
                left           = new CodePrimitiveExpression(Convert.ToString(value, NumberFormatInfo.InvariantInfo));
                expression2    = new CodeTypeOfExpression(type.FullName);
                argumentArray  = new CodeAttributeArgument[] { new CodeAttributeArgument(expression2), new CodeAttributeArgument(left) };
                initExpression = new CodeCastExpression(type.FullName, new CodePrimitiveExpression(value));
            }
            else if (type == typeof(DateTime))
            {
                string   str;
                long     ticks;
                DateTime time = (DateTime)value;
                if (mapping.TypeDesc.FormatterName == "Date")
                {
                    str = XmlCustomFormatter.FromDate(time);
                    DateTime time2 = new DateTime(time.Year, time.Month, time.Day);
                    ticks = time2.Ticks;
                }
                else if (mapping.TypeDesc.FormatterName == "Time")
                {
                    str   = XmlCustomFormatter.FromDateTime(time);
                    ticks = time.Ticks;
                }
                else
                {
                    str   = XmlCustomFormatter.FromDateTime(time);
                    ticks = time.Ticks;
                }
                left           = new CodePrimitiveExpression(str);
                expression2    = new CodeTypeOfExpression(type.FullName);
                argumentArray  = new CodeAttributeArgument[] { new CodeAttributeArgument(expression2), new CodeAttributeArgument(left) };
                initExpression = new CodeObjectCreateExpression(new CodeTypeReference(typeof(DateTime)), new CodeExpression[] { new CodePrimitiveExpression(ticks) });
            }
            else if (type == typeof(Guid))
            {
                left           = new CodePrimitiveExpression(Convert.ToString(value, NumberFormatInfo.InvariantInfo));
                expression2    = new CodeTypeOfExpression(type.FullName);
                argumentArray  = new CodeAttributeArgument[] { new CodeAttributeArgument(expression2), new CodeAttributeArgument(left) };
                initExpression = new CodeObjectCreateExpression(new CodeTypeReference(typeof(Guid)), new CodeExpression[] { left });
            }
            if ((mapping.TypeDesc.FullName != type.ToString()) && !(mapping is EnumMapping))
            {
                initExpression = new CodeCastExpression(mapping.TypeDesc.FullName, initExpression);
            }
            return(argumentArray);
        }