44using System . Linq ;
55using System . Reflection ;
66using System . Text . RegularExpressions ;
7- #if SILVERLIGHT
8-
9- #endif
107using System . Threading ;
118using ServiceStack . Common . Support ;
129
@@ -18,8 +15,6 @@ namespace ServiceStack.Text
1815 public static class AssemblyUtils
1916 {
2017 private const string FileUri = "file:///" ;
21- private const string DllExt = "dll" ;
22- private const string ExeExt = "dll" ;
2318 private const char UriSeperator = '/' ;
2419
2520 private static Dictionary < string , Type > TypeCache = new Dictionary < string , Type > ( ) ;
@@ -35,7 +30,7 @@ public static Type FindType(string typeName)
3530 Type type = null ;
3631 if ( TypeCache . TryGetValue ( typeName , out type ) ) return type ;
3732
38- #if ! SILVERLIGHT
33+ #if ! SL5
3934 type = Type . GetType ( typeName ) ;
4035#endif
4136 if ( type == null )
@@ -60,30 +55,20 @@ public static Type FindType(string typeName)
6055 }
6156#endif
6257
63- #if ! XBOX
64-
65-
66- /// <summary>
67- /// The top-most interface of the given type, if any.
68- /// </summary>
69- public static Type MainInterface < T > ( )
58+ /// <summary>
59+ /// The top-most interface of the given type, if any.
60+ /// </summary>
61+ public static Type MainInterface < T > ( )
7062 {
71- var t = typeof ( T ) ;
72- #if NETFX_CORE
73- if ( t . GetTypeInfo ( ) . BaseType == typeof ( object ) ) {
74- // on Windows, this can be just "t.GetInterfaces()" but Mono doesn't return in order.
75- var interfaceType = t . GetTypeInfo ( ) . ImplementedInterfaces . FirstOrDefault ( i => ! t . GetTypeInfo ( ) . ImplementedInterfaces . Any ( i2 => i2 . GetTypeInfo ( ) . ImplementedInterfaces . Contains ( i ) ) ) ;
76- if ( interfaceType != null ) return interfaceType ;
77- }
78- #else
79- if ( t . BaseType == typeof ( object ) ) {
80- // on Windows, this can be just "t.GetInterfaces()" but Mono doesn't return in order.
81- var interfaceType = t . GetInterfaces ( ) . FirstOrDefault ( i => ! t . GetInterfaces ( ) . Any ( i2 => i2 . GetInterfaces ( ) . Contains ( i ) ) ) ;
82- if ( interfaceType != null ) return interfaceType ;
83- }
84- #endif
85- return t ; // not safe to use interface, as it might be a superclass's one.
86- }
63+ var t = typeof ( T ) ;
64+ if ( t . BaseType ( ) == typeof ( object ) )
65+ {
66+ // on Windows, this can be just "t.GetInterfaces()" but Mono doesn't return in order.
67+ var interfaceType = t . Interfaces ( ) . FirstOrDefault ( i => ! t . Interfaces ( ) . Any ( i2 => i2 . Interfaces ( ) . Contains ( i ) ) ) ;
68+ if ( interfaceType != null ) return interfaceType ;
69+ }
70+ return t ; // not safe to use interface, as it might be a superclass's one.
71+ }
8772
8873 /// <summary>
8974 /// Find type if it exists
@@ -99,80 +84,12 @@ public static Type FindType(string typeName, string assemblyName)
9984 return type ;
10085 }
10186
102- #if ! NETFX_CORE
103- var binPath = GetAssemblyBinPath ( Assembly . GetExecutingAssembly ( ) ) ;
104- Assembly assembly = null ;
105- var assemblyDllPath = binPath + String . Format ( "{0}.{1}" , assemblyName , DllExt ) ;
106- if ( File . Exists ( assemblyDllPath ) )
107- {
108- assembly = LoadAssembly ( assemblyDllPath ) ;
109- }
110- var assemblyExePath = binPath + String . Format ( "{0}.{1}" , assemblyName , ExeExt ) ;
111- if ( File . Exists ( assemblyExePath ) )
112- {
113- assembly = LoadAssembly ( assemblyExePath ) ;
114- }
115- return assembly != null ? assembly . GetType ( typeName ) : null ;
116- #else
117- return null ;
118- #endif
87+ return PclExport . Instance . FindType ( typeName , assemblyName ) ;
11988 }
120- #endif
12189
122- #if NETFX_CORE
123- private sealed class AppDomain
124- {
125- public static AppDomain CurrentDomain { get ; private set ; }
126- public static Assembly [ ] cacheObj = null ;
127-
128- static AppDomain ( )
129- {
130- CurrentDomain = new AppDomain ( ) ;
131- }
132-
133- public Assembly [ ] GetAssemblies ( )
134- {
135- return cacheObj ?? GetAssemblyListAsync ( ) . Result . ToArray ( ) ;
136- }
137-
138- private async System . Threading . Tasks . Task < IEnumerable < Assembly > > GetAssemblyListAsync ( )
139- {
140- var folder = Windows . ApplicationModel . Package . Current . InstalledLocation ;
141-
142- List < Assembly > assemblies = new List < Assembly > ( ) ;
143- foreach ( Windows . Storage . StorageFile file in await folder . GetFilesAsync ( ) )
144- {
145- if ( file . FileType == ".dll" || file . FileType == ".exe" )
146- {
147- try
148- {
149- var filename = file . Name . Substring ( 0 , file . Name . Length - file . FileType . Length ) ;
150- AssemblyName name = new AssemblyName ( ) { Name = filename } ;
151- Assembly asm = Assembly . Load ( name ) ;
152- assemblies . Add ( asm ) ;
153- }
154- catch ( Exception )
155- {
156- // Invalid WinRT assembly!
157- }
158- }
159- }
160-
161- cacheObj = assemblies . ToArray ( ) ;
162-
163- return cacheObj ;
164- }
165- }
166- #endif
167-
168- #if ! XBOX
16990 public static Type FindTypeFromLoadedAssemblies ( string typeName )
17091 {
171- #if SILVERLIGHT4
172- var assemblies = ( ( dynamic ) AppDomain . CurrentDomain ) . GetAssemblies ( ) as Assembly [ ] ;
173- #else
174- var assemblies = AppDomain . CurrentDomain . GetAssemblies ( ) ;
175- #endif
92+ var assemblies = PclExport . Instance . GetAllAssemblies ( ) ;
17693 foreach ( var assembly in assemblies )
17794 {
17895 var type = assembly . GetType ( typeName ) ;
@@ -183,46 +100,15 @@ public static Type FindTypeFromLoadedAssemblies(string typeName)
183100 }
184101 return null ;
185102 }
186- #endif
187103
188- #if ! SILVERLIGHT
189- private static Assembly LoadAssembly ( string assemblyPath )
190- {
191- return Assembly . LoadFrom ( assemblyPath ) ;
192- }
193- #elif NETFX_CORE
194- private static Assembly LoadAssembly ( string assemblyPath )
104+ public static Assembly LoadAssembly ( string assemblyPath )
195105 {
196- return Assembly . Load ( new AssemblyName ( assemblyPath ) ) ;
106+ return PclExport . Instance . LoadAssembly ( assemblyPath ) ;
197107 }
198- #elif WINDOWS_PHONE
199- private static Assembly LoadAssembly ( string assemblyPath )
200- {
201- return Assembly . LoadFrom ( assemblyPath ) ;
202- }
203- #else
204- private static Assembly LoadAssembly ( string assemblyPath )
205- {
206- var sri = System . Windows . Application . GetResourceStream ( new Uri ( assemblyPath , UriKind . Relative ) ) ;
207- var myPart = new System . Windows . AssemblyPart ( ) ;
208- var assembly = myPart . Load ( sri . Stream ) ;
209- return assembly ;
210- }
211- #endif
212108
213- #if ! XBOX
214109 public static string GetAssemblyBinPath ( Assembly assembly )
215110 {
216- #if WINDOWS_PHONE
217- var codeBase = assembly . GetName ( ) . CodeBase ;
218- #elif NETFX_CORE
219- var codeBase = assembly . GetName ( ) . FullName ;
220- #elif SILVERLIGHT
221- var codeBase = assembly . FullName ;
222- #else
223- var codeBase = assembly . CodeBase ;
224- #endif
225-
111+ var codeBase = PclExport . Instance . GetAssemblyCodeBase ( assembly ) ;
226112 var binPathPos = codeBase . LastIndexOf ( UriSeperator ) ;
227113 var assemblyPath = codeBase . Substring ( 0 , binPathPos + 1 ) ;
228114 if ( assemblyPath . StartsWith ( FileUri , StringComparison . OrdinalIgnoreCase ) )
@@ -231,13 +117,8 @@ public static string GetAssemblyBinPath(Assembly assembly)
231117 }
232118 return assemblyPath ;
233119 }
234- #endif
235120
236- #if ! SILVERLIGHT
237- static readonly Regex versionRegEx = new Regex ( ", Version=[^\\ ]]+" , RegexOptions . Compiled ) ;
238- #else
239- static readonly Regex versionRegEx = new Regex ( ", Version=[^\\ ]]+" ) ;
240- #endif
121+ static readonly Regex versionRegEx = new Regex ( ", Version=[^\\ ]]+" , PclExport . Instance . RegexOptions ) ;
241122 public static string ToTypeString ( this Type type )
242123 {
243124 return versionRegEx . Replace ( type . AssemblyQualifiedName , "" ) ;
0 commit comments