@@ -9,6 +9,8 @@ namespace Tenray.Topaz.Interop;
99/// </summary>
1010public sealed class NamespaceProxy : ITypeProxy
1111{
12+ private Dictionary < string , NamespaceProxy > SubnamespaceProxies = new ( ) ;
13+
1214 /// <summary>
1315 /// Namespace name.
1416 /// eg: System.Collections
@@ -51,6 +53,11 @@ public sealed class NamespaceProxy : ITypeProxy
5153 /// </summary>
5254 public Type ProxiedType { get ; }
5355
56+ /// <summary>
57+ /// If true, types of the current namespace are accessible.
58+ /// </summary>
59+ public bool EnableTypeRetrieval { get ; set ; }
60+
5461 public NamespaceProxy (
5562 string name ,
5663 IReadOnlySet < string > whitelist ,
@@ -87,26 +94,34 @@ public bool TryGetStaticMember(
8794 return false ;
8895 }
8996
97+ if ( SubnamespaceProxies . TryGetValue ( memberName , out var subProxy ) )
98+ {
99+ value = subProxy ;
100+ return true ;
101+ }
102+
90103 var fullname = Name + "." + memberName ;
91104 var type = FindType ( fullname ) ;
92105 if ( type == null )
93106 {
94107 if ( AllowSubNamespaces )
95108 {
96- value = new NamespaceProxy ( fullname ,
109+ value = subProxy = new NamespaceProxy ( fullname ,
97110 Whitelist ,
98111 true ,
99112 ValueConverter ,
100113 MemberInfoProvider ,
101114 MaxGenericTypeArgumentCount ,
102115 ProxyOptions ) ;
116+ subProxy . EnableTypeRetrieval = true ;
117+ SubnamespaceProxies . Add ( memberName , subProxy ) ;
103118 return true ;
104119 }
105120 value = null ;
106121 return false ;
107122 }
108123
109- if ( ! type . IsPublic )
124+ if ( ! EnableTypeRetrieval || ! type . IsPublic )
110125 {
111126 value = null ;
112127 return false ;
@@ -124,15 +139,28 @@ public bool TryGetStaticMember(
124139 return true ;
125140 }
126141
142+ static Type SearchType ( string typeName )
143+ {
144+ var type = Type . GetType ( typeName , false , false ) ;
145+ if ( type != null ) return type ;
146+ foreach ( var a in AppDomain . CurrentDomain . GetAssemblies ( ) )
147+ {
148+ type = a . GetType ( typeName , false , false ) ;
149+ if ( type != null )
150+ return type ;
151+ }
152+ return null ;
153+ }
154+
127155 private Type FindType ( string fullname )
128156 {
129- var type = Type . GetType ( fullname , false , false ) ;
157+ var type = SearchType ( fullname ) ;
130158 if ( type != null )
131159 return type ;
132160 // search for generic types.
133161 for ( var i = 1 ; i < MaxGenericTypeArgumentCount ; ++ i )
134162 {
135- type = Type . GetType ( fullname + "`" + i , false , false ) ;
163+ type = SearchType ( fullname + "`" + i ) ;
136164 if ( type != null )
137165 return type ;
138166 }
@@ -152,4 +180,23 @@ public override string ToString()
152180 {
153181 return Name ;
154182 }
183+
184+ public void AddSubNameSpaces ( Span < string > parts ,
185+ IReadOnlySet < string > whitelist ,
186+ bool allowSubNamespaces )
187+ {
188+ if ( parts . Length == 0 )
189+ {
190+ EnableTypeRetrieval = true ;
191+ return ;
192+ }
193+ if ( SubnamespaceProxies . TryGetValue ( parts [ 0 ] , out var proxy ) )
194+ {
195+ proxy . AddSubNameSpaces ( parts . Slice ( 1 ) , whitelist , allowSubNamespaces ) ;
196+ return ;
197+ }
198+ var subProxy = new NamespaceProxy ( Name + "." + parts [ 0 ] , whitelist , allowSubNamespaces && parts . Length == 1 , ValueConverter , MemberInfoProvider , MaxGenericTypeArgumentCount ) ;
199+ subProxy . AddSubNameSpaces ( parts . Slice ( 1 ) , whitelist , allowSubNamespaces ) ;
200+ SubnamespaceProxies . Add ( parts [ 0 ] , subProxy ) ;
201+ }
155202}
0 commit comments