@@ -276,36 +276,69 @@ public static string GetCustomAttributeStrings(HasCustomAttributes context, int
276276 /// Returns the name of the given type, as it would appear in a C# source file.
277277 /// This mainly involves stripping the backtick section from generic type names, and replacing certain system types with their primitive name.
278278 /// </summary>
279- /// <param name="originalName">The original name of the type </param>
280- public static string GetTypeName ( string originalName )
279+ /// <param name="type"> </param>
280+ public static string GetTypeName ( TypeAnalysisContext type )
281281 {
282- if ( originalName . Contains ( "`" ) )
283- //Generics - remove `1 etc
284- return originalName . Remove ( originalName . IndexOf ( '`' ) , 2 ) ;
282+ if ( type is WrappedTypeAnalysisContext wrapped )
283+ {
284+ var elementTypeName = GetTypeName ( wrapped . ElementType ) ;
285+ switch ( wrapped )
286+ {
287+ case ArrayTypeAnalysisContext arrayType :
288+ {
289+ return arrayType . Rank switch
290+ {
291+ 1 => elementTypeName + "[]" ,
292+ 2 => elementTypeName + "[,]" ,
293+ 3 => elementTypeName + "[,,]" ,
294+ _ => elementTypeName + "[" + new string ( ',' , arrayType . Rank - 1 ) + "]"
295+ } ;
296+ }
297+ case SzArrayTypeAnalysisContext :
298+ return elementTypeName + "[]" ;
299+ case PointerTypeAnalysisContext :
300+ return elementTypeName + "*" ;
301+ case ByRefTypeAnalysisContext :
302+ return elementTypeName ; //Remove trailing & for ref params
303+ default :
304+ return elementTypeName ;
305+ }
306+ }
285307
286- if ( originalName [ ^ 1 ] == '&' )
287- originalName = originalName [ ..^ 1 ] ; //Remove trailing & for ref params
308+ if ( type is GenericInstanceTypeAnalysisContext genericInstanceType )
309+ {
310+ var genericTypeName = GetTypeName ( genericInstanceType . GenericType ) ;
311+ var backTickIndex = genericTypeName . IndexOf ( '`' ) ;
312+ return backTickIndex > 0 ? genericTypeName [ ..backTickIndex ] : genericTypeName ;
313+ }
288314
289- return originalName switch
315+ if ( type . Namespace is "System" )
316+ {
317+ return type . Name switch
318+ {
319+ "Void" => "void" ,
320+ "Boolean" => "bool" ,
321+ "Byte" => "byte" ,
322+ "SByte" => "sbyte" ,
323+ "Char" => "char" ,
324+ "Decimal" => "decimal" ,
325+ "Single" => "float" ,
326+ "Double" => "double" ,
327+ "Int32" => "int" ,
328+ "UInt32" => "uint" ,
329+ "Int64" => "long" ,
330+ "UInt64" => "ulong" ,
331+ "Int16" => "short" ,
332+ "UInt16" => "ushort" ,
333+ "String" => "string" ,
334+ "Object" => "object" ,
335+ _ => type . Name ,
336+ } ;
337+ }
338+ else
290339 {
291- "Void" => "void" ,
292- "Boolean" => "bool" ,
293- "Byte" => "byte" ,
294- "SByte" => "sbyte" ,
295- "Char" => "char" ,
296- "Decimal" => "decimal" ,
297- "Single" => "float" ,
298- "Double" => "double" ,
299- "Int32" => "int" ,
300- "UInt32" => "uint" ,
301- "Int64" => "long" ,
302- "UInt64" => "ulong" ,
303- "Int16" => "short" ,
304- "UInt16" => "ushort" ,
305- "String" => "string" ,
306- "Object" => "object" ,
307- _ => originalName
308- } ;
340+ return type . Name ;
341+ }
309342 }
310343
311344 /// <summary>
@@ -319,7 +352,7 @@ public static void AppendInheritanceInfo(TypeAnalysisContext type, StringBuilder
319352 var baseType = type . BaseType ;
320353 var needsBaseClass = baseType is { FullName : not "System.Object" and not "System.ValueType" and not "System.Enum" } ;
321354 if ( needsBaseClass )
322- sb . Append ( " : " ) . Append ( GetTypeName ( baseType ! . Name ) ) ;
355+ sb . Append ( " : " ) . Append ( GetTypeName ( baseType ! ) ) ;
323356
324357 //Interfaces
325358 if ( type . InterfaceContexts . Count <= 0 )
@@ -336,7 +369,7 @@ public static void AppendInheritanceInfo(TypeAnalysisContext type, StringBuilder
336369
337370 addComma = true ;
338371
339- sb . Append ( GetTypeName ( iface . Name ) ) ;
372+ sb . Append ( GetTypeName ( iface ) ) ;
340373 }
341374 }
342375}
0 commit comments