Updates referencesource to .NET 4.7
[mono.git] / mcs / class / referencesource / System.Data.Entity.Design / System / Data / EntityModel / Emitters / SchemaTypeEmitter.cs
1 //---------------------------------------------------------------------
2 // <copyright file="SchemaTypeEmitter.cs" company="Microsoft">
3 //      Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>
5 //
6 // @owner       Microsoft
7 // @backupOwner Microsoft
8 //---------------------------------------------------------------------
9
10 using System;
11 using System.CodeDom;
12 using System.Data.EntityModel.SchemaObjectModel;
13 using Som = System.Data.EntityModel.SchemaObjectModel;
14 using System.Data.Metadata.Edm;
15 using System.Reflection;
16 using System.Diagnostics;
17 using System.Collections.Generic;
18 using System.Data.Entity.Design;
19 using System.Data.Entity.Design.SsdlGenerator;
20
21
22 namespace System.Data.EntityModel.Emitters
23 {
24     /// <summary>
25     /// 
26     /// </summary>
27     internal abstract class SchemaTypeEmitter : MetadataItemEmitter
28     {
29         #region Public Methods
30         public abstract CodeTypeDeclarationCollection EmitApiClass();
31         #endregion
32
33         #region Protected Methods
34         /// <summary>
35         /// 
36         /// </summary>
37         /// <param name="generator"></param>
38         /// <param name="schemaType"></param>
39         protected SchemaTypeEmitter(ClientApiGenerator generator, MetadataItem item)
40         : base(generator, item)
41         {
42         }
43
44         /// <summary>
45         /// 
46         /// </summary>
47         /// <param name="typeDecl"></param>
48         protected virtual void EmitTypeAttributes( CodeTypeDeclaration typeDecl )
49         {
50             Generator.AttributeEmitter.EmitTypeAttributes( this, typeDecl );
51         }
52
53         /// <summary>
54         /// Emitter-specific validation: for SchemaTypeEmitter-derived classes, we
55         /// check the EdmItemCollection for other entities that have the same name
56         /// but differ in case
57         /// </summary>
58         protected override void Validate()
59         {
60             Generator.VerifyLanguageCaseSensitiveCompatibilityForType(Item);
61         }
62
63         /// <summary>
64         /// Add attributes to a type's CustomAttributes collection
65         /// </summary>
66         /// <param name="itemName">The name of the type</param>
67         /// <param name="typeDecl">The type to annotate</param>
68         /// <param name="additionalAttributes">The additional attributes</param>
69         protected void EmitTypeAttributes(string itemName, CodeTypeDeclaration typeDecl,
70                                           List<CodeAttributeDeclaration> additionalAttributes)
71         {
72             if (additionalAttributes != null && additionalAttributes.Count > 0)
73             {
74                 try
75                 {
76                     typeDecl.CustomAttributes.AddRange(additionalAttributes.ToArray());
77                 }
78                 catch (ArgumentNullException e)
79                 {
80                     Generator.AddError(Strings.InvalidAttributeSuppliedForType(itemName),
81                                        ModelBuilderErrorCode.InvalidAttributeSuppliedForType,
82                                        EdmSchemaErrorSeverity.Error,
83                                        e);
84                 }
85             }
86
87             EmitTypeAttributes(typeDecl);
88         }
89
90         /// <summary>
91         /// Add interfaces to the type's list of BaseTypes
92         /// </summary>
93         /// <param name="itemName">The name of the type</param>
94         /// <param name="typeDecl">The type whose list of base types needs to be extended</param>
95         /// <param name="additionalInterfaces">The interfaces to add to the list of base types</param>
96         protected void AddInterfaces(string itemName, 
97                                      CodeTypeDeclaration typeDecl, 
98                                      List<Type> additionalInterfaces)
99         {
100             if (additionalInterfaces != null)
101             {
102                 try
103                 {
104                     foreach (Type interfaceType in additionalInterfaces)
105                     {
106                         typeDecl.BaseTypes.Add(new CodeTypeReference(interfaceType));
107                     }
108                 }
109                 catch (ArgumentNullException e)
110                 {
111                     Generator.AddError(Strings.InvalidInterfaceSuppliedForType(itemName),
112                                       ModelBuilderErrorCode.InvalidInterfaceSuppliedForType,
113                                       EdmSchemaErrorSeverity.Error,
114                                       e);
115                 }
116             }
117         }
118
119         /// <summary>
120         /// Add interfaces to the type's list of BaseTypes
121         /// </summary>
122         /// <param name="itemName">The name of the type</param>
123         /// <param name="typeDecl">The type to which members need to be added</param>
124         /// <param name="additionalMembers">The members to add</param>
125         protected void AddMembers(string itemName, CodeTypeDeclaration typeDecl, 
126                                   List<CodeTypeMember> additionalMembers)
127         {
128             if (additionalMembers != null && additionalMembers.Count > 0)
129             {
130                 try
131                 {
132                     typeDecl.Members.AddRange(additionalMembers.ToArray());
133                 }
134                 catch (ArgumentNullException e)
135                 {
136                     Generator.AddError(Strings.InvalidMemberSuppliedForType(itemName),
137                                        ModelBuilderErrorCode.InvalidMemberSuppliedForType,
138                                        EdmSchemaErrorSeverity.Error,
139                                        e);
140                 }
141             }
142         }
143
144         #endregion
145
146         #region Protected Properties
147
148         /// <summary>
149         /// Gets the element that code is being emitted for.
150         /// </summary>
151         internal new GlobalItem Item
152         {
153             get
154             {
155                 return base.Item as GlobalItem;
156             }
157         }
158
159         internal void SetTypeVisibility(CodeTypeDeclaration typeDecl)
160         {
161             typeDecl.TypeAttributes &= ~System.Reflection.TypeAttributes.VisibilityMask;
162             typeDecl.TypeAttributes |= GetTypeAccessibilityValue(Item);
163         }
164
165
166         #endregion
167     }
168 }