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;
35 using System.ComponentModel;
36 using System.Diagnostics;
38 using System.Globalization;
39 using System.Xml.Schema;
41 using Microsoft.CSharp;
43 namespace System.Xml.Serialization {
44 internal class MapCodeGenerator {
46 CodeNamespace codeNamespace;
47 CodeCompileUnit codeCompileUnit;
48 CodeAttributeDeclarationCollection includeMetadata;
49 XmlTypeMapping exportedAnyType = null;
50 protected bool includeArrayTypes;
51 CodeDomProvider codeProvider;
52 CodeGenerationOptions options;
53 CodeIdentifiers identifiers;
55 Hashtable exportedMaps = new Hashtable ();
56 Hashtable includeMaps = new Hashtable ();
58 public MapCodeGenerator (CodeNamespace codeNamespace, CodeCompileUnit codeCompileUnit, CodeGenerationOptions options)
60 this.codeCompileUnit = codeCompileUnit;
61 this.codeNamespace = codeNamespace;
62 this.options = options;
63 this.identifiers = new CodeIdentifiers ();
66 public MapCodeGenerator (CodeNamespace codeNamespace, CodeCompileUnit codeCompileUnit, CodeDomProvider codeProvider, CodeGenerationOptions options, Hashtable mappings)
68 this.codeCompileUnit = codeCompileUnit;
69 this.codeNamespace = codeNamespace;
70 this.codeProvider = codeProvider;
71 this.options = options;
73 this.identifiers = new CodeIdentifiers ((codeProvider.LanguageOptions & LanguageOptions.CaseInsensitive) == 0);
75 this.identifiers = new CodeIdentifiers ();
77 // this.mappings = mappings;
80 public CodeAttributeDeclarationCollection IncludeMetadata
84 if (includeMetadata != null) return includeMetadata;
85 includeMetadata = new CodeAttributeDeclarationCollection ();
87 foreach (XmlTypeMapping map in includeMaps.Values)
88 GenerateClassInclude (includeMetadata, map);
90 return includeMetadata;
94 #region Code generation methods
96 public void ExportMembersMapping (XmlMembersMapping xmlMembersMapping)
98 CodeTypeDeclaration dummyClass = new CodeTypeDeclaration ();
99 ExportMembersMapCode (dummyClass, (ClassMap)xmlMembersMapping.ObjectMap, xmlMembersMapping.Namespace, null);
102 public void ExportTypeMapping (XmlTypeMapping xmlTypeMapping, bool isTopLevel)
104 ExportMapCode (xmlTypeMapping, isTopLevel);
105 RemoveInclude (xmlTypeMapping);
108 void ExportMapCode (XmlTypeMapping map, bool isTopLevel)
110 switch (map.TypeData.SchemaType)
112 case SchemaTypes.Enum:
113 ExportEnumCode (map, isTopLevel);
116 case SchemaTypes.Array:
117 ExportArrayCode (map);
120 case SchemaTypes.Class:
121 ExportClassCode (map, isTopLevel);
124 case SchemaTypes.XmlSerializable:
125 case SchemaTypes.XmlNode:
126 case SchemaTypes.Primitive:
132 void ExportClassCode (XmlTypeMapping map, bool isTopLevel)
134 CodeTypeDeclaration codeClass;
135 if (IsMapExported (map)) {
136 codeClass = GetMapDeclaration (map);
137 if (codeClass != null) {
138 // Regenerate attributes, since things may have changed
139 codeClass.CustomAttributes.Clear ();
141 AddClassAttributes (codeClass);
143 GenerateClass (map, codeClass, isTopLevel);
144 ExportDerivedTypeAttributes (map, codeClass);
149 if (map.TypeData.Type == typeof(object))
151 exportedAnyType = map;
152 SetMapExported (map, null);
153 foreach (XmlTypeMapping dmap in exportedAnyType.DerivedTypes) {
154 if (IsMapExported (dmap) || !dmap.IncludeInSchema) continue;
155 ExportTypeMapping (dmap, false);
161 codeClass = new CodeTypeDeclaration (map.TypeData.TypeName);
162 SetMapExported (map, codeClass);
164 AddCodeType (codeClass, map.Documentation);
165 codeClass.Attributes = MemberAttributes.Public;
168 codeClass.IsPartial = CodeProvider.Supports(GeneratorSupport.PartialTypes);
169 AddClassAttributes (codeClass);
172 GenerateClass (map, codeClass, isTopLevel);
173 ExportDerivedTypeAttributes (map, codeClass);
175 ExportMembersMapCode (codeClass, (ClassMap)map.ObjectMap, map.XmlTypeNamespace, map.BaseMap);
177 if (map.BaseMap != null && map.BaseMap.TypeData.SchemaType != SchemaTypes.XmlNode)
179 CodeTypeReference ctr = GetDomType (map.BaseMap.TypeData, false);
180 codeClass.BaseTypes.Add (ctr);
181 if (map.BaseMap.IncludeInSchema) {
182 ExportMapCode (map.BaseMap, false);
183 AddInclude (map.BaseMap);
186 ExportDerivedTypes (map, codeClass);
189 void ExportDerivedTypeAttributes (XmlTypeMapping map, CodeTypeDeclaration codeClass)
191 foreach (XmlTypeMapping tm in map.DerivedTypes)
193 GenerateClassInclude (codeClass.CustomAttributes, tm);
194 ExportDerivedTypeAttributes (tm, codeClass);
198 void ExportDerivedTypes (XmlTypeMapping map, CodeTypeDeclaration codeClass)
200 foreach (XmlTypeMapping tm in map.DerivedTypes)
202 if (codeClass.CustomAttributes == null)
203 codeClass.CustomAttributes = new CodeAttributeDeclarationCollection ();
205 ExportMapCode (tm, false);
206 ExportDerivedTypes (tm, codeClass);
210 void ExportMembersMapCode (CodeTypeDeclaration codeClass, ClassMap map, string defaultNamespace, XmlTypeMapping baseMap)
212 ICollection attributes = map.AttributeMembers;
213 ICollection members = map.ElementMembers;
216 if (attributes != null)
217 foreach (XmlTypeMapMemberAttribute attr in attributes)
218 identifiers.AddUnique (attr.Name, attr);
220 foreach (XmlTypeMapMemberElement member in members)
221 identifiers.AddUnique (member.Name, member);
225 if (attributes != null) {
226 foreach (XmlTypeMapMemberAttribute attr in attributes) {
227 if (baseMap != null && DefinedInBaseMap (baseMap, attr)) continue;
228 AddAttributeFieldMember (codeClass, attr, defaultNamespace);
232 members = map.ElementMembers;
236 foreach (XmlTypeMapMemberElement member in members)
238 if (baseMap != null && DefinedInBaseMap (baseMap, member)) continue;
240 Type memType = member.GetType();
241 if (memType == typeof(XmlTypeMapMemberList))
243 AddArrayElementFieldMember (codeClass, (XmlTypeMapMemberList) member, defaultNamespace);
245 else if (memType == typeof(XmlTypeMapMemberFlatList))
247 AddElementFieldMember (codeClass, member, defaultNamespace);
249 else if (memType == typeof(XmlTypeMapMemberAnyElement))
251 AddAnyElementFieldMember (codeClass, member, defaultNamespace);
253 else if (memType == typeof(XmlTypeMapMemberElement))
255 AddElementFieldMember (codeClass, member, defaultNamespace);
259 throw new InvalidOperationException ("Member type " + memType + " not supported");
264 XmlTypeMapMember anyAttrMember = map.DefaultAnyAttributeMember;
265 if (anyAttrMember != null)
267 CodeTypeMember codeField = CreateFieldMember (codeClass, anyAttrMember.TypeData, anyAttrMember.Name);
268 AddComments (codeField, anyAttrMember.Documentation);
269 codeField.Attributes = MemberAttributes.Public;
270 GenerateAnyAttribute (codeField);
274 CodeTypeMember CreateFieldMember (CodeTypeDeclaration codeClass, Type type, string name)
276 return CreateFieldMember (codeClass, new CodeTypeReference(type), name, System.DBNull.Value, null, null);
279 CodeTypeMember CreateFieldMember (CodeTypeDeclaration codeClass, TypeData type, string name)
281 return CreateFieldMember (codeClass, GetDomType (type, false), name, System.DBNull.Value, null, null);
284 CodeTypeMember CreateFieldMember (CodeTypeDeclaration codeClass, XmlTypeMapMember member)
286 return CreateFieldMember (codeClass, GetDomType (member.TypeData, member.RequiresNullable), member.Name, member.DefaultValue, member.TypeData, member.Documentation);
289 CodeTypeMember CreateFieldMember (CodeTypeDeclaration codeClass, CodeTypeReference type, string name, object defaultValue, TypeData defaultType, string documentation)
291 CodeMemberField codeField = null;
292 CodeTypeMember codeProp = null;
294 if ((options & CodeGenerationOptions.GenerateProperties) > 0) {
295 string field = identifiers.AddUnique (CodeIdentifier.MakeCamel (name + "Field"), name);
296 codeField = new CodeMemberField (type, field);
297 codeField.Attributes = MemberAttributes.Private;
298 codeClass.Members.Add (codeField);
300 CodeMemberProperty prop = new CodeMemberProperty ();
303 prop.Attributes = MemberAttributes.Public | MemberAttributes.Final;
305 prop.HasGet = prop.HasSet = true;
307 CodeExpression ce = new CodeFieldReferenceExpression (new CodeThisReferenceExpression(), field);
308 prop.SetStatements.Add (new CodeAssignStatement (ce, new CodePropertySetValueReferenceExpression()));
309 prop.GetStatements.Add (new CodeMethodReturnStatement (ce));
312 codeField = new CodeMemberField (type, name);
313 codeField.Attributes = MemberAttributes.Public;
314 codeProp = codeField;
317 if (defaultValue != System.DBNull.Value)
318 GenerateDefaultAttribute (codeField, codeProp, defaultType, defaultValue);
320 AddComments (codeProp, documentation);
321 codeClass.Members.Add (codeProp);
325 void AddAttributeFieldMember (CodeTypeDeclaration codeClass, XmlTypeMapMemberAttribute attinfo, string defaultNamespace)
327 CodeTypeMember codeField = CreateFieldMember (codeClass, attinfo);
329 CodeAttributeDeclarationCollection attributes = codeField.CustomAttributes;
330 if (attributes == null) attributes = new CodeAttributeDeclarationCollection ();
332 GenerateAttributeMember (attributes, attinfo, defaultNamespace, false);
333 if (attributes.Count > 0) codeField.CustomAttributes = attributes;
335 if (attinfo.MappedType != null) {
336 ExportMapCode (attinfo.MappedType, false);
337 RemoveInclude (attinfo.MappedType);
340 if (attinfo.TypeData.IsValueType && attinfo.IsOptionalValueType)
342 codeField = CreateFieldMember (codeClass, typeof(bool), identifiers.MakeUnique (attinfo.Name + "Specified"));
343 codeField.Attributes = MemberAttributes.Public;
344 GenerateSpecifierMember (codeField);
348 public void AddAttributeMemberAttributes (XmlTypeMapMemberAttribute attinfo, string defaultNamespace, CodeAttributeDeclarationCollection attributes, bool forceUseMemberName)
350 GenerateAttributeMember (attributes, attinfo, defaultNamespace, forceUseMemberName);
353 void AddElementFieldMember (CodeTypeDeclaration codeClass, XmlTypeMapMemberElement member, string defaultNamespace)
355 CodeTypeMember codeField = CreateFieldMember (codeClass, member);
357 CodeAttributeDeclarationCollection attributes = codeField.CustomAttributes;
358 if (attributes == null) attributes = new CodeAttributeDeclarationCollection ();
360 AddElementMemberAttributes (member, defaultNamespace, attributes, false);
361 if (attributes.Count > 0) codeField.CustomAttributes = attributes;
363 if (member.TypeData.IsValueType && member.IsOptionalValueType)
365 codeField = CreateFieldMember (codeClass, typeof(bool), identifiers.MakeUnique (member.Name + "Specified"));
366 codeField.Attributes = MemberAttributes.Public;
367 GenerateSpecifierMember (codeField);
371 public void AddElementMemberAttributes (XmlTypeMapMemberElement member, string defaultNamespace, CodeAttributeDeclarationCollection attributes, bool forceUseMemberName)
373 TypeData defaultType = member.TypeData;
374 bool addAlwaysAttr = false;
376 if (member is XmlTypeMapMemberFlatList)
378 defaultType = defaultType.ListItemTypeData;
379 addAlwaysAttr = true;
382 foreach (XmlTypeMapElementInfo einfo in member.ElementInfo)
384 if (einfo.MappedType != null) {
385 ExportMapCode (einfo.MappedType, false);
386 RemoveInclude (einfo.MappedType);
389 if (ExportExtraElementAttributes (attributes, einfo, defaultNamespace, defaultType))
392 GenerateElementInfoMember (attributes, member, einfo, defaultType, defaultNamespace, addAlwaysAttr, forceUseMemberName);
395 GenerateElementMember (attributes, member);
398 void AddAnyElementFieldMember (CodeTypeDeclaration codeClass, XmlTypeMapMemberElement member, string defaultNamespace)
400 CodeTypeMember codeField = CreateFieldMember (codeClass, member);
402 CodeAttributeDeclarationCollection attributes = new CodeAttributeDeclarationCollection ();
403 foreach (XmlTypeMapElementInfo einfo in member.ElementInfo)
404 ExportExtraElementAttributes (attributes, einfo, defaultNamespace, einfo.TypeData);
406 if (attributes.Count > 0) codeField.CustomAttributes = attributes;
409 bool DefinedInBaseMap (XmlTypeMapping map, XmlTypeMapMember member)
411 if (((ClassMap)map.ObjectMap).FindMember (member.Name) != null)
413 else if (map.BaseMap != null)
414 return DefinedInBaseMap (map.BaseMap, member);
419 void AddArrayElementFieldMember (CodeTypeDeclaration codeClass, XmlTypeMapMemberList member, string defaultNamespace)
421 CodeTypeMember codeField = CreateFieldMember (codeClass, member.TypeData, member.Name);
423 CodeAttributeDeclarationCollection attributes = new CodeAttributeDeclarationCollection ();
424 AddArrayAttributes (attributes, member, defaultNamespace, false);
426 ListMap listMap = (ListMap) member.ListTypeMapping.ObjectMap;
427 AddArrayItemAttributes (attributes, listMap, member.TypeData.ListItemTypeData, defaultNamespace, 0);
429 if (attributes.Count > 0) codeField.CustomAttributes = attributes;
432 public void AddArrayAttributes (CodeAttributeDeclarationCollection attributes, XmlTypeMapMemberElement member, string defaultNamespace, bool forceUseMemberName)
434 GenerateArrayElement (attributes, member, defaultNamespace, forceUseMemberName);
437 public void AddArrayItemAttributes (CodeAttributeDeclarationCollection attributes, ListMap listMap, TypeData type, string defaultNamespace, int nestingLevel)
439 foreach (XmlTypeMapElementInfo ainfo in listMap.ItemInfo)
442 if (ainfo.MappedType != null) defaultName = ainfo.MappedType.ElementName;
443 else defaultName = ainfo.TypeData.XmlType;
445 GenerateArrayItemAttributes (attributes, listMap, type, ainfo, defaultName, defaultNamespace, nestingLevel);
446 if (ainfo.MappedType != null) {
447 if (!IsMapExported (ainfo.MappedType) && includeArrayTypes)
448 AddInclude (ainfo.MappedType);
449 ExportMapCode (ainfo.MappedType, false);
453 if (listMap.IsMultiArray)
455 XmlTypeMapping nmap = listMap.NestedArrayMapping;
456 AddArrayItemAttributes (attributes, (ListMap) nmap.ObjectMap, nmap.TypeData.ListItemTypeData, defaultNamespace, nestingLevel + 1);
460 void ExportArrayCode (XmlTypeMapping map)
462 ListMap listMap = (ListMap) map.ObjectMap;
463 foreach (XmlTypeMapElementInfo ainfo in listMap.ItemInfo)
465 if (ainfo.MappedType != null) {
466 if (!IsMapExported (ainfo.MappedType) && includeArrayTypes)
467 AddInclude (ainfo.MappedType);
468 ExportMapCode (ainfo.MappedType, false);
473 bool ExportExtraElementAttributes (CodeAttributeDeclarationCollection attributes, XmlTypeMapElementInfo einfo, string defaultNamespace, TypeData defaultType)
475 if (einfo.IsTextElement) {
476 GenerateTextElementAttribute (attributes, einfo, defaultType);
479 else if (einfo.IsUnnamedAnyElement) {
480 GenerateUnnamedAnyElementAttribute (attributes, einfo, defaultNamespace);
486 void ExportEnumCode (XmlTypeMapping map, bool isTopLevel)
488 if (IsMapExported (map)) return;
490 CodeTypeDeclaration codeEnum = new CodeTypeDeclaration (map.TypeData.TypeName);
491 SetMapExported (map, codeEnum);
493 codeEnum.Attributes = MemberAttributes.Public;
494 codeEnum.IsEnum = true;
495 AddCodeType (codeEnum, map.Documentation);
497 EnumMap emap = (EnumMap) map.ObjectMap;
499 codeEnum.CustomAttributes.Add (new CodeAttributeDeclaration ("System.FlagsAttribute"));
502 CodeAttributeDeclaration generatedCodeAttribute = new CodeAttributeDeclaration (
503 new CodeTypeReference (typeof(GeneratedCodeAttribute)));
504 generatedCodeAttribute.Arguments.Add (new CodeAttributeArgument (
505 new CodePrimitiveExpression ("System.Xml")));
506 generatedCodeAttribute.Arguments.Add (new CodeAttributeArgument (
507 new CodePrimitiveExpression (Consts.FxFileVersion)));
508 codeEnum.CustomAttributes.Add (generatedCodeAttribute);
510 codeEnum.CustomAttributes.Add (new CodeAttributeDeclaration (
511 new CodeTypeReference (typeof (SerializableAttribute))));
514 GenerateEnum (map, codeEnum, isTopLevel);
517 foreach (EnumMap.EnumMapMember emem in emap.Members)
519 CodeMemberField codeField = new CodeMemberField ("", emem.EnumName);
521 codeField.InitExpression = new CodePrimitiveExpression (flag);
525 AddComments (codeField, emem.Documentation);
527 GenerateEnumItem (codeField, emem);
528 codeEnum.Members.Add (codeField);
532 void AddInclude (XmlTypeMapping map)
534 if (!includeMaps.ContainsKey (map.TypeData.FullTypeName))
535 includeMaps [map.TypeData.FullTypeName] = map;
538 void RemoveInclude (XmlTypeMapping map)
540 includeMaps.Remove (map.TypeData.FullTypeName);
545 #region Helper methods
547 bool IsMapExported (XmlTypeMapping map)
549 if (exportedMaps.Contains (map.TypeData.FullTypeName)) return true;
553 void SetMapExported (XmlTypeMapping map, CodeTypeDeclaration declaration)
555 exportedMaps.Add (map.TypeData.FullTypeName, declaration);
558 CodeTypeDeclaration GetMapDeclaration (XmlTypeMapping map)
560 return exportedMaps [map.TypeData.FullTypeName] as CodeTypeDeclaration;
563 public static void AddCustomAttribute (CodeTypeMember ctm, CodeAttributeDeclaration att, bool addIfNoParams)
565 if (att.Arguments.Count == 0 && !addIfNoParams) return;
567 if (ctm.CustomAttributes == null) ctm.CustomAttributes = new CodeAttributeDeclarationCollection ();
568 ctm.CustomAttributes.Add (att);
571 public static void AddCustomAttribute (CodeTypeMember ctm, string name, params CodeAttributeArgument[] args)
573 if (ctm.CustomAttributes == null) ctm.CustomAttributes = new CodeAttributeDeclarationCollection ();
574 ctm.CustomAttributes.Add (new CodeAttributeDeclaration (name, args));
577 public static CodeAttributeArgument GetArg (string name, object value)
579 return new CodeAttributeArgument (name, new CodePrimitiveExpression(value));
582 public static CodeAttributeArgument GetArg (object value)
584 return new CodeAttributeArgument (new CodePrimitiveExpression(value));
587 public static CodeAttributeArgument GetTypeArg (string name, string typeName)
589 return new CodeAttributeArgument (name, new CodeTypeOfExpression(typeName));
592 public static CodeAttributeArgument GetEnumArg (string name, string enumType, string enumValue)
594 return new CodeAttributeArgument (name, new CodeFieldReferenceExpression (new CodeTypeReferenceExpression(enumType), enumValue));
597 public static void AddComments (CodeTypeMember member, string comments)
599 if (comments == null || comments == "") member.Comments.Add (new CodeCommentStatement ("<remarks/>", true));
600 else member.Comments.Add (new CodeCommentStatement ("<remarks>\n" + comments + "\n</remarks>", true));
603 void AddCodeType (CodeTypeDeclaration type, string comments)
605 AddComments (type, comments);
606 codeNamespace.Types.Add (type);
610 void AddClassAttributes (CodeTypeDeclaration codeClass)
612 CodeAttributeDeclaration generatedCodeAttribute = new CodeAttributeDeclaration (
613 new CodeTypeReference (typeof (GeneratedCodeAttribute)));
614 generatedCodeAttribute.Arguments.Add (new CodeAttributeArgument (
615 new CodePrimitiveExpression ("System.Xml")));
616 generatedCodeAttribute.Arguments.Add (new CodeAttributeArgument (
617 new CodePrimitiveExpression (Consts.FxFileVersion)));
618 codeClass.CustomAttributes.Add (generatedCodeAttribute);
620 codeClass.CustomAttributes.Add (new CodeAttributeDeclaration (
621 new CodeTypeReference (typeof (SerializableAttribute))));
622 codeClass.CustomAttributes.Add (new CodeAttributeDeclaration (
623 new CodeTypeReference (typeof (DebuggerStepThroughAttribute))));
625 CodeAttributeDeclaration designerCategoryAttribute = new CodeAttributeDeclaration (
626 new CodeTypeReference (typeof (DesignerCategoryAttribute)));
627 designerCategoryAttribute.Arguments.Add (new CodeAttributeArgument (
628 new CodePrimitiveExpression ("code")));
629 codeClass.CustomAttributes.Add (designerCategoryAttribute);
633 CodeTypeReference GetDomType (TypeData data, bool requiresNullable)
636 if (data.IsValueType && (data.IsNullable || requiresNullable))
637 return new CodeTypeReference ("System.Nullable", new CodeTypeReference (data.FullTypeName));
639 if (data.SchemaType == SchemaTypes.Array)
640 return new CodeTypeReference (GetDomType (data.ListItemTypeData, false),1);
642 return new CodeTypeReference (data.FullTypeName);
647 #region Private Properties
650 private CodeDomProvider CodeProvider {
652 if (codeProvider == null) {
653 codeProvider = new CSharpCodeProvider ();
662 #region Overridable methods
664 protected virtual void GenerateClass (XmlTypeMapping map, CodeTypeDeclaration codeClass, bool isTopLevel)
668 protected virtual void GenerateClassInclude (CodeAttributeDeclarationCollection attributes, XmlTypeMapping map)
672 protected virtual void GenerateAnyAttribute (CodeTypeMember codeField)
676 protected virtual void GenerateDefaultAttribute (CodeMemberField internalField, CodeTypeMember externalField, TypeData typeData, object defaultValue)
678 if (typeData.Type == null)
680 // It must be an enumeration defined in the schema.
681 if (typeData.SchemaType != SchemaTypes.Enum)
682 throw new InvalidOperationException ("Type " + typeData.TypeName + " not supported");
684 IFormattable defaultValueFormattable = defaultValue as IFormattable;
685 CodeFieldReferenceExpression fref = new CodeFieldReferenceExpression (new CodeTypeReferenceExpression (GetDomType (typeData, false)), defaultValueFormattable != null ? defaultValueFormattable.ToString(null, CultureInfo.InvariantCulture) : defaultValue.ToString ());
686 CodeAttributeArgument arg = new CodeAttributeArgument (fref);
687 AddCustomAttribute (externalField, "System.ComponentModel.DefaultValue", arg);
688 internalField.InitExpression = fref;
692 AddCustomAttribute (externalField, "System.ComponentModel.DefaultValue", GetArg (defaultValue));
693 internalField.InitExpression = new CodePrimitiveExpression (defaultValue);
697 protected virtual void GenerateAttributeMember (CodeAttributeDeclarationCollection attributes, XmlTypeMapMemberAttribute attinfo, string defaultNamespace, bool forceUseMemberName)
701 protected virtual void GenerateElementInfoMember (CodeAttributeDeclarationCollection attributes, XmlTypeMapMemberElement member, XmlTypeMapElementInfo einfo, TypeData defaultType, string defaultNamespace, bool addAlwaysAttr, bool forceUseMemberName)
705 protected virtual void GenerateElementMember (CodeAttributeDeclarationCollection attributes, XmlTypeMapMemberElement member)
709 protected virtual void GenerateArrayElement (CodeAttributeDeclarationCollection attributes, XmlTypeMapMemberElement member, string defaultNamespace, bool forceUseMemberName)
713 protected virtual void GenerateArrayItemAttributes (CodeAttributeDeclarationCollection attributes, ListMap listMap, TypeData type, XmlTypeMapElementInfo ainfo, string defaultName, string defaultNamespace, int nestingLevel)
717 protected virtual void GenerateTextElementAttribute (CodeAttributeDeclarationCollection attributes, XmlTypeMapElementInfo einfo, TypeData defaultType)
721 protected virtual void GenerateUnnamedAnyElementAttribute (CodeAttributeDeclarationCollection attributes, XmlTypeMapElementInfo einfo, string defaultNamespace)
725 protected virtual void GenerateEnum (XmlTypeMapping map, CodeTypeDeclaration codeEnum, bool isTopLevel)
729 protected virtual void GenerateEnumItem (CodeMemberField codeField, EnumMap.EnumMapMember emem)
733 protected virtual void GenerateSpecifierMember (CodeTypeMember codeField)