@@ -15,8 +15,9 @@ public static class PublicTypeExtensions
1515 /// Returns a friendly, readable version of the name of the given <paramref name="type"/>.
1616 /// </summary>
1717 /// <param name="type">The type for which to retrieve a friendly, readable name.</param>
18+ /// <param name="translationSettings"></param>
1819 /// <returns>A friendly, readable version of the name of the given <paramref name="type"/>.</returns>
19- public static string GetFriendlyName ( this Type type )
20+ public static string GetFriendlyName ( this Type type , TranslationSettings translationSettings )
2021 {
2122 if ( type . FullName == null )
2223 {
@@ -26,7 +27,7 @@ public static string GetFriendlyName(this Type type)
2627
2728 if ( type . IsArray )
2829 {
29- return type . GetElementType ( ) . GetFriendlyName ( ) + "[]" ;
30+ return type . GetElementType ( ) . GetFriendlyName ( translationSettings ) + "[]" ;
3031 }
3132
3233 if ( ! type . IsGenericType ( ) )
@@ -35,7 +36,7 @@ public static string GetFriendlyName(this Type type)
3536
3637 if ( type . IsNested )
3738 {
38- return type . DeclaringType . GetFriendlyName ( ) + "." + qualifiedTypeName ;
39+ return type . DeclaringType . GetFriendlyName ( translationSettings ) + "." + qualifiedTypeName ;
3940 }
4041
4142 return qualifiedTypeName ;
@@ -45,16 +46,16 @@ public static string GetFriendlyName(this Type type)
4546
4647 if ( ( underlyingNullableType = Nullable . GetUnderlyingType ( type ) ) != null )
4748 {
48- return underlyingNullableType . GetFriendlyName ( ) + "?" ;
49+ return underlyingNullableType . GetFriendlyName ( translationSettings ) + "?" ;
4950 }
5051
51- return GetGenericTypeName ( type ) ;
52+ return GetGenericTypeName ( type , translationSettings ) ;
5253 }
5354
54- private static string GetGenericTypeName ( Type genericType )
55+ private static string GetGenericTypeName ( Type genericType , TranslationSettings translationSettings )
5556 {
5657 var typeGenericTypeArguments = genericType . GetGenericTypeArguments ( ) ;
57- var genericTypeName = GetGenericTypeName ( genericType . Name , typeGenericTypeArguments . Length , typeGenericTypeArguments ) ;
58+ var genericTypeName = GetGenericTypeName ( genericType . Name , typeGenericTypeArguments . Length , typeGenericTypeArguments , translationSettings ) ;
5859
5960 if ( ! genericType . IsNested )
6061 {
@@ -102,7 +103,7 @@ private static string GetGenericTypeName(Type genericType)
102103 typeGenericTypeArguments = typeGenericTypeArgumentsSubset ;
103104 }
104105
105- parentTypeName = GetGenericTypeName ( parentTypeName , numberOfParameters , typeArguments ) ;
106+ parentTypeName = GetGenericTypeName ( parentTypeName , numberOfParameters , typeArguments , translationSettings ) ;
106107 }
107108
108109 genericTypeName = parentTypeName + "." + genericTypeName ;
@@ -111,22 +112,21 @@ private static string GetGenericTypeName(Type genericType)
111112 return genericTypeName ;
112113 }
113114
114- private static string GetGenericTypeName (
115- string typeName ,
115+ private static string GetGenericTypeName ( string typeName ,
116116 int numberOfParameters ,
117- IEnumerable < Type > typeArguments )
117+ IEnumerable < Type > typeArguments , TranslationSettings translationSettings )
118118 {
119119 var typeGenericTypeArgumentFriendlyNames =
120- typeArguments . Project ( GetFriendlyName ) . Join ( ", " ) ;
120+ typeArguments . Project ( type => GetFriendlyName ( type , translationSettings ) ) . Join ( ", " ) ;
121121
122122 typeName = typeName . Replace (
123123 "`" + numberOfParameters ,
124124 "<" + typeGenericTypeArgumentFriendlyNames + ">" ) ;
125125
126- return typeName . StartsWith ( '<' ) ? GetAnonymousTypeName ( typeName ) : typeName ;
126+ return typeName . StartsWith ( '<' ) ? GetAnonymousTypeName ( typeName , translationSettings ) : typeName ;
127127 }
128128
129- private static string GetAnonymousTypeName ( string typeName )
129+ private static string GetAnonymousTypeName ( string typeName , TranslationSettings translationSettings )
130130 {
131131 var anonTypeIndex = typeName . IndexOf ( "AnonymousType" , StringComparison . Ordinal ) ;
132132
@@ -135,6 +135,11 @@ private static string GetAnonymousTypeName(string typeName)
135135 return typeName ;
136136 }
137137
138+ if ( translationSettings . AnonymousTypesAsObject )
139+ {
140+ return "object" ;
141+ }
142+
138143 typeName = typeName . Substring ( anonTypeIndex ) ;
139144
140145 var trimStartIndex = "AnonymousType" . Length ;
@@ -149,27 +154,29 @@ private static string GetAnonymousTypeName(string typeName)
149154 /// Retrieves a camel-case variable name for a variable of this <paramref name="type"/>.
150155 /// </summary>
151156 /// <param name="type">The Type for which to retrieve the variable name.</param>
157+ /// <param name="translationSettings"></param>
152158 /// <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 ( ) ;
159+ public static string GetVariableNameInCamelCase ( this Type type , TranslationSettings translationSettings ) => GetVariableName ( type , translationSettings ) . ToCamelCase ( ) ;
154160
155161 /// <summary>
156162 /// Retrieves a pascal-case variable name for a variable of this <paramref name="type"/>.
157163 /// </summary>
158164 /// <param name="type">The Type for which to retrieve the variable name.</param>
165+ /// <param name="translationSettings"></param>
159166 /// <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 ( ) ;
167+ public static string GetVariableNameInPascalCase ( this Type type , TranslationSettings translationSettings ) => GetVariableName ( type , translationSettings ) . ToPascalCase ( ) ;
161168
162- private static string GetVariableName ( Type type )
169+ private static string GetVariableName ( Type type , TranslationSettings translationSettings )
163170 {
164171 if ( type . IsArray )
165172 {
166- return GetVariableName ( type . GetElementType ( ) ) + "Array" ;
173+ return GetVariableName ( type . GetElementType ( ) , translationSettings ) + "Array" ;
167174 }
168175
169176 var typeIsEnumerable = type . IsEnumerable ( ) ;
170177 var typeIsDictionary = typeIsEnumerable && type . IsDictionary ( ) ;
171178 var namingType = ( typeIsEnumerable && ! typeIsDictionary ) ? type . GetEnumerableElementType ( ) : type ;
172- var variableName = GetBaseVariableName ( namingType ) ;
179+ var variableName = GetBaseVariableName ( namingType , translationSettings ) ;
173180
174181 if ( namingType . IsInterface ( ) )
175182 {
@@ -178,31 +185,32 @@ private static string GetVariableName(Type type)
178185
179186 if ( namingType . IsGenericType ( ) )
180187 {
181- variableName = GetGenericTypeVariableName ( variableName , namingType ) ;
188+ variableName = GetGenericTypeVariableName ( variableName , namingType , translationSettings ) ;
182189 }
183190
184191 variableName = RemoveLeadingNonAlphaNumerics ( variableName ) ;
185192
186193 return ( typeIsDictionary || ! typeIsEnumerable ) ? variableName : variableName . Pluralise ( ) ;
187194 }
188195
189- private static string GetBaseVariableName ( Type namingType )
190- => namingType . IsPrimitive ( ) ? namingType . GetFriendlyName ( ) : namingType . Name ;
196+ private static string GetBaseVariableName ( Type namingType , TranslationSettings translationSettings )
197+ => namingType . IsPrimitive ( ) ? namingType . GetFriendlyName ( translationSettings ) : namingType . Name ;
191198
192- private static string GetGenericTypeVariableName ( string variableName , Type namingType )
199+ private static string GetGenericTypeVariableName ( string variableName , Type namingType ,
200+ TranslationSettings translationSettings )
193201 {
194202 var nonNullableType = namingType . GetNonNullableType ( ) ;
195203 var genericTypeArguments = namingType . GetGenericTypeArguments ( ) ;
196204
197205 if ( nonNullableType != namingType )
198206 {
199- return "nullable" + genericTypeArguments [ 0 ] . GetVariableNameInPascalCase ( ) ;
207+ return "nullable" + genericTypeArguments [ 0 ] . GetVariableNameInPascalCase ( translationSettings ) ;
200208 }
201209
202210 variableName = variableName . Substring ( 0 , variableName . IndexOf ( '`' ) ) ;
203211
204212 variableName += genericTypeArguments
205- . Project ( arg => "_" + arg . GetVariableNameInPascalCase ( ) )
213+ . Project ( arg => "_" + arg . GetVariableNameInPascalCase ( translationSettings ) )
206214 . Join ( string . Empty ) ;
207215
208216 return variableName ;
0 commit comments