Updates referencesource to .NET 4.7
[mono.git] / mcs / class / referencesource / System.Data.Entity / System / Data / Metadata / Edm / ComplexType.cs
1 //---------------------------------------------------------------------
2 // <copyright file="ComplexType.cs" company="Microsoft">
3 //      Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>
5 //
6 // @owner       Microsoft
7 // @backupOwner Microsoft
8 //---------------------------------------------------------------------
9 using System.Data.Common;
10 using System.Threading;
11 using System.Diagnostics;
12
13 namespace System.Data.Metadata.Edm
14 {
15     /// <summary>
16     /// Represent the Edm Complex Type
17     /// </summary>
18     public class ComplexType : StructuralType
19     {
20         #region Constructors
21         /// <summary>
22         /// Initializes a new instance of Complex Type with the given properties
23         /// </summary>
24         /// <param name="name">The name of the complex type</param>
25         /// <param name="namespaceName">The namespace name of the type</param>
26         /// <param name="version">The version of this type</param>
27         /// <param name="dataSpace">dataSpace in which this ComplexType belongs to</param>
28         /// <exception cref="System.ArgumentNullException">If either name, namespace or version arguments are null</exception>
29         internal ComplexType(string name, string namespaceName, DataSpace dataSpace)
30             : base(name, namespaceName, dataSpace)
31         {
32         }
33
34         /// <summary>
35         /// Initializes a new instance of Complex Type - required for bootstraping code
36         /// </summary>
37         internal ComplexType()
38         {
39             // No initialization of item attributes in here, it's used as a pass thru in the case for delay population
40             // of item attributes
41         }
42
43
44         #endregion
45
46         #region Fields
47         private ReadOnlyMetadataCollection<EdmProperty> _properties;
48         #endregion
49
50         #region Properties
51         /// <summary>
52         /// Returns the kind of the type
53         /// </summary>
54         public override BuiltInTypeKind BuiltInTypeKind { get { return BuiltInTypeKind.ComplexType; } }
55
56
57
58         /// <summary>
59         /// Returns just the properties from the collection
60         /// of members on this type
61         /// </summary>
62         public ReadOnlyMetadataCollection<EdmProperty> Properties
63         {
64             get
65             {
66                 Debug.Assert(IsReadOnly, "this is a wrapper around this.Members, don't call it during metadata loading, only call it after the metadata is set to readonly");
67                 if (null == _properties)
68                 {
69                     Interlocked.CompareExchange(ref _properties,
70                         new FilteredReadOnlyMetadataCollection<EdmProperty, EdmMember>(
71                             this.Members, Helper.IsEdmProperty), null);
72                 }
73                 return _properties;
74             }
75         }
76         #endregion
77
78         #region Methods
79
80         /// <summary>
81         /// Validates a EdmMember object to determine if it can be added to this type's 
82         /// Members collection. If this method returns without throwing, it is assumed
83         /// the member is valid. 
84         /// </summary>
85         /// <param name="member">The member to validate</param>
86         /// <exception cref="System.ArgumentException">Thrown if the member is not a EdmProperty</exception>
87         internal override void ValidateMemberForAdd(EdmMember member)
88         {
89             Debug.Assert(Helper.IsEdmProperty(member) || Helper.IsNavigationProperty(member),
90                 "Only members of type Property may be added to ComplexType.");
91         }
92         #endregion
93     }
94
95     internal sealed class ClrComplexType : ComplexType
96     {
97         /// <summary>cached CLR type handle, allowing the Type reference to be GC'd</summary>
98         private readonly System.RuntimeTypeHandle _type;
99
100         /// <summary>cached dynamic method to construct a CLR instance</summary>
101         private Delegate _constructor;
102
103         private readonly string _cspaceTypeName;
104
105         /// <summary>
106         /// Initializes a new instance of Complex Type with properties from the type.
107         /// </summary>
108         /// <param name="clrType">The CLR type to construct from</param>
109         internal ClrComplexType(Type clrType, string cspaceNamespaceName, string cspaceTypeName)
110             : base(EntityUtil.GenericCheckArgumentNull(clrType, "clrType").Name, clrType.Namespace ?? string.Empty, 
111                    DataSpace.OSpace)
112         {
113             System.Diagnostics.Debug.Assert(!String.IsNullOrEmpty(cspaceNamespaceName) &&
114                 !String.IsNullOrEmpty(cspaceTypeName), "Mapping information must never be null");
115
116             _type = clrType.TypeHandle;
117             _cspaceTypeName = cspaceNamespaceName + "." + cspaceTypeName;
118             this.Abstract = clrType.IsAbstract;
119         }
120         internal static ClrComplexType CreateReadonlyClrComplexType(Type clrType, string cspaceNamespaceName, string cspaceTypeName)
121         {
122             ClrComplexType type = new ClrComplexType(clrType, cspaceNamespaceName, cspaceTypeName);
123             type.SetReadOnly();
124             
125             return type;
126         }
127
128         /// <summary>cached dynamic method to construct a CLR instance</summary>
129         internal Delegate Constructor
130         {
131             get { return _constructor; }
132             set
133             {
134                 // It doesn't matter which delegate wins, but only one should be jitted
135                 Interlocked.CompareExchange(ref _constructor, value, null);
136             }
137         }
138
139         /// <summary>
140         /// </summary>
141         internal override System.Type ClrType
142         {
143             get { return Type.GetTypeFromHandle(_type); }
144         }
145
146         internal string CSpaceTypeName { get { return _cspaceTypeName; } }
147     }
148 }