@@ -36,14 +36,17 @@ internal static string GetFriendlyName(this Type type, TranslationSettings trans
3636
3737 if ( ! type . IsGenericType ( ) )
3838 {
39- var qualifiedTypeName = type . FullName . GetSubstitutionOrNull ( ) ?? type . Name ;
39+ var substitutedTypeName = type . FullName . GetSubstitutionOrNull ( ) ;
40+ var qualifiedTypeName = substitutedTypeName ?? type . Name ;
4041
4142 if ( type . IsNested )
4243 {
4344 return type . DeclaringType . GetFriendlyName ( translationSettings ) + "." + qualifiedTypeName ;
4445 }
4546
46- return qualifiedTypeName ;
47+ return ( substitutedTypeName == null )
48+ ? GetFinalisedTypeName ( type , qualifiedTypeName , translationSettings )
49+ : qualifiedTypeName ;
4750 }
4851
4952 Type underlyingNullableType ;
@@ -59,67 +62,77 @@ internal static string GetFriendlyName(this Type type, TranslationSettings trans
5962 private static string GetGenericTypeName ( Type genericType , TranslationSettings settings )
6063 {
6164 var typeGenericTypeArguments = genericType . GetGenericTypeArguments ( ) ;
62- var genericTypeName = GetGenericTypeName ( genericType , typeGenericTypeArguments . Length , typeGenericTypeArguments , settings ) ;
65+ var genericTypeName = GetClosedGenericTypeName ( genericType , ref typeGenericTypeArguments , settings ) ;
6366
6467 if ( ! genericType . IsNested )
6568 {
66- return genericTypeName ;
69+ return GetFinalisedTypeName ( genericType , genericTypeName , settings ) ;
6770 }
6871
6972 // ReSharper disable once PossibleNullReferenceException
7073 while ( genericType . IsNested )
7174 {
7275 genericType = genericType . DeclaringType ;
73- var parentTypeName = genericType . Name ;
76+ var parentTypeName = GetClosedGenericTypeName ( genericType , ref typeGenericTypeArguments , settings ) ;
7477
75- var backtickIndex = parentTypeName . IndexOf ( "`" , StringComparison . Ordinal ) ;
78+ genericTypeName = parentTypeName + "." + genericTypeName ;
79+ }
7680
77- if ( backtickIndex != - 1 )
78- {
79- var numberOfParameters = int . Parse ( parentTypeName . Substring ( backtickIndex + 1 ) ) ;
80-
81- Type [ ] typeArguments ;
82-
83- if ( numberOfParameters == typeGenericTypeArguments . Length )
84- {
85- typeArguments = typeGenericTypeArguments ;
86- }
87- else
88- {
89- typeArguments = new Type [ numberOfParameters ] ;
90- var numberOfRemainingTypeArguments = typeGenericTypeArguments . Length - numberOfParameters ;
91- var typeGenericTypeArgumentsSubset = new Type [ numberOfRemainingTypeArguments ] ;
92-
93- Array . Copy (
94- typeGenericTypeArguments ,
95- numberOfRemainingTypeArguments ,
96- typeArguments ,
97- 0 ,
98- numberOfParameters ) ;
99-
100- Array . Copy (
101- typeGenericTypeArguments ,
102- 0 ,
103- typeGenericTypeArgumentsSubset ,
104- 0 ,
105- numberOfRemainingTypeArguments ) ;
106-
107- typeGenericTypeArguments = typeGenericTypeArgumentsSubset ;
108- }
109-
110- parentTypeName = GetGenericTypeName ( genericType , numberOfParameters , typeArguments , settings ) ;
111- }
81+ return GetFinalisedTypeName ( genericType , genericTypeName , settings ) ;
82+ }
11283
113- genericTypeName = parentTypeName + "." + genericTypeName ;
84+ private static string GetClosedGenericTypeName (
85+ Type genericType ,
86+ ref Type [ ] typeGenericTypeArguments ,
87+ TranslationSettings settings )
88+ {
89+ var typeName = genericType . Name ;
90+
91+ var backtickIndex = typeName . IndexOf ( "`" , StringComparison . Ordinal ) ;
92+
93+ if ( backtickIndex == - 1 )
94+ {
95+ return typeName ;
11496 }
11597
116- return genericTypeName ;
98+ var numberOfParameters = int . Parse ( typeName . Substring ( backtickIndex + 1 ) ) ;
99+
100+ Type [ ] typeArguments ;
101+
102+ if ( numberOfParameters == typeGenericTypeArguments . Length )
103+ {
104+ typeArguments = typeGenericTypeArguments ;
105+ }
106+ else
107+ {
108+ typeArguments = new Type [ numberOfParameters ] ;
109+ var numberOfRemainingTypeArguments = typeGenericTypeArguments . Length - numberOfParameters ;
110+ var typeGenericTypeArgumentsSubset = new Type [ numberOfRemainingTypeArguments ] ;
111+
112+ Array . Copy (
113+ typeGenericTypeArguments ,
114+ numberOfRemainingTypeArguments ,
115+ typeArguments ,
116+ 0 ,
117+ numberOfParameters ) ;
118+
119+ Array . Copy (
120+ typeGenericTypeArguments ,
121+ 0 ,
122+ typeGenericTypeArgumentsSubset ,
123+ 0 ,
124+ numberOfRemainingTypeArguments ) ;
125+
126+ typeGenericTypeArguments = typeGenericTypeArgumentsSubset ;
127+ }
128+
129+ return GetGenericTypeName ( genericType , numberOfParameters , typeArguments , settings ) ;
117130 }
118131
119132 private static string GetGenericTypeName (
120133 Type type ,
121134 int numberOfParameters ,
122- IEnumerable < Type > typeArguments ,
135+ IList < Type > typeArguments ,
123136 TranslationSettings settings )
124137 {
125138 var anonTypeIndex = 0 ;
@@ -135,12 +148,12 @@ private static string GetGenericTypeName(
135148
136149 var typeName = type . Name ;
137150
138- var typeGenericTypeArgumentFriendlyNames =
139- typeArguments . Project ( t => GetFriendlyName ( t , settings ) ) . Join ( ", " ) ;
151+ var typeGenericTypeArgumentNames = typeArguments
152+ . ProjectToArray ( t => GetFriendlyName ( t , settings ) ) ;
140153
141154 typeName = typeName . Replace (
142155 "`" + numberOfParameters ,
143- "<" + typeGenericTypeArgumentFriendlyNames + ">" ) ;
156+ "<" + typeGenericTypeArgumentNames . Join ( ", " ) + ">" ) ;
144157
145158 return isAnonType ? GetAnonymousTypeName ( typeName , anonTypeIndex ) : typeName ;
146159 }
@@ -157,6 +170,13 @@ private static string GetAnonymousTypeName(string typeName, int anonTypeIndex)
157170 return typeName ;
158171 }
159172
173+ private static string GetFinalisedTypeName ( Type type , string typeName , TranslationSettings settings )
174+ {
175+ return settings . FullyQualifyTypeNames && ( type . Namespace != null )
176+ ? type . Namespace + "." + typeName
177+ : typeName ;
178+ }
179+
160180 /// <summary>
161181 /// Retrieves a camel-case variable name for a variable of this <paramref name="type"/>.
162182 /// </summary>
@@ -297,9 +317,8 @@ public static Type GetDictionaryType(this Type type)
297317 }
298318 }
299319
300- var interfaceType = type
301- . GetAllInterfaces ( )
302- . FirstOrDefault ( t => t . IsClosedTypeOf ( typeof ( IDictionary < , > ) ) ) ;
320+ var interfaceType = InternalEnumerableExtensions . FirstOrDefault ( type
321+ . GetAllInterfaces ( ) , t => t . IsClosedTypeOf ( typeof ( IDictionary < , > ) ) ) ;
303322
304323 return interfaceType ;
305324 }
@@ -326,14 +345,13 @@ public static Type GetEnumerableElementType(this Type enumerableType)
326345
327346 if ( enumerableType . IsGenericType ( ) )
328347 {
329- return enumerableType . GetGenericTypeArguments ( ) . Last ( ) ;
348+ return InternalEnumerableExtensions . Last ( enumerableType . GetGenericTypeArguments ( ) ) ;
330349 }
331350
332- var enumerableInterfaceType = enumerableType
333- . GetAllInterfaces ( )
334- . FirstOrDefault ( interfaceType => interfaceType . IsClosedTypeOf ( typeof ( IEnumerable < > ) ) ) ;
351+ var enumerableInterfaceType = InternalEnumerableExtensions . FirstOrDefault ( enumerableType
352+ . GetAllInterfaces ( ) , interfaceType => interfaceType . IsClosedTypeOf ( typeof ( IEnumerable < > ) ) ) ;
335353
336- return enumerableInterfaceType ? . GetGenericTypeArguments ( ) . First ( ) ?? typeof ( object ) ;
354+ return InternalEnumerableExtensions . First ( enumerableInterfaceType ? . GetGenericTypeArguments ( ) ) ?? typeof ( object ) ;
337355 }
338356
339357 /// <summary>
0 commit comments