コード例 #1
0
        /// <summary>
        /// The name from property.
        /// </summary>
        /// <param name="root">
        /// The root.
        /// </param>
        /// <param name="rootObjectName">
        /// The root object name.
        /// </param>
        public static void NameFromProperty(TypeDescription root, string rootObjectName)
        {
            if (!string.IsNullOrWhiteSpace(rootObjectName))
            {
                root.AssignedName = SafeIdentifier(rootObjectName);
            }

            var allTreeNodes = root.AllTypeDescriptions().ToList();

            var allChids =
                allTreeNodes.Where(p => p.Parent != null)
                .Select(
                    p =>
                    new
            {
                SafeParentName = SafeIdentifier(p.ParentName, true),
                ParentIsArray  = p.Parent != null && p.Parent.IsArray,
                Type           = p,
            })
                .GroupBy(d => d.SafeParentName)
                .ToList();

            foreach (var namedCollection in allChids)
            {
                var items = namedCollection.ToList();
                if (items.Count == 1)
                {
                    namedCollection.Single().Type.AssignedName = namedCollection.Key;
                }
                else
                {
                    var index = 0;
                    foreach (var item in items)
                    {
                        item.Type.AssignedName = $"{namedCollection.Key}{index++ :000}";
                    }
                }
            }
        }