2 // System.Xml.Serialization.MapCodeGenerator
5 // Lluis Sanchez Gual (lluis@ximian.com)
7 // Copyright (C) Ximian, Inc., 2003
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.CodeDom.Compiler;
33 using System.Collections;
34 using System.ComponentModel;
35 using System.Diagnostics;
36 using System.Globalization;
37 using System.Xml.Schema;
39 using Microsoft.CSharp;
41 namespace System.Xml.Serialization {
42 internal class MapCodeGenerator {
44 CodeNamespace codeNamespace;
45 // CodeCompileUnit codeCompileUnit;
46 CodeAttributeDeclarationCollection includeMetadata;
47 XmlTypeMapping exportedAnyType;
48 protected bool includeArrayTypes;
49 CodeDomProvider codeProvider;
50 CodeGenerationOptions options;
51 CodeIdentifiers identifiers;
53 Hashtable exportedMaps = new Hashtable ();
54 Hashtable includeMaps = new Hashtable ();
56 public MapCodeGenerator (CodeNamespace codeNamespace, CodeCompileUnit codeCompileUnit, CodeGenerationOptions options)
58 // this.codeCompileUnit = codeCompileUnit;
59 this.codeNamespace = codeNamespace;
60 this.options = options;
61 this.identifiers = new CodeIdentifiers ();
64 public MapCodeGenerator (CodeNamespace codeNamespace, CodeCompileUnit codeCompileUnit, CodeDomProvider codeProvider, CodeGenerationOptions options, Hashtable mappings)
66 // this.codeCompileUnit = codeCompileUnit;
67 this.codeNamespace = codeNamespace;
68 this.options = options;
69 this.codeProvider = codeProvider;
70 this.identifiers = new CodeIdentifiers ((codeProvider.LanguageOptions & LanguageOptions.CaseInsensitive) == 0);
71 // this.mappings = mappings;
74 public CodeAttributeDeclarationCollection IncludeMetadata
78 if (includeMetadata != null) return includeMetadata;
79 includeMetadata = new CodeAttributeDeclarationCollection ();
81 foreach (XmlTypeMapping map in includeMaps.Values)
82 GenerateClassInclude (includeMetadata, map);
84 return includeMetadata;
88 #region Code generation methods
90 public void ExportMembersMapping (XmlMembersMapping xmlMembersMapping)
92 CodeTypeDeclaration dummyClass = new CodeTypeDeclaration ();
93 ExportMembersMapCode (dummyClass, (ClassMap)xmlMembersMapping.ObjectMap, xmlMembersMapping.Namespace, null);
96 public void ExportTypeMapping (XmlTypeMapping xmlTypeMapping, bool isTopLevel)
98 ExportMapCode (xmlTypeMapping, isTopLevel);
99 RemoveInclude (xmlTypeMapping);
102 void ExportMapCode (XmlTypeMapping map, bool isTopLevel)
104 switch (map.TypeData.SchemaType)
106 case SchemaTypes.Enum:
107 ExportEnumCode (map, isTopLevel);
110 case SchemaTypes.Array:
111 ExportArrayCode (map);
114 case SchemaTypes.Class:
115 ExportClassCode (map, isTopLevel);
118 case SchemaTypes.XmlSerializable:
119 case SchemaTypes.XmlNode:
120 case SchemaTypes.Primitive:
126 void ExportClassCode (XmlTypeMapping map, bool isTopLevel)
128 CodeTypeDeclaration codeClass;
129 if (IsMapExported (map)) {
130 codeClass = GetMapDeclaration (map);
131 if (codeClass != null) {
132 // Regenerate attributes, since things may have changed
133 codeClass.CustomAttributes.Clear ();
134 AddClassAttributes (codeClass);
135 GenerateClass (map, codeClass, isTopLevel);
136 ExportDerivedTypeAttributes (map, codeClass);
141 if (map.TypeData.Type == typeof(object))
143 exportedAnyType = map;
144 SetMapExported (map, null);
145 foreach (XmlTypeMapping dmap in exportedAnyType.DerivedTypes) {
146 if (IsMapExported (dmap) || !dmap.IncludeInSchema) continue;
147 ExportTypeMapping (dmap, false);
153 codeClass = new CodeTypeDeclaration (map.TypeData.TypeName);
154 SetMapExported (map, codeClass);
156 AddCodeType (codeClass, map.Documentation);
157 codeClass.Attributes = MemberAttributes.Public;
159 codeClass.IsPartial = CodeProvider.Supports(GeneratorSupport.PartialTypes);
160 AddClassAttributes (codeClass);
162 GenerateClass (map, codeClass, isTopLevel);
163 ExportDerivedTypeAttributes (map, codeClass);
165 ExportMembersMapCode (codeClass, (ClassMap)map.ObjectMap, map.XmlTypeNamespace, map.BaseMap);
167 if (map.BaseMap != null && map.BaseMap.TypeData.SchemaType != SchemaTypes.XmlNode)
169 CodeTypeReference ctr = GetDomType (map.BaseMap.TypeData, false);
170 codeClass.BaseTypes.Add (ctr);
171 if (map.BaseMap.IncludeInSchema) {
172 ExportMapCode (map.BaseMap, false);
173 AddInclude (map.BaseMap);
176 ExportDerivedTypes (map, codeClass);
179 void ExportDerivedTypeAttributes (XmlTypeMapping map, CodeTypeDeclaration codeClass)
181 foreach (XmlTypeMapping tm in map.DerivedTypes)
183 GenerateClassInclude (codeClass.CustomAttributes, tm);
184 ExportDerivedTypeAttributes (tm, codeClass);
188 void ExportDerivedTypes (XmlTypeMapping map, CodeTypeDeclaration codeClass)
190 foreach (XmlTypeMapping tm in map.DerivedTypes)
192 if (codeClass.CustomAttributes == null)
193 codeClass.CustomAttributes = new CodeAttributeDeclarationCollection ();
195 ExportMapCode (tm, false);
196 ExportDerivedTypes (tm, codeClass);
200 void ExportMembersMapCode (CodeTypeDeclaration codeClass, ClassMap map, string defaultNamespace, XmlTypeMapping baseMap)
202 ICollection attributes = map.AttributeMembers;
203 ICollection members = map.ElementMembers;
206 if (attributes != null)
207 foreach (XmlTypeMapMemberAttribute attr in attributes)
208 identifiers.AddUnique (attr.Name, attr);
210 foreach (XmlTypeMapMemberElement member in members)
211 identifiers.AddUnique (member.Name, member);
215 if (attributes != null) {
216 foreach (XmlTypeMapMemberAttribute attr in attributes) {
217 if (baseMap != null && DefinedInBaseMap (baseMap, attr)) continue;
218 AddAttributeFieldMember (codeClass, attr, defaultNamespace);
222 members = map.ElementMembers;
226 foreach (XmlTypeMapMemberElement member in members)
228 if (baseMap != null && DefinedInBaseMap (baseMap, member)) continue;
230 Type memType = member.GetType();
231 if (memType == typeof(XmlTypeMapMemberList))
233 AddArrayElementFieldMember (codeClass, (XmlTypeMapMemberList) member, defaultNamespace);
235 else if (memType == typeof(XmlTypeMapMemberFlatList))
237 AddElementFieldMember (codeClass, member, defaultNamespace);
239 else if (memType == typeof(XmlTypeMapMemberAnyElement))
241 AddAnyElementFieldMember (codeClass, member, defaultNamespace);
243 else if (memType == typeof(XmlTypeMapMemberElement))
245 AddElementFieldMember (codeClass, member, defaultNamespace);
249 throw new InvalidOperationException ("Member type " + memType + " not supported");
254 XmlTypeMapMember anyAttrMember = map.DefaultAnyAttributeMember;
255 if (anyAttrMember != null)
257 CodeTypeMember codeField = CreateFieldMember (codeClass, anyAttrMember.TypeData, anyAttrMember.Name);
258 AddComments (codeField, anyAttrMember.Documentation);
259 codeField.Attributes = MemberAttributes.Public;
260 GenerateAnyAttribute (codeField);
264 CodeTypeMember CreateFieldMember (CodeTypeDeclaration codeClass, Type type, string name)
266 return CreateFieldMember (codeClass, new CodeTypeReference(type), name, System.DBNull.Value, null, null);
269 CodeTypeMember CreateFieldMember (CodeTypeDeclaration codeClass, TypeData type, string name)
271 return CreateFieldMember (codeClass, GetDomType (type, false), name, System.DBNull.Value, null, null);
274 CodeTypeMember CreateFieldMember (CodeTypeDeclaration codeClass, XmlTypeMapMember member)
276 return CreateFieldMember (codeClass, GetDomType (member.TypeData, member.RequiresNullable), member.Name, member.DefaultValue, member.TypeData, member.Documentation);
279 CodeTypeMember CreateFieldMember (CodeTypeDeclaration codeClass, CodeTypeReference type, string name, object defaultValue, TypeData defaultType, string documentation)
281 CodeMemberField codeField = null;
282 CodeTypeMember codeProp = null;
284 if ((options & CodeGenerationOptions.GenerateProperties) > 0) {
285 string field = identifiers.AddUnique (CodeIdentifier.MakeCamel (name + "Field"), name);
286 codeField = new CodeMemberField (type, field);
287 codeField.Attributes = MemberAttributes.Private;
288 codeClass.Members.Add (codeField);
290 CodeMemberProperty prop = new CodeMemberProperty ();
293 prop.Attributes = MemberAttributes.Public | MemberAttributes.Final;
295 prop.HasGet = prop.HasSet = true;
297 CodeExpression ce = new CodeFieldReferenceExpression (new CodeThisReferenceExpression(), field);
298 prop.SetStatements.Add (new CodeAssignStatement (ce, new CodePropertySetValueReferenceExpression()));
299 prop.GetStatements.Add (new CodeMethodReturnStatement (ce));
302 codeField = new CodeMemberField (type, name);
303 codeField.Attributes = MemberAttributes.Public;
304 codeProp = codeField;
307 if (defaultValue != System.DBNull.Value)
308 GenerateDefaultAttribute (codeField, codeProp, defaultType, defaultValue);
310 AddComments (codeProp, documentation);
311 codeClass.Members.Add (codeProp);
315 void AddAttributeFieldMember (CodeTypeDeclaration codeClass, XmlTypeMapMemberAttribute attinfo, string defaultNamespace)
317 CodeTypeMember codeField = CreateFieldMember (codeClass, attinfo);
319 CodeAttributeDeclarationCollection attributes = codeField.CustomAttributes;
320 if (attributes == null) attributes = new CodeAttributeDeclarationCollection ();
322 GenerateAttributeMember (attributes, attinfo, defaultNamespace, false);
323 if (attributes.Count > 0) codeField.CustomAttributes = attributes;
325 if (attinfo.MappedType != null) {
326 ExportMapCode (attinfo.MappedType, false);
327 RemoveInclude (attinfo.MappedType);
330 if (attinfo.TypeData.IsValueType && attinfo.IsOptionalValueType)
332 codeField = CreateFieldMember (codeClass, typeof(bool), identifiers.MakeUnique (attinfo.Name + "Specified"));
333 codeField.Attributes = MemberAttributes.Public;
334 GenerateSpecifierMember (codeField);
338 public void AddAttributeMemberAttributes (XmlTypeMapMemberAttribute attinfo, string defaultNamespace, CodeAttributeDeclarationCollection attributes, bool forceUseMemberName)
340 GenerateAttributeMember (attributes, attinfo, defaultNamespace, forceUseMemberName);
343 void AddElementFieldMember (CodeTypeDeclaration codeClass, XmlTypeMapMemberElement member, string defaultNamespace)
345 CodeTypeMember codeField = CreateFieldMember (codeClass, member);
347 CodeAttributeDeclarationCollection attributes = codeField.CustomAttributes;
348 if (attributes == null) attributes = new CodeAttributeDeclarationCollection ();
350 AddElementMemberAttributes (member, defaultNamespace, attributes, false);
351 if (attributes.Count > 0) codeField.CustomAttributes = attributes;
353 if (member.TypeData.IsValueType && member.IsOptionalValueType)
355 codeField = CreateFieldMember (codeClass, typeof(bool), identifiers.MakeUnique (member.Name + "Specified"));
356 codeField.Attributes = MemberAttributes.Public;
357 GenerateSpecifierMember (codeField);
361 public void AddElementMemberAttributes (XmlTypeMapMemberElement member, string defaultNamespace, CodeAttributeDeclarationCollection attributes, bool forceUseMemberName)
363 TypeData defaultType = member.TypeData;
364 bool addAlwaysAttr = false;
366 if (member is XmlTypeMapMemberFlatList)
368 defaultType = defaultType.ListItemTypeData;
369 addAlwaysAttr = true;
372 foreach (XmlTypeMapElementInfo einfo in member.ElementInfo)
374 if (einfo.MappedType != null) {
375 ExportMapCode (einfo.MappedType, false);
376 RemoveInclude (einfo.MappedType);
379 if (ExportExtraElementAttributes (attributes, einfo, defaultNamespace, defaultType))
382 GenerateElementInfoMember (attributes, member, einfo, defaultType, defaultNamespace, addAlwaysAttr, forceUseMemberName | addAlwaysAttr);
385 GenerateElementMember (attributes, member);
388 void AddAnyElementFieldMember (CodeTypeDeclaration codeClass, XmlTypeMapMemberElement member, string defaultNamespace)
390 CodeTypeMember codeField = CreateFieldMember (codeClass, member);
392 CodeAttributeDeclarationCollection attributes = new CodeAttributeDeclarationCollection ();
393 foreach (XmlTypeMapElementInfo einfo in member.ElementInfo)
394 ExportExtraElementAttributes (attributes, einfo, defaultNamespace, einfo.TypeData);
396 if (attributes.Count > 0) codeField.CustomAttributes = attributes;
399 bool DefinedInBaseMap (XmlTypeMapping map, XmlTypeMapMember member)
401 if (((ClassMap)map.ObjectMap).FindMember (member.Name) != null)
403 else if (map.BaseMap != null)
404 return DefinedInBaseMap (map.BaseMap, member);
409 void AddArrayElementFieldMember (CodeTypeDeclaration codeClass, XmlTypeMapMemberList member, string defaultNamespace)
411 CodeTypeMember codeField = CreateFieldMember (codeClass, member.TypeData, member.Name);
413 CodeAttributeDeclarationCollection attributes = new CodeAttributeDeclarationCollection ();
414 AddArrayAttributes (attributes, member, defaultNamespace, false);
416 ListMap listMap = (ListMap) member.ListTypeMapping.ObjectMap;
417 AddArrayItemAttributes (attributes, listMap, member.TypeData.ListItemTypeData, defaultNamespace, 0);
419 if (attributes.Count > 0) codeField.CustomAttributes = attributes;
422 public void AddArrayAttributes (CodeAttributeDeclarationCollection attributes, XmlTypeMapMemberElement member, string defaultNamespace, bool forceUseMemberName)
424 GenerateArrayElement (attributes, member, defaultNamespace, forceUseMemberName);
427 public void AddArrayItemAttributes (CodeAttributeDeclarationCollection attributes, ListMap listMap, TypeData type, string defaultNamespace, int nestingLevel)
429 foreach (XmlTypeMapElementInfo ainfo in listMap.ItemInfo)
432 if (ainfo.MappedType != null) defaultName = ainfo.MappedType.ElementName;
433 else defaultName = ainfo.TypeData.XmlType;
435 GenerateArrayItemAttributes (attributes, listMap, type, ainfo, defaultName, defaultNamespace, nestingLevel);
436 if (ainfo.MappedType != null) {
437 if (!IsMapExported (ainfo.MappedType) && includeArrayTypes)
438 AddInclude (ainfo.MappedType);
439 ExportMapCode (ainfo.MappedType, false);
443 if (listMap.IsMultiArray)
445 XmlTypeMapping nmap = listMap.NestedArrayMapping;
446 AddArrayItemAttributes (attributes, (ListMap) nmap.ObjectMap, nmap.TypeData.ListItemTypeData, defaultNamespace, nestingLevel + 1);
450 void ExportArrayCode (XmlTypeMapping map)
452 ListMap listMap = (ListMap) map.ObjectMap;
453 foreach (XmlTypeMapElementInfo ainfo in listMap.ItemInfo)
455 if (ainfo.MappedType != null) {
456 if (!IsMapExported (ainfo.MappedType) && includeArrayTypes)
457 AddInclude (ainfo.MappedType);
458 ExportMapCode (ainfo.MappedType, false);
463 bool ExportExtraElementAttributes (CodeAttributeDeclarationCollection attributes, XmlTypeMapElementInfo einfo, string defaultNamespace, TypeData defaultType)
465 if (einfo.IsTextElement) {
466 GenerateTextElementAttribute (attributes, einfo, defaultType);
469 else if (einfo.IsUnnamedAnyElement) {
470 GenerateUnnamedAnyElementAttribute (attributes, einfo, defaultNamespace);
476 void ExportEnumCode (XmlTypeMapping map, bool isTopLevel)
478 if (IsMapExported (map)) return;
480 CodeTypeDeclaration codeEnum = new CodeTypeDeclaration (map.TypeData.TypeName);
481 SetMapExported (map, codeEnum);
483 codeEnum.Attributes = MemberAttributes.Public;
484 codeEnum.IsEnum = true;
485 AddCodeType (codeEnum, map.Documentation);
487 EnumMap emap = (EnumMap) map.ObjectMap;
489 codeEnum.CustomAttributes.Add (new CodeAttributeDeclaration ("System.FlagsAttribute"));
491 CodeAttributeDeclaration generatedCodeAttribute = new CodeAttributeDeclaration (
492 new CodeTypeReference (typeof(GeneratedCodeAttribute)));
493 generatedCodeAttribute.Arguments.Add (new CodeAttributeArgument (
494 new CodePrimitiveExpression ("System.Xml")));
495 generatedCodeAttribute.Arguments.Add (new CodeAttributeArgument (
496 new CodePrimitiveExpression (Consts.FxFileVersion)));
497 codeEnum.CustomAttributes.Add (generatedCodeAttribute);
499 codeEnum.CustomAttributes.Add (new CodeAttributeDeclaration (
500 new CodeTypeReference (typeof (SerializableAttribute))));
502 GenerateEnum (map, codeEnum, isTopLevel);
505 foreach (EnumMap.EnumMapMember emem in emap.Members)
507 CodeMemberField codeField = new CodeMemberField ("", emem.EnumName);
509 codeField.InitExpression = new CodePrimitiveExpression (flag);
513 AddComments (codeField, emem.Documentation);
515 GenerateEnumItem (codeField, emem);
516 codeEnum.Members.Add (codeField);
520 void AddInclude (XmlTypeMapping map)
522 if (!includeMaps.ContainsKey (map.TypeData.FullTypeName))
523 includeMaps [map.TypeData.FullTypeName] = map;
526 void RemoveInclude (XmlTypeMapping map)
528 includeMaps.Remove (map.TypeData.FullTypeName);
533 #region Helper methods
535 bool IsMapExported (XmlTypeMapping map)
537 if (exportedMaps.Contains (map.TypeData.FullTypeName)) return true;
541 void SetMapExported (XmlTypeMapping map, CodeTypeDeclaration declaration)
543 exportedMaps.Add (map.TypeData.FullTypeName, declaration);
546 CodeTypeDeclaration GetMapDeclaration (XmlTypeMapping map)
548 return exportedMaps [map.TypeData.FullTypeName] as CodeTypeDeclaration;
551 public static void AddCustomAttribute (CodeTypeMember ctm, CodeAttributeDeclaration att, bool addIfNoParams)
553 if (att.Arguments.Count == 0 && !addIfNoParams) return;
555 if (ctm.CustomAttributes == null) ctm.CustomAttributes = new CodeAttributeDeclarationCollection ();
556 ctm.CustomAttributes.Add (att);
559 public static void AddCustomAttribute (CodeTypeMember ctm, string name, params CodeAttributeArgument[] args)
561 if (ctm.CustomAttributes == null) ctm.CustomAttributes = new CodeAttributeDeclarationCollection ();
562 ctm.CustomAttributes.Add (new CodeAttributeDeclaration (name, args));
565 public static CodeAttributeArgument GetArg (string name, object value)
567 return new CodeAttributeArgument (name, new CodePrimitiveExpression(value));
570 public static CodeAttributeArgument GetArg (object value)
572 return new CodeAttributeArgument (new CodePrimitiveExpression(value));
575 public static CodeAttributeArgument GetTypeArg (string name, string typeName)
577 return new CodeAttributeArgument (name, new CodeTypeOfExpression(typeName));
580 public static CodeAttributeArgument GetEnumArg (string name, string enumType, string enumValue)
582 return new CodeAttributeArgument (name, new CodeFieldReferenceExpression (new CodeTypeReferenceExpression(enumType), enumValue));
585 public static void AddComments (CodeTypeMember member, string comments)
587 if (comments == null || comments == "") member.Comments.Add (new CodeCommentStatement ("<remarks/>", true));
588 else member.Comments.Add (new CodeCommentStatement ("<remarks>\n" + comments + "\n</remarks>", true));
591 void AddCodeType (CodeTypeDeclaration type, string comments)
593 AddComments (type, comments);
594 codeNamespace.Types.Add (type);
597 void AddClassAttributes (CodeTypeDeclaration codeClass)
599 CodeAttributeDeclaration generatedCodeAttribute = new CodeAttributeDeclaration (
600 new CodeTypeReference (typeof (GeneratedCodeAttribute)));
601 generatedCodeAttribute.Arguments.Add (new CodeAttributeArgument (
602 new CodePrimitiveExpression ("System.Xml")));
603 generatedCodeAttribute.Arguments.Add (new CodeAttributeArgument (
604 new CodePrimitiveExpression (Consts.FxFileVersion)));
605 codeClass.CustomAttributes.Add (generatedCodeAttribute);
607 codeClass.CustomAttributes.Add (new CodeAttributeDeclaration (
608 new CodeTypeReference (typeof (SerializableAttribute))));
609 codeClass.CustomAttributes.Add (new CodeAttributeDeclaration (
610 new CodeTypeReference (typeof (DebuggerStepThroughAttribute))));
612 CodeAttributeDeclaration designerCategoryAttribute = new CodeAttributeDeclaration (
613 new CodeTypeReference (typeof (DesignerCategoryAttribute)));
614 designerCategoryAttribute.Arguments.Add (new CodeAttributeArgument (
615 new CodePrimitiveExpression ("code")));
616 codeClass.CustomAttributes.Add (designerCategoryAttribute);
619 CodeTypeReference GetDomType (TypeData data, bool requiresNullable)
621 if (data.IsValueType && (data.IsNullable || requiresNullable))
622 return new CodeTypeReference ("System.Nullable", new CodeTypeReference (data.FullTypeName));
623 if (data.SchemaType == SchemaTypes.Array)
624 return new CodeTypeReference (GetDomType (data.ListItemTypeData, false),1);
626 return new CodeTypeReference (data.FullTypeName);
631 #region Private Properties
633 private CodeDomProvider CodeProvider {
635 if (codeProvider == null) {
636 codeProvider = new CSharpCodeProvider ();
644 #region Overridable methods
646 protected virtual void GenerateClass (XmlTypeMapping map, CodeTypeDeclaration codeClass, bool isTopLevel)
650 protected virtual void GenerateClassInclude (CodeAttributeDeclarationCollection attributes, XmlTypeMapping map)
654 protected virtual void GenerateAnyAttribute (CodeTypeMember codeField)
658 protected virtual void GenerateDefaultAttribute (CodeMemberField internalField, CodeTypeMember externalField, TypeData typeData, object defaultValue)
660 if (typeData.Type == null)
662 // It must be an enumeration defined in the schema.
663 if (typeData.SchemaType != SchemaTypes.Enum)
664 throw new InvalidOperationException ("Type " + typeData.TypeName + " not supported");
666 IFormattable defaultValueFormattable = defaultValue as IFormattable;
667 CodeFieldReferenceExpression fref = new CodeFieldReferenceExpression (new CodeTypeReferenceExpression (GetDomType (typeData, false)), defaultValueFormattable != null ? defaultValueFormattable.ToString(null, CultureInfo.InvariantCulture) : defaultValue.ToString ());
668 CodeAttributeArgument arg = new CodeAttributeArgument (fref);
669 AddCustomAttribute (externalField, "System.ComponentModel.DefaultValue", arg);
670 //internalField.InitExpression = fref;
674 defaultValue = defaultValue is decimal ? (object) ('"' + ((decimal) defaultValue).ToString (CultureInfo.InvariantCulture) + '"') : defaultValue;
675 AddCustomAttribute (externalField, "System.ComponentModel.DefaultValue", GetArg (defaultValue));
676 //internalField.InitExpression = new CodePrimitiveExpression (defaultValue);
680 protected virtual void GenerateAttributeMember (CodeAttributeDeclarationCollection attributes, XmlTypeMapMemberAttribute attinfo, string defaultNamespace, bool forceUseMemberName)
684 protected virtual void GenerateElementInfoMember (CodeAttributeDeclarationCollection attributes, XmlTypeMapMemberElement member, XmlTypeMapElementInfo einfo, TypeData defaultType, string defaultNamespace, bool addAlwaysAttr, bool forceUseMemberName)
688 protected virtual void GenerateElementMember (CodeAttributeDeclarationCollection attributes, XmlTypeMapMemberElement member)
692 protected virtual void GenerateArrayElement (CodeAttributeDeclarationCollection attributes, XmlTypeMapMemberElement member, string defaultNamespace, bool forceUseMemberName)
696 protected virtual void GenerateArrayItemAttributes (CodeAttributeDeclarationCollection attributes, ListMap listMap, TypeData type, XmlTypeMapElementInfo ainfo, string defaultName, string defaultNamespace, int nestingLevel)
700 protected virtual void GenerateTextElementAttribute (CodeAttributeDeclarationCollection attributes, XmlTypeMapElementInfo einfo, TypeData defaultType)
704 protected virtual void GenerateUnnamedAnyElementAttribute (CodeAttributeDeclarationCollection attributes, XmlTypeMapElementInfo einfo, string defaultNamespace)
708 protected virtual void GenerateEnum (XmlTypeMapping map, CodeTypeDeclaration codeEnum, bool isTopLevel)
712 protected virtual void GenerateEnumItem (CodeMemberField codeField, EnumMap.EnumMapMember emem)
716 protected virtual void GenerateSpecifierMember (CodeTypeMember codeField)