@@ -42,7 +42,7 @@ internal static class TypeUtils
42
42
/// </summary>
43
43
private static readonly string OrleansCoreAssembly = typeof ( IGrain ) . Assembly . GetName ( ) . FullName ;
44
44
45
- private static readonly ConcurrentDictionary < Tuple < Type , string , bool , bool > , string > ParseableNameCache = new ConcurrentDictionary < Tuple < Type , string , bool , bool > , string > ( ) ;
45
+ private static readonly ConcurrentDictionary < Tuple < Type , TypeFormattingOptions > , string > ParseableNameCache = new ConcurrentDictionary < Tuple < Type , TypeFormattingOptions > , string > ( ) ;
46
46
47
47
private static readonly ConcurrentDictionary < Tuple < Type , bool > , List < Type > > ReferencedTypes = new ConcurrentDictionary < Tuple < Type , bool > , List < Type > > ( ) ;
48
48
@@ -60,8 +60,15 @@ public static string GetSimpleTypeName(Type t, Func<Type, bool> fullName=null, L
60
60
if ( typeInfo . IsNestedPublic || typeInfo . IsNestedPrivate )
61
61
{
62
62
if ( typeInfo . DeclaringType . IsGenericType )
63
- return GetTemplatedName ( GetUntemplatedTypeName ( typeInfo . DeclaringType . Name ) , typeInfo . DeclaringType , typeInfo . GetGenericArguments ( ) , _ => true , language ) + "." + GetUntemplatedTypeName ( typeInfo . Name ) ;
64
-
63
+ {
64
+ return GetTemplatedName (
65
+ GetUntemplatedTypeName ( typeInfo . DeclaringType . Name ) ,
66
+ typeInfo . DeclaringType ,
67
+ typeInfo . GetGenericArguments ( ) ,
68
+ _ => true ,
69
+ language ) + "." + GetUntemplatedTypeName ( typeInfo . Name ) ;
70
+ }
71
+
65
72
return GetTemplatedName ( typeInfo . DeclaringType , language : language ) + "." + GetUntemplatedTypeName ( typeInfo . Name ) ;
66
73
}
67
74
@@ -618,25 +625,25 @@ public static string GetUnadornedMethodName(this MethodInfo method)
618
625
/// <returns>
619
626
/// A string representation of the <paramref name="type"/>.
620
627
/// </returns>
621
- public static string GetParseableName ( this Type type , string nameSuffix = null , bool includeNamespace = true , bool includeGenericParameters = true )
628
+ public static string GetParseableName ( this Type type , TypeFormattingOptions options = null )
622
629
{
623
- return
624
- ParseableNameCache . GetOrAdd (
625
- Tuple . Create ( type , nameSuffix , includeNamespace , includeGenericParameters ) ,
626
- _ =>
627
- {
628
- var builder = new StringBuilder ( ) ;
629
- var typeInfo = type . GetTypeInfo ( ) ;
630
- GetParseableName (
631
- type ,
632
- nameSuffix ?? string . Empty ,
633
- builder ,
634
- new Queue < Type > (
635
- typeInfo . IsGenericTypeDefinition ? typeInfo . GetGenericArguments ( ) : typeInfo . GenericTypeArguments ) ,
636
- includeNamespace ,
637
- includeGenericParameters ) ;
638
- return builder . ToString ( ) ;
639
- } ) ;
630
+ options = options ?? new TypeFormattingOptions ( ) ;
631
+ return ParseableNameCache . GetOrAdd (
632
+ Tuple . Create ( type , options ) ,
633
+ _ =>
634
+ {
635
+ var builder = new StringBuilder ( ) ;
636
+ var typeInfo = type . GetTypeInfo ( ) ;
637
+ GetParseableName (
638
+ type ,
639
+ builder ,
640
+ new Queue < Type > (
641
+ typeInfo . IsGenericTypeDefinition
642
+ ? typeInfo . GetGenericArguments ( )
643
+ : typeInfo . GenericTypeArguments ) ,
644
+ options ) ;
645
+ return builder . ToString ( ) ;
646
+ } ) ;
640
647
}
641
648
642
649
/// <summary>
@@ -651,33 +658,28 @@ public static string GetParseableName(this Type type, string nameSuffix = null,
651
658
/// <param name="typeArguments">
652
659
/// The type arguments of <paramref name="type"/>.
653
660
/// </param>
654
- /// <param name="includeNamespace ">
655
- /// A value indicating whether or not to include the namespace name .
661
+ /// <param name="options ">
662
+ /// The type formatting options .
656
663
/// </param>
657
664
private static void GetParseableName (
658
665
Type type ,
659
- string nameSuffix ,
660
666
StringBuilder builder ,
661
667
Queue < Type > typeArguments ,
662
- bool includeNamespace = true ,
663
- bool includeGenericParameters = true )
668
+ TypeFormattingOptions options )
664
669
{
665
670
var typeInfo = type . GetTypeInfo ( ) ;
666
671
if ( typeInfo . IsArray )
667
672
{
668
673
builder . AppendFormat (
669
674
"{0}[{1}]" ,
670
- typeInfo . GetElementType ( )
671
- . GetParseableName (
672
- includeNamespace : includeNamespace ,
673
- includeGenericParameters : includeGenericParameters ) ,
675
+ typeInfo . GetElementType ( ) . GetParseableName ( options ) ,
674
676
string . Concat ( Enumerable . Range ( 0 , type . GetArrayRank ( ) - 1 ) . Select ( _ => ',' ) ) ) ;
675
677
return ;
676
678
}
677
679
678
680
if ( typeInfo . IsGenericParameter )
679
681
{
680
- if ( includeGenericParameters )
682
+ if ( options . IncludeGenericTypeParameters )
681
683
{
682
684
builder . Append ( typeInfo . GetUnadornedTypeName ( ) ) ;
683
685
}
@@ -688,57 +690,63 @@ private static void GetParseableName(
688
690
if ( typeInfo . DeclaringType != null )
689
691
{
690
692
// This is not the root type.
691
- GetParseableName ( typeInfo . DeclaringType , string . Empty , builder , typeArguments , includeNamespace , includeGenericParameters ) ;
692
- builder . Append ( '.' ) ;
693
+ GetParseableName ( typeInfo . DeclaringType , builder , typeArguments , options ) ;
694
+ builder . Append ( options . NestedTypeSeparator ) ;
693
695
}
694
- else if ( ! string . IsNullOrWhiteSpace ( type . Namespace ) && includeNamespace )
696
+ else if ( ! string . IsNullOrWhiteSpace ( type . Namespace ) && options . IncludeNamespace )
695
697
{
696
- // This is the root type.
697
- builder . AppendFormat ( "global::{0}." , type . Namespace ) ;
698
+ // This is the root type, so include the namespace.
699
+ var namespaceName = type . Namespace ;
700
+ if ( options . NestedTypeSeparator != '.' )
701
+ {
702
+ namespaceName = namespaceName . Replace ( '.' , options . NestedTypeSeparator ) ;
703
+ }
704
+
705
+ if ( options . IncludeGlobal )
706
+ {
707
+ builder . AppendFormat ( "global::" ) ;
708
+ }
709
+
710
+ builder . AppendFormat ( "{0}{1}" , namespaceName , options . NestedTypeSeparator ) ;
698
711
}
699
712
700
713
if ( typeInfo . IsConstructedGenericType )
701
714
{
702
715
// Get the unadorned name, the generic parameters, and add them together.
703
- var unadornedTypeName = typeInfo . GetUnadornedTypeName ( ) + nameSuffix ;
716
+ var unadornedTypeName = typeInfo . GetUnadornedTypeName ( ) + options . NameSuffix ;
704
717
builder . Append ( EscapeIdentifier ( unadornedTypeName ) ) ;
705
718
var generics =
706
719
Enumerable . Range ( 0 , Math . Min ( typeInfo . GetGenericArguments ( ) . Count ( ) , typeArguments . Count ) )
707
720
. Select ( _ => typeArguments . Dequeue ( ) )
708
721
. ToList ( ) ;
709
- if ( generics . Count > 0 )
722
+ if ( generics . Count > 0 && options . IncludeTypeParameters )
710
723
{
711
724
var genericParameters = string . Join (
712
725
"," ,
713
- generics . Select (
714
- generic =>
715
- GetParseableName (
716
- generic ,
717
- includeNamespace : includeNamespace ,
718
- includeGenericParameters : includeGenericParameters ) ) ) ;
726
+ generics . Select ( generic => GetParseableName ( generic , options ) ) ) ;
719
727
builder . AppendFormat ( "<{0}>" , genericParameters ) ;
720
728
}
721
729
}
722
730
else if ( typeInfo . IsGenericTypeDefinition )
723
731
{
724
732
// Get the unadorned name, the generic parameters, and add them together.
725
- var unadornedTypeName = type . GetUnadornedTypeName ( ) + nameSuffix ;
733
+ var unadornedTypeName = type . GetUnadornedTypeName ( ) + options . NameSuffix ;
726
734
builder . Append ( EscapeIdentifier ( unadornedTypeName ) ) ;
727
735
var generics =
728
736
Enumerable . Range ( 0 , Math . Min ( type . GetGenericArguments ( ) . Count ( ) , typeArguments . Count ) )
729
737
. Select ( _ => typeArguments . Dequeue ( ) )
730
738
. ToList ( ) ;
731
- if ( generics . Count > 0 )
739
+ if ( generics . Count > 0 && options . IncludeTypeParameters )
732
740
{
733
741
var genericParameters = string . Join (
734
742
"," ,
735
- generics . Select ( _ => includeGenericParameters ? _ . ToString ( ) : string . Empty ) ) ;
743
+ generics . Select ( _ => options . IncludeGenericTypeParameters ? _ . ToString ( ) : string . Empty ) ) ;
736
744
builder . AppendFormat ( "<{0}>" , genericParameters ) ;
737
745
}
738
746
}
739
747
else
740
748
{
741
- builder . Append ( EscapeIdentifier ( type . GetUnadornedTypeName ( ) + nameSuffix ) ) ;
749
+ builder . Append ( EscapeIdentifier ( type . GetUnadornedTypeName ( ) + options . NameSuffix ) ) ;
742
750
}
743
751
}
744
752
0 commit comments