void AddDefaultValueAttribute(CodeMemberField field, CodeAttributeDeclarationCollection metadata, object defaultValue, TypeMapping mapping, CodeCommentStatementCollection comments, TypeDesc memberTypeDesc, Accessor accessor, CodeConstructor ctor) {
string attributeName = accessor.IsFixed ? "fixed" : "default";
if (!memberTypeDesc.HasDefaultSupport) {
if (comments != null && defaultValue is string) {
DropDefaultAttribute(accessor, comments, memberTypeDesc.FullName);
// do not generate intializers for the user prefered types if they do not have default capability
AddWarningComment(comments, Res.GetString(Res.XmlDropAttributeValue, attributeName, mapping.TypeName, defaultValue.ToString()));
}
return;
}
if (memberTypeDesc.IsArrayLike && accessor is ElementAccessor) {
if (comments != null && defaultValue is string) {
DropDefaultAttribute(accessor, comments, memberTypeDesc.FullName);
// do not generate intializers for array-like types
AddWarningComment(comments, Res.GetString(Res.XmlDropArrayAttributeValue, attributeName, defaultValue.ToString(), ((ElementAccessor)accessor).Name));
}
return;
}
if (mapping.TypeDesc.IsMappedType && field != null && defaultValue is string) {
SchemaImporterExtension extension = mapping.TypeDesc.ExtendedType.Extension;
CodeExpression init = extension.ImportDefaultValue((string)defaultValue, mapping.TypeDesc.FullName);
if (init != null) {
if (ctor != null) {
AddInitializationStatement(ctor, field, init);
}
else {
field.InitExpression = extension.ImportDefaultValue((string)defaultValue, mapping.TypeDesc.FullName);
}
}
if (comments != null) {
DropDefaultAttribute(accessor, comments, mapping.TypeDesc.FullName);
if (init == null) {
AddWarningComment(comments, Res.GetString(Res.XmlNotKnownDefaultValue, extension.GetType().FullName, attributeName, (string)defaultValue, mapping.TypeName, mapping.Namespace));
}
}
return;
}
object value = null;
if (defaultValue is string || defaultValue == null) {
value = ImportDefault(mapping, (string)defaultValue);
}
if (value == null) return;
if (!(mapping is PrimitiveMapping)) {
DropDefaultAttribute(accessor, comments, memberTypeDesc.FullName);
AddWarningComment(comments, Res.GetString(Res.XmlDropNonPrimitiveAttributeValue, attributeName, defaultValue.ToString()));
return;
}
PrimitiveMapping pm = (PrimitiveMapping)mapping;
if (comments != null && !pm.TypeDesc.HasDefaultSupport && pm.TypeDesc.IsMappedType) {
// do not generate intializers for the user prefered types if they do not have default capability
DropDefaultAttribute(accessor, comments, pm.TypeDesc.FullName);
return;
}
if (value == DBNull.Value) {
if (comments != null) {
AddWarningComment(comments, Res.GetString(Res.XmlDropAttributeValue, attributeName, pm.TypeName, defaultValue.ToString()));
}
return;
}
CodeAttributeArgument[] arguments = null;
CodeExpression initExpression = null;
if (pm.IsList) {
#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(object[])) throw new InvalidOperationException(Res.GetString(Res.XmlInternalErrorDetails, "Default value for list should be object[], not " + value.GetType().Name));
#endif
object[] vals = (object[])value;
CodeExpression[] initializers = new CodeExpression[vals.Length];
for (int i = 0; i < vals.Length; i++) {
GetDefaultValueArguments(pm, vals[i], out initializers[i]);
}
initExpression = new CodeArrayCreateExpression(field.Type, initializers);
}
else {
arguments = GetDefaultValueArguments(pm, value, out initExpression);
}
if (field != null) {
if (ctor != null) {
AddInitializationStatement(ctor, field, initExpression);
}
else {
field.InitExpression = initExpression;
}
}
if (arguments != null && pm.TypeDesc.HasDefaultSupport && accessor.IsOptional && !accessor.IsFixed) {
// Add [DefaultValueAttribute]
CodeAttributeDeclaration attribute = new CodeAttributeDeclaration(typeof(DefaultValueAttribute).FullName, arguments);
metadata.Add(attribute);
}
else if (comments != null) {
DropDefaultAttribute(accessor, comments, memberTypeDesc.FullName);
}
}