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);
}
}