Beispiel #1
0
 /// <summary>
 /// Creates a class name <see cref="String"/> to use when generating
 /// source code.
 /// </summary>
 /// <param name="t">
 /// The <see cref="Type"/> object containing the class name to evaluate.
 /// </param>
 /// <param name="order">
 /// The <see cref="NamespaceAliasOrder"/> value to evaluate when building the
 /// appropriate class name.
 /// </param>
 /// <returns>
 /// The class name to use when building a new <see cref="CodeObjectCreateExpression"/>
 /// object.
 /// </returns>
 public static string GetObjectTypeName(this Type t, NamespaceAliasOrder order)
 {
     if (NamespaceAliases.ContainsKey(t.Namespace))
     {
         return(order == NamespaceAliasOrder.None ? t.FullName :
                $"{NamespaceAliases[t.Namespace]}.{t.Name}");
     }
     return(t.Name);
 }
Beispiel #2
0
        /// <summary>
        /// Generates a variable name to use when generating the appropriate
        /// CodeDom objects for a given <see cref="Type"/>.
        /// </summary>
        /// <param name="t">
        /// The <see cref="Type"/> to generate the variable name for.
        /// </param>
        /// <param name="typeCount">
        /// The <see cref="IDictionary{TKey, TValue}"/> object that tracks
        /// the number of times a given type has been generated.
        /// </param>
        /// <returns>
        /// A new variable name to use to represent <paramref name="t"/>.
        /// </returns>
        public static string GenerateVariableName(this Type t, IDictionary <Type, int> typeCount)
        {
            string tmp;  // Hold the generated name
            string nsPrefix = String.Empty;

            // Include the namespace alias as part of the variable name
            if (NamespaceAliases.ContainsKey(t.Namespace))
            {
                nsPrefix = NamespaceAliases[t.Namespace].ToLowerInvariant();
            }

            // Simply return the generated name if the current
            // type is not considered generic.
            if (!t.IsGenericType)
            {
                tmp = String.Concat(nsPrefix, t.Name).ToCamelCase();
                if (typeCount != null && typeCount.ContainsKey(t))
                {
                    return(String.Concat(tmp, ++typeCount[t]));
                }
                typeCount.Add(t, 0);
                return(tmp);
            }

            // Include the generic types as part of the var name.
            var sb = new StringBuilder();

            foreach (var item in t.GenericTypeArguments)
            {
                sb.Append(item.Name.RetrieveUpperCaseChars().ToTitleCase());
            }
            tmp = t.Name;

            if (typeCount != null && typeCount.ContainsKey(t))
            {
                return(String.Concat(nsPrefix,
                                     tmp.Substring(0, tmp.IndexOf("`")),
                                     sb.ToString(),
                                     ++typeCount[t]).ToCamelCase());
            }

            typeCount.Add(t, 0);
            return(String.Concat(nsPrefix,
                                 tmp.Substring(0, tmp.IndexOf("`")),
                                 sb.ToString()).ToCamelCase());
        }