1 //---------------------------------------------------------------------
2 // <copyright file="ComplexType.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
7 // @backupOwner Microsoft
8 //---------------------------------------------------------------------
9 using System.Data.Common;
10 using System.Threading;
11 using System.Diagnostics;
13 namespace System.Data.Metadata.Edm
16 /// Represent the Edm Complex Type
18 public class ComplexType : StructuralType
22 /// Initializes a new instance of Complex Type with the given properties
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)
35 /// Initializes a new instance of Complex Type - required for bootstraping code
37 internal ComplexType()
39 // No initialization of item attributes in here, it's used as a pass thru in the case for delay population
47 private ReadOnlyMetadataCollection<EdmProperty> _properties;
52 /// Returns the kind of the type
54 public override BuiltInTypeKind BuiltInTypeKind { get { return BuiltInTypeKind.ComplexType; } }
59 /// Returns just the properties from the collection
60 /// of members on this type
62 public ReadOnlyMetadataCollection<EdmProperty> Properties
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);
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.
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.");
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;
106 /// Initializes a new instance of Complex Type with properties from the type.
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,
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);
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);
141 internal override System.Type ClrType
143 get { return Type.GetTypeFromHandle(_type); }
146 internal string CSpaceTypeName { get { return _cspaceTypeName; } }