55 using System . Collections . Generic ;
66 using System . Diagnostics ;
77 using NetStandardPolyfills ;
8+ using static TranslationContext ;
89
910 /// <summary>
1011 /// Provides a set of static extension methods for type information.
@@ -15,8 +16,12 @@ public static class PublicTypeExtensions
1516 /// Returns a friendly, readable version of the name of the given <paramref name="type"/>.
1617 /// </summary>
1718 /// <param name="type">The type for which to retrieve a friendly, readable name.</param>
19+ /// <param name="configuration">The configuration to use for the variable naming, if required.</param>
1820 /// <returns>A friendly, readable version of the name of the given <paramref name="type"/>.</returns>
19- public static string GetFriendlyName ( this Type type )
21+ public static string GetFriendlyName ( this Type type , Func < TranslationSettings , TranslationSettings > configuration = null )
22+ => GetFriendlyName ( type , GetTranslationSettings ( configuration ) ) ;
23+
24+ internal static string GetFriendlyName ( this Type type , TranslationSettings translationSettings )
2025 {
2126 if ( type . FullName == null )
2227 {
@@ -26,7 +31,7 @@ public static string GetFriendlyName(this Type type)
2631
2732 if ( type . IsArray )
2833 {
29- return type . GetElementType ( ) . GetFriendlyName ( ) + "[]" ;
34+ return type . GetElementType ( ) . GetFriendlyName ( translationSettings ) + "[]" ;
3035 }
3136
3237 if ( ! type . IsGenericType ( ) )
@@ -35,7 +40,7 @@ public static string GetFriendlyName(this Type type)
3540
3641 if ( type . IsNested )
3742 {
38- return type . DeclaringType . GetFriendlyName ( ) + "." + qualifiedTypeName ;
43+ return type . DeclaringType . GetFriendlyName ( translationSettings ) + "." + qualifiedTypeName ;
3944 }
4045
4146 return qualifiedTypeName ;
@@ -45,16 +50,16 @@ public static string GetFriendlyName(this Type type)
4550
4651 if ( ( underlyingNullableType = Nullable . GetUnderlyingType ( type ) ) != null )
4752 {
48- return underlyingNullableType . GetFriendlyName ( ) + "?" ;
53+ return underlyingNullableType . GetFriendlyName ( translationSettings ) + "?" ;
4954 }
5055
51- return GetGenericTypeName ( type ) ;
56+ return GetGenericTypeName ( type , translationSettings ) ;
5257 }
5358
54- private static string GetGenericTypeName ( Type genericType )
59+ private static string GetGenericTypeName ( Type genericType , TranslationSettings settings )
5560 {
5661 var typeGenericTypeArguments = genericType . GetGenericTypeArguments ( ) ;
57- var genericTypeName = GetGenericTypeName ( genericType . Name , typeGenericTypeArguments . Length , typeGenericTypeArguments ) ;
62+ var genericTypeName = GetGenericTypeName ( genericType , typeGenericTypeArguments . Length , typeGenericTypeArguments , settings ) ;
5863
5964 if ( ! genericType . IsNested )
6065 {
@@ -102,7 +107,7 @@ private static string GetGenericTypeName(Type genericType)
102107 typeGenericTypeArguments = typeGenericTypeArgumentsSubset ;
103108 }
104109
105- parentTypeName = GetGenericTypeName ( parentTypeName , numberOfParameters , typeArguments ) ;
110+ parentTypeName = GetGenericTypeName ( genericType , numberOfParameters , typeArguments , settings ) ;
106111 }
107112
108113 genericTypeName = parentTypeName + "." + genericTypeName ;
@@ -112,29 +117,36 @@ private static string GetGenericTypeName(Type genericType)
112117 }
113118
114119 private static string GetGenericTypeName (
115- string typeName ,
120+ Type type ,
116121 int numberOfParameters ,
117- IEnumerable < Type > typeArguments )
122+ IEnumerable < Type > typeArguments ,
123+ TranslationSettings settings )
118124 {
125+ var anonTypeIndex = 0 ;
126+
127+ var isAnonType =
128+ type . Name . StartsWith ( '<' ) &&
129+ ( ( anonTypeIndex = type . Name . IndexOf ( "AnonymousType" , StringComparison . Ordinal ) ) != - 1 ) ;
130+
131+ if ( isAnonType && ( settings . AnonymousTypeNameFactory != null ) )
132+ {
133+ return settings . AnonymousTypeNameFactory . Invoke ( type ) ;
134+ }
135+
136+ var typeName = type . Name ;
137+
119138 var typeGenericTypeArgumentFriendlyNames =
120- typeArguments . Project ( GetFriendlyName ) . Join ( ", " ) ;
139+ typeArguments . Project ( t => GetFriendlyName ( t , settings ) ) . Join ( ", " ) ;
121140
122141 typeName = typeName . Replace (
123142 "`" + numberOfParameters ,
124143 "<" + typeGenericTypeArgumentFriendlyNames + ">" ) ;
125144
126- return typeName . StartsWith ( '<' ) ? GetAnonymousTypeName ( typeName ) : typeName ;
145+ return isAnonType ? GetAnonymousTypeName ( typeName , anonTypeIndex ) : typeName ;
127146 }
128147
129- private static string GetAnonymousTypeName ( string typeName )
148+ private static string GetAnonymousTypeName ( string typeName , int anonTypeIndex )
130149 {
131- var anonTypeIndex = typeName . IndexOf ( "AnonymousType" , StringComparison . Ordinal ) ;
132-
133- if ( anonTypeIndex == - 1 )
134- {
135- return typeName ;
136- }
137-
138150 typeName = typeName . Substring ( anonTypeIndex ) ;
139151
140152 var trimStartIndex = "AnonymousType" . Length ;
@@ -149,27 +161,37 @@ private static string GetAnonymousTypeName(string typeName)
149161 /// Retrieves a camel-case variable name for a variable of this <paramref name="type"/>.
150162 /// </summary>
151163 /// <param name="type">The Type for which to retrieve the variable name.</param>
164+ /// <param name="configuration">The configuration to use for the variable naming, if required.</param>
152165 /// <returns>A camel-case variable name for a variable of this <paramref name="type"/>.</returns>
153- public static string GetVariableNameInCamelCase ( this Type type ) => GetVariableName ( type ) . ToCamelCase ( ) ;
166+ public static string GetVariableNameInCamelCase ( this Type type , Func < TranslationSettings , TranslationSettings > configuration = null )
167+ => GetVariableNameInCamelCase ( type , GetTranslationSettings ( configuration ) ) ;
168+
169+ internal static string GetVariableNameInCamelCase ( this Type type , TranslationSettings settings )
170+ => GetVariableName ( type , settings ) . ToCamelCase ( ) ;
154171
155172 /// <summary>
156173 /// Retrieves a pascal-case variable name for a variable of this <paramref name="type"/>.
157174 /// </summary>
158175 /// <param name="type">The Type for which to retrieve the variable name.</param>
176+ /// <param name="configuration">The configuration to use for the variable naming, if required.</param>
159177 /// <returns>A pascal-case variable name for a variable of this <paramref name="type"/>.</returns>
160- public static string GetVariableNameInPascalCase ( this Type type ) => GetVariableName ( type ) . ToPascalCase ( ) ;
178+ public static string GetVariableNameInPascalCase ( this Type type , Func < TranslationSettings , TranslationSettings > configuration = null )
179+ => GetVariableNameInPascalCase ( type , GetTranslationSettings ( configuration ) ) ;
180+
181+ internal static string GetVariableNameInPascalCase ( this Type type , TranslationSettings settings )
182+ => GetVariableName ( type , settings ) . ToPascalCase ( ) ;
161183
162- private static string GetVariableName ( Type type )
184+ private static string GetVariableName ( Type type , TranslationSettings settings )
163185 {
164186 if ( type . IsArray )
165187 {
166- return GetVariableName ( type . GetElementType ( ) ) + "Array" ;
188+ return GetVariableName ( type . GetElementType ( ) , settings ) + "Array" ;
167189 }
168190
169191 var typeIsEnumerable = type . IsEnumerable ( ) ;
170192 var typeIsDictionary = typeIsEnumerable && type . IsDictionary ( ) ;
171193 var namingType = ( typeIsEnumerable && ! typeIsDictionary ) ? type . GetEnumerableElementType ( ) : type ;
172- var variableName = GetBaseVariableName ( namingType ) ;
194+ var variableName = GetBaseVariableName ( namingType , settings ) ;
173195
174196 if ( namingType . IsInterface ( ) )
175197 {
@@ -178,31 +200,34 @@ private static string GetVariableName(Type type)
178200
179201 if ( namingType . IsGenericType ( ) )
180202 {
181- variableName = GetGenericTypeVariableName ( variableName , namingType ) ;
203+ variableName = GetGenericTypeVariableName ( variableName , namingType , settings ) ;
182204 }
183205
184206 variableName = RemoveLeadingNonAlphaNumerics ( variableName ) ;
185207
186208 return ( typeIsDictionary || ! typeIsEnumerable ) ? variableName : variableName . Pluralise ( ) ;
187209 }
188210
189- private static string GetBaseVariableName ( Type namingType )
190- => namingType . IsPrimitive ( ) ? namingType . GetFriendlyName ( ) : namingType . Name ;
211+ private static string GetBaseVariableName ( Type namingType , TranslationSettings translationSettings )
212+ => namingType . IsPrimitive ( ) ? namingType . GetFriendlyName ( translationSettings ) : namingType . Name ;
191213
192- private static string GetGenericTypeVariableName ( string variableName , Type namingType )
214+ private static string GetGenericTypeVariableName (
215+ string variableName ,
216+ Type namingType ,
217+ TranslationSettings settings )
193218 {
194219 var nonNullableType = namingType . GetNonNullableType ( ) ;
195220 var genericTypeArguments = namingType . GetGenericTypeArguments ( ) ;
196221
197222 if ( nonNullableType != namingType )
198223 {
199- return "nullable" + genericTypeArguments [ 0 ] . GetVariableNameInPascalCase ( ) ;
224+ return "nullable" + genericTypeArguments [ 0 ] . GetVariableNameInPascalCase ( settings ) ;
200225 }
201226
202227 variableName = variableName . Substring ( 0 , variableName . IndexOf ( '`' ) ) ;
203228
204229 variableName += genericTypeArguments
205- . Project ( arg => "_" + arg . GetVariableNameInPascalCase ( ) )
230+ . Project ( arg => "_" + arg . GetVariableNameInPascalCase ( settings ) )
206231 . Join ( string . Empty ) ;
207232
208233 return variableName ;
0 commit comments