Updates referencesource to .NET 4.7
[mono-project.git] / mcs / class / referencesource / System.Data.Entity / System / Data / Metadata / Edm / ComplexType.cs
blobad0fe21a19dc35a6db88b03623460c5495e08093
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;
13 namespace System.Data.Metadata.Edm
15 /// <summary>
16 /// Represent the Edm Complex Type
17 /// </summary>
18 public class ComplexType : StructuralType
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)
34 /// <summary>
35 /// Initializes a new instance of Complex Type - required for bootstraping code
36 /// </summary>
37 internal ComplexType()
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
44 #endregion
46 #region Fields
47 private ReadOnlyMetadataCollection<EdmProperty> _properties;
48 #endregion
50 #region Properties
51 /// <summary>
52 /// Returns the kind of the type
53 /// </summary>
54 public override BuiltInTypeKind BuiltInTypeKind { get { return BuiltInTypeKind.ComplexType; } }
58 /// <summary>
59 /// Returns just the properties from the collection
60 /// of members on this type
61 /// </summary>
62 public ReadOnlyMetadataCollection<EdmProperty> Properties
64 get
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)
69 Interlocked.CompareExchange(ref _properties,
70 new FilteredReadOnlyMetadataCollection<EdmProperty, EdmMember>(
71 this.Members, Helper.IsEdmProperty), null);
73 return _properties;
76 #endregion
78 #region Methods
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)
89 Debug.Assert(Helper.IsEdmProperty(member) || Helper.IsNavigationProperty(member),
90 "Only members of type Property may be added to ComplexType.");
92 #endregion
95 internal sealed class ClrComplexType : ComplexType
97 /// <summary>cached CLR type handle, allowing the Type reference to be GC'd</summary>
98 private readonly System.RuntimeTypeHandle _type;
100 /// <summary>cached dynamic method to construct a CLR instance</summary>
101 private Delegate _constructor;
103 private readonly string _cspaceTypeName;
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)
113 System.Diagnostics.Debug.Assert(!String.IsNullOrEmpty(cspaceNamespaceName) &&
114 !String.IsNullOrEmpty(cspaceTypeName), "Mapping information must never be null");
116 _type = clrType.TypeHandle;
117 _cspaceTypeName = cspaceNamespaceName + "." + cspaceTypeName;
118 this.Abstract = clrType.IsAbstract;
120 internal static ClrComplexType CreateReadonlyClrComplexType(Type clrType, string cspaceNamespaceName, string cspaceTypeName)
122 ClrComplexType type = new ClrComplexType(clrType, cspaceNamespaceName, cspaceTypeName);
123 type.SetReadOnly();
125 return type;
128 /// <summary>cached dynamic method to construct a CLR instance</summary>
129 internal Delegate Constructor
131 get { return _constructor; }
134 // It doesn't matter which delegate wins, but only one should be jitted
135 Interlocked.CompareExchange(ref _constructor, value, null);
139 /// <summary>
140 /// </summary>
141 internal override System.Type ClrType
143 get { return Type.GetTypeFromHandle(_type); }
146 internal string CSpaceTypeName { get { return _cspaceTypeName; } }