internal class MapCodeGenerator {
CodeNamespace codeNamespace;
- CodeCompileUnit codeCompileUnit;
+// CodeCompileUnit codeCompileUnit;
CodeAttributeDeclarationCollection includeMetadata;
- XmlTypeMapping exportedAnyType = null;
+ XmlTypeMapping exportedAnyType;
protected bool includeArrayTypes;
+#if NET_2_0
CodeDomProvider codeProvider;
+#endif
CodeGenerationOptions options;
+ CodeIdentifiers identifiers;
Hashtable exportedMaps = new Hashtable ();
Hashtable includeMaps = new Hashtable ();
public MapCodeGenerator (CodeNamespace codeNamespace, CodeCompileUnit codeCompileUnit, CodeGenerationOptions options)
{
- this.codeCompileUnit = codeCompileUnit;
+// this.codeCompileUnit = codeCompileUnit;
this.codeNamespace = codeNamespace;
this.options = options;
+ this.identifiers = new CodeIdentifiers ();
}
public MapCodeGenerator (CodeNamespace codeNamespace, CodeCompileUnit codeCompileUnit, CodeDomProvider codeProvider, CodeGenerationOptions options, Hashtable mappings)
{
- this.codeCompileUnit = codeCompileUnit;
+// this.codeCompileUnit = codeCompileUnit;
this.codeNamespace = codeNamespace;
- this.codeProvider = codeProvider;
this.options = options;
+#if NET_2_0
+ this.codeProvider = codeProvider;
+ this.identifiers = new CodeIdentifiers ((codeProvider.LanguageOptions & LanguageOptions.CaseInsensitive) == 0);
+#else
+ this.identifiers = new CodeIdentifiers ();
+#endif
// this.mappings = mappings;
}
if (map.BaseMap != null && map.BaseMap.TypeData.SchemaType != SchemaTypes.XmlNode)
{
- CodeTypeReference ctr = new CodeTypeReference (map.BaseMap.TypeData.FullTypeName);
+ CodeTypeReference ctr = GetDomType (map.BaseMap.TypeData, false);
codeClass.BaseTypes.Add (ctr);
if (map.BaseMap.IncludeInSchema) {
ExportMapCode (map.BaseMap, false);
void ExportMembersMapCode (CodeTypeDeclaration codeClass, ClassMap map, string defaultNamespace, XmlTypeMapping baseMap)
{
+ ICollection attributes = map.AttributeMembers;
+ ICollection members = map.ElementMembers;
+
+ // collect names
+ if (attributes != null)
+ foreach (XmlTypeMapMemberAttribute attr in attributes)
+ identifiers.AddUnique (attr.Name, attr);
+ if (members != null)
+ foreach (XmlTypeMapMemberElement member in members)
+ identifiers.AddUnique (member.Name, member);
+
// Write attributes
- ICollection attributes = map.AttributeMembers;
if (attributes != null) {
foreach (XmlTypeMapMemberAttribute attr in attributes) {
if (baseMap != null && DefinedInBaseMap (baseMap, attr)) continue;
}
}
- ICollection members = map.ElementMembers;
+ members = map.ElementMembers;
if (members != null)
{
CodeTypeMember CreateFieldMember (CodeTypeDeclaration codeClass, TypeData type, string name)
{
- return CreateFieldMember (codeClass, GetDomType (type), name, System.DBNull.Value, null, null);
+ return CreateFieldMember (codeClass, GetDomType (type, false), name, System.DBNull.Value, null, null);
}
CodeTypeMember CreateFieldMember (CodeTypeDeclaration codeClass, XmlTypeMapMember member)
{
- return CreateFieldMember (codeClass, GetDomType (member.TypeData), member.Name, member.DefaultValue, member.TypeData, member.Documentation);
+ return CreateFieldMember (codeClass, GetDomType (member.TypeData, member.RequiresNullable), member.Name, member.DefaultValue, member.TypeData, member.Documentation);
}
CodeTypeMember CreateFieldMember (CodeTypeDeclaration codeClass, CodeTypeReference type, string name, object defaultValue, TypeData defaultType, string documentation)
CodeTypeMember codeProp = null;
if ((options & CodeGenerationOptions.GenerateProperties) > 0) {
- string field = CodeIdentifier.MakeCamel (name + "Field");
+ string field = identifiers.AddUnique (CodeIdentifier.MakeCamel (name + "Field"), name);
codeField = new CodeMemberField (type, field);
codeField.Attributes = MemberAttributes.Private;
codeClass.Members.Add (codeField);
if (attinfo.TypeData.IsValueType && attinfo.IsOptionalValueType)
{
- codeField = CreateFieldMember (codeClass, typeof(bool), attinfo.Name + "Specified");
+ codeField = CreateFieldMember (codeClass, typeof(bool), identifiers.MakeUnique (attinfo.Name + "Specified"));
codeField.Attributes = MemberAttributes.Public;
GenerateSpecifierMember (codeField);
}
if (member.TypeData.IsValueType && member.IsOptionalValueType)
{
- codeField = CreateFieldMember (codeClass, typeof(bool), member.Name + "Specified");
+ codeField = CreateFieldMember (codeClass, typeof(bool), identifiers.MakeUnique (member.Name + "Specified"));
codeField.Attributes = MemberAttributes.Public;
GenerateSpecifierMember (codeField);
}
foreach (XmlTypeMapElementInfo einfo in member.ElementInfo)
{
- if (ExportExtraElementAttributes (attributes, einfo, defaultNamespace, defaultType))
- continue;
-
- GenerateElementInfoMember (attributes, member, einfo, defaultType, defaultNamespace, addAlwaysAttr, forceUseMemberName);
if (einfo.MappedType != null) {
ExportMapCode (einfo.MappedType, false);
RemoveInclude (einfo.MappedType);
}
+
+ if (ExportExtraElementAttributes (attributes, einfo, defaultNamespace, defaultType))
+ continue;
+
+ GenerateElementInfoMember (attributes, member, einfo, defaultType, defaultNamespace, addAlwaysAttr, forceUseMemberName | addAlwaysAttr);
}
GenerateElementMember (attributes, member);
}
#endif
- CodeTypeReference GetDomType (TypeData data)
+ CodeTypeReference GetDomType (TypeData data, bool requiresNullable)
{
+#if NET_2_0
+ if (data.IsValueType && (data.IsNullable || requiresNullable))
+ return new CodeTypeReference ("System.Nullable", new CodeTypeReference (data.FullTypeName));
+#endif
if (data.SchemaType == SchemaTypes.Array)
- return new CodeTypeReference (GetDomType (data.ListItemTypeData),1);
+ return new CodeTypeReference (GetDomType (data.ListItemTypeData, false),1);
else
return new CodeTypeReference (data.FullTypeName);
}
throw new InvalidOperationException ("Type " + typeData.TypeName + " not supported");
IFormattable defaultValueFormattable = defaultValue as IFormattable;
- CodeFieldReferenceExpression fref = new CodeFieldReferenceExpression (new CodeTypeReferenceExpression (typeData.FullTypeName), defaultValueFormattable != null ? defaultValueFormattable.ToString(null, CultureInfo.InvariantCulture) : defaultValue.ToString ());
+ CodeFieldReferenceExpression fref = new CodeFieldReferenceExpression (new CodeTypeReferenceExpression (GetDomType (typeData, false)), defaultValueFormattable != null ? defaultValueFormattable.ToString(null, CultureInfo.InvariantCulture) : defaultValue.ToString ());
CodeAttributeArgument arg = new CodeAttributeArgument (fref);
AddCustomAttribute (externalField, "System.ComponentModel.DefaultValue", arg);
- internalField.InitExpression = fref;
+ //internalField.InitExpression = fref;
}
else
{
+ defaultValue = defaultValue is decimal ? (object) ('"' + ((decimal) defaultValue).ToString (CultureInfo.InvariantCulture) + '"') : defaultValue;
AddCustomAttribute (externalField, "System.ComponentModel.DefaultValue", GetArg (defaultValue));
- internalField.InitExpression = new CodePrimitiveExpression (defaultValue);
+ //internalField.InitExpression = new CodePrimitiveExpression (defaultValue);
}
}