Skip to content

Commit d69b1ac

Browse files
committed
Refactor to support PCL
1 parent 5f97fcb commit d69b1ac

74 files changed

Lines changed: 2865 additions & 2079 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/ServiceStack.Text.sln

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".nuget", ".nuget", "{EEDB29
2424
.nuget\NuGet.targets = .nuget\NuGet.targets
2525
EndProjectSection
2626
EndProject
27+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ServiceStack.Text.PCL", "ServiceStack.Text\ServiceStack.Text.PCL.csproj", "{9C22FC96-80DD-4C68-9B13-A8A96D3339B3}"
28+
EndProject
2729
Global
2830
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2931
Debug|Any CPU = Debug|Any CPU
@@ -97,6 +99,30 @@ Global
9799
{9770BD40-AA3B-4785-B5E0-F4C470F9F14E}.Signed|x86.ActiveCfg = Signed|x86
98100
{9770BD40-AA3B-4785-B5E0-F4C470F9F14E}.Signed|x86.Build.0 = Signed|x86
99101
{9770BD40-AA3B-4785-B5E0-F4C470F9F14E}.Signed|Xbox 360.ActiveCfg = Signed|Any CPU
102+
{9C22FC96-80DD-4C68-9B13-A8A96D3339B3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
103+
{9C22FC96-80DD-4C68-9B13-A8A96D3339B3}.Debug|Any CPU.Build.0 = Debug|Any CPU
104+
{9C22FC96-80DD-4C68-9B13-A8A96D3339B3}.Debug|ARM.ActiveCfg = Debug|Any CPU
105+
{9C22FC96-80DD-4C68-9B13-A8A96D3339B3}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
106+
{9C22FC96-80DD-4C68-9B13-A8A96D3339B3}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
107+
{9C22FC96-80DD-4C68-9B13-A8A96D3339B3}.Debug|x64.ActiveCfg = Debug|Any CPU
108+
{9C22FC96-80DD-4C68-9B13-A8A96D3339B3}.Debug|x86.ActiveCfg = Debug|Any CPU
109+
{9C22FC96-80DD-4C68-9B13-A8A96D3339B3}.Debug|Xbox 360.ActiveCfg = Debug|Any CPU
110+
{9C22FC96-80DD-4C68-9B13-A8A96D3339B3}.Release|Any CPU.ActiveCfg = Release|Any CPU
111+
{9C22FC96-80DD-4C68-9B13-A8A96D3339B3}.Release|Any CPU.Build.0 = Release|Any CPU
112+
{9C22FC96-80DD-4C68-9B13-A8A96D3339B3}.Release|ARM.ActiveCfg = Release|Any CPU
113+
{9C22FC96-80DD-4C68-9B13-A8A96D3339B3}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
114+
{9C22FC96-80DD-4C68-9B13-A8A96D3339B3}.Release|Mixed Platforms.Build.0 = Release|Any CPU
115+
{9C22FC96-80DD-4C68-9B13-A8A96D3339B3}.Release|x64.ActiveCfg = Release|Any CPU
116+
{9C22FC96-80DD-4C68-9B13-A8A96D3339B3}.Release|x86.ActiveCfg = Release|Any CPU
117+
{9C22FC96-80DD-4C68-9B13-A8A96D3339B3}.Release|Xbox 360.ActiveCfg = Release|Any CPU
118+
{9C22FC96-80DD-4C68-9B13-A8A96D3339B3}.Signed|Any CPU.ActiveCfg = Release|Any CPU
119+
{9C22FC96-80DD-4C68-9B13-A8A96D3339B3}.Signed|Any CPU.Build.0 = Release|Any CPU
120+
{9C22FC96-80DD-4C68-9B13-A8A96D3339B3}.Signed|ARM.ActiveCfg = Release|Any CPU
121+
{9C22FC96-80DD-4C68-9B13-A8A96D3339B3}.Signed|Mixed Platforms.ActiveCfg = Release|Any CPU
122+
{9C22FC96-80DD-4C68-9B13-A8A96D3339B3}.Signed|Mixed Platforms.Build.0 = Release|Any CPU
123+
{9C22FC96-80DD-4C68-9B13-A8A96D3339B3}.Signed|x64.ActiveCfg = Release|Any CPU
124+
{9C22FC96-80DD-4C68-9B13-A8A96D3339B3}.Signed|x86.ActiveCfg = Release|Any CPU
125+
{9C22FC96-80DD-4C68-9B13-A8A96D3339B3}.Signed|Xbox 360.ActiveCfg = Release|Any CPU
100126
EndGlobalSection
101127
GlobalSection(SolutionProperties) = preSolution
102128
HideSolutionNode = FALSE

src/ServiceStack.Text/AssemblyUtils.cs

Lines changed: 20 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44
using System.Linq;
55
using System.Reflection;
66
using System.Text.RegularExpressions;
7-
#if SILVERLIGHT
8-
9-
#endif
107
using System.Threading;
118
using 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

Comments
 (0)