11using System ;
22using System . Collections . Generic ;
33using System . Linq ;
4+ using System . Text ;
45
56namespace Stunts
67{
@@ -10,29 +11,83 @@ namespace Stunts
1011 public static class StuntNaming
1112 {
1213 /// <summary>
13- /// The namespace where generated stunts are declared.
14+ /// The default namespace where generated stunts are declared.
1415 /// </summary>
15- public const string Namespace = "Stunts" ;
16+ public const string DefaultNamespace = "Stunts" ;
1617
1718 /// <summary>
18- /// The suffix added to stunt type names.
19+ /// The default suffix added to stunt type names.
1920 /// </summary>
20- public const string NameSuffix = "Stunt" ;
21+ public const string DefaultSuffix = "Stunt" ;
2122
2223 /// <summary>
23- /// Gets the runtime stunt name from its base type and implemented interfaces.
24+ /// Gets the runtime stunt name from its base type and optional additional
25+ /// interfaces, using the <see cref="DefaultSuffix"/>.
2426 /// </summary>
25- public static string GetName ( Type baseType , Type [ ] implementedInterfaces )
26- {
27- Array . Sort ( implementedInterfaces , Comparer < Type > . Create ( ( x , y ) => x . Name . CompareTo ( y . Name ) ) ) ;
27+ public static string GetName ( Type baseType , params Type [ ] additionalInterfaces )
28+ => GetName ( DefaultSuffix , baseType , additionalInterfaces ) ;
2829
29- return baseType . Name + string . Join ( "" , implementedInterfaces . Select ( x => x . Name ) ) + NameSuffix ;
30+ /// <summary>
31+ /// Gets the runtime stunt name from its base type and optional additional interfaces
32+ /// and the given <paramref name="suffix"/> appended to the type name.
33+ /// </summary>
34+ public static string GetName ( string suffix , Type baseType , params Type [ ] additionalInterfaces )
35+ {
36+ if ( baseType . IsClass )
37+ {
38+ return new StringBuilder ( )
39+ . AddName ( baseType )
40+ . AddNames ( additionalInterfaces . OrderBy ( x => x . Name , StringComparer . Ordinal ) )
41+ . Append ( suffix )
42+ . ToString ( ) ;
43+ }
44+ else
45+ {
46+ return new StringBuilder ( )
47+ . AddNames ( new [ ] { baseType }
48+ . Concat ( additionalInterfaces )
49+ . OrderBy ( x => x . Name , StringComparer . Ordinal ) )
50+ . Append ( suffix )
51+ . ToString ( ) ;
52+ }
3053 }
3154
55+ /// <summary>
56+ /// Gets the runtime stunt full name from its base type and optional additional interfaces,
57+ /// using the <see cref="DefaultNamespace"/> and <see cref="DefaultSuffix"/>.
58+ /// </summary>
59+ public static string GetFullName ( Type baseType , params Type [ ] additionalInterfaces )
60+ => GetFullName ( DefaultNamespace , DefaultSuffix , baseType , additionalInterfaces ) ;
61+
3262 /// <summary>
3363 /// Gets the runtime stunt full name from its base type and implemented interfaces.
3464 /// </summary>
35- public static string GetFullName ( Type baseType , Type [ ] implementedInterfaces )
36- => Namespace + "." + GetName ( baseType , implementedInterfaces ) ;
65+ public static string GetFullName ( string @namespace , Type baseType , params Type [ ] additionalInterfaces )
66+ => GetFullName ( @namespace , DefaultSuffix , baseType , additionalInterfaces ) ;
67+
68+ /// <summary>
69+ /// Gets the runtime stunt full name from its base type and implemented interfaces.
70+ /// </summary>
71+ public static string GetFullName ( string @namespace , string suffix , Type baseType , params Type [ ] additionalInterfaces )
72+ => @namespace + "." + GetName ( suffix , baseType , additionalInterfaces ) ;
73+ }
74+
75+ internal static class StringBuilderExtensions
76+ {
77+ public static StringBuilder AddNames ( this StringBuilder builder , IEnumerable < Type > types )
78+ {
79+ foreach ( var type in types )
80+ {
81+ builder . AddName ( type ) ;
82+ if ( type . IsConstructedGenericType )
83+ {
84+ builder . Append ( "Of" ) . AddNames ( type . GenericTypeArguments ) ;
85+ }
86+ }
87+ return builder ;
88+ }
89+
90+ public static StringBuilder AddName ( this StringBuilder builder , Type type )
91+ => type . IsGenericType ? builder . Append ( type . Name . Substring ( 0 , type . Name . IndexOf ( '`' ) ) ) : builder . Append ( type . Name ) ;
3792 }
3893}
0 commit comments