/// <summary>
        /// Verifies that the <see cref="MetadataTypeAttribute"/> reference does not contain a cyclic reference and
        /// registers the AssociatedMetadataTypeTypeDescriptionProvider in that case.
        /// </summary>
        /// <param name="type">The entity type with the MetadataType attribute.</param>
        private static void RegisterAssociatedMetadataTypeTypeDescriptor(Type type)
        {
            Type           currentType            = type;
            HashSet <Type> metadataTypeReferences = new HashSet <Type>();

            metadataTypeReferences.Add(currentType);
            while (true)
            {
                MetadataTypeAttribute attribute = (MetadataTypeAttribute)Attribute.GetCustomAttribute(currentType, typeof(MetadataTypeAttribute));
                if (attribute == null)
                {
                    break;
                }
                else
                {
                    currentType = attribute.MetadataClassType;
                    // If we find a cyclic reference, throw an error.
                    if (metadataTypeReferences.Contains(currentType))
                    {
                        throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, Resource.CyclicMetadataTypeAttributesFound, type.FullName));
                    }
                    else
                    {
                        metadataTypeReferences.Add(currentType);
                    }
                }
            }

            // If the MetadataType reference chain doesn't contain a cycle, register the use of the AssociatedMetadataTypeTypeDescriptionProvider.
            DomainServiceDescription.RegisterCustomTypeDescriptor(new AssociatedMetadataTypeTypeDescriptionProvider(type), type);
        }