|
1 | 1 | //Copyright (c) ServiceStack, Inc. All Rights Reserved. |
2 | 2 | //License: https://raw.github.com/ServiceStack/ServiceStack/master/license.txt |
3 | 3 |
|
4 | | -#if !(PCL || LITE || NO_DYNAMIC) |
| 4 | +#if NET45 || NETCORE2_1 |
5 | 5 |
|
6 | 6 | using System; |
7 | 7 | using System.Collections.Generic; |
|
10 | 10 | using ServiceStack.Text.Common; |
11 | 11 | using ServiceStack.Text.Json; |
12 | 12 | using System.Linq; |
13 | | -using System.Text; |
14 | 13 |
|
15 | | -#if !(__IOS__) |
16 | 14 | using System.Reflection; |
17 | 15 | using System.Reflection.Emit; |
18 | | -#endif |
19 | 16 |
|
20 | 17 | namespace ServiceStack |
21 | 18 | { |
@@ -188,157 +185,5 @@ internal static string Underscored(IEnumerable<char> pascalCase) |
188 | 185 | } |
189 | 186 | } |
190 | 187 |
|
191 | | -#if !(__IOS__) |
192 | | - public static class DynamicProxy |
193 | | - { |
194 | | - public static T GetInstanceFor<T>() |
195 | | - { |
196 | | - return (T)GetInstanceFor(typeof(T)); |
197 | | - } |
198 | | - |
199 | | - static readonly ModuleBuilder ModuleBuilder; |
200 | | - static readonly AssemblyBuilder DynamicAssembly; |
201 | | - static readonly Type[] EmptyTypes = new Type[0]; |
202 | | - |
203 | | - public static object GetInstanceFor(Type targetType) |
204 | | - { |
205 | | - lock (DynamicAssembly) |
206 | | - { |
207 | | - var constructedType = DynamicAssembly.GetType(ProxyName(targetType)) ?? GetConstructedType(targetType); |
208 | | - var instance = Activator.CreateInstance(constructedType); |
209 | | - return instance; |
210 | | - } |
211 | | - } |
212 | | - |
213 | | - static string ProxyName(Type targetType) |
214 | | - { |
215 | | - return targetType.Name + "Proxy"; |
216 | | - } |
217 | | - |
218 | | - static DynamicProxy() |
219 | | - { |
220 | | - var assemblyName = new AssemblyName("DynImpl"); |
221 | | -#if NETSTANDARD2_0 |
222 | | - DynamicAssembly = AssemblyBuilder.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run); |
223 | | -#else |
224 | | - DynamicAssembly = AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.RunAndSave); |
225 | | -#endif |
226 | | - ModuleBuilder = DynamicAssembly.DefineDynamicModule("DynImplModule"); |
227 | | - } |
228 | | - |
229 | | - static Type GetConstructedType(Type targetType) |
230 | | - { |
231 | | - var typeBuilder = ModuleBuilder.DefineType(targetType.Name + "Proxy", TypeAttributes.Public); |
232 | | - |
233 | | - var ctorBuilder = typeBuilder.DefineConstructor( |
234 | | - MethodAttributes.Public, |
235 | | - CallingConventions.Standard, |
236 | | - new Type[] { }); |
237 | | - var ilGenerator = ctorBuilder.GetILGenerator(); |
238 | | - ilGenerator.Emit(OpCodes.Ret); |
239 | | - |
240 | | - IncludeType(targetType, typeBuilder); |
241 | | - |
242 | | - foreach (var face in targetType.GetInterfaces()) |
243 | | - IncludeType(face, typeBuilder); |
244 | | - |
245 | | -#if NETSTANDARD2_0 |
246 | | - return typeBuilder.CreateTypeInfo().AsType(); |
247 | | -#else |
248 | | - return typeBuilder.CreateType(); |
249 | | -#endif |
250 | | - } |
251 | | - |
252 | | - static void IncludeType(Type typeOfT, TypeBuilder typeBuilder) |
253 | | - { |
254 | | - var methodInfos = typeOfT.GetMethods(); |
255 | | - foreach (var methodInfo in methodInfos) |
256 | | - { |
257 | | - if (methodInfo.Name.StartsWith("set_", StringComparison.Ordinal)) continue; // we always add a set for a get. |
258 | | - |
259 | | - if (methodInfo.Name.StartsWith("get_", StringComparison.Ordinal)) |
260 | | - { |
261 | | - BindProperty(typeBuilder, methodInfo); |
262 | | - } |
263 | | - else |
264 | | - { |
265 | | - BindMethod(typeBuilder, methodInfo); |
266 | | - } |
267 | | - } |
268 | | - |
269 | | - typeBuilder.AddInterfaceImplementation(typeOfT); |
270 | | - } |
271 | | - |
272 | | - static void BindMethod(TypeBuilder typeBuilder, MethodInfo methodInfo) |
273 | | - { |
274 | | - var methodBuilder = typeBuilder.DefineMethod( |
275 | | - methodInfo.Name, |
276 | | - MethodAttributes.Public | MethodAttributes.Virtual, |
277 | | - methodInfo.ReturnType, |
278 | | - methodInfo.GetParameters().Select(p => p.GetType()).ToArray() |
279 | | - ); |
280 | | - var methodILGen = methodBuilder.GetILGenerator(); |
281 | | - if (methodInfo.ReturnType == typeof(void)) |
282 | | - { |
283 | | - methodILGen.Emit(OpCodes.Ret); |
284 | | - } |
285 | | - else |
286 | | - { |
287 | | - if (methodInfo.ReturnType.IsValueType || methodInfo.ReturnType.IsEnum) |
288 | | - { |
289 | | - MethodInfo getMethod = typeof(Activator).GetMethod("CreateInstance", new[] { typeof(Type) }); |
290 | | - LocalBuilder lb = methodILGen.DeclareLocal(methodInfo.ReturnType); |
291 | | - methodILGen.Emit(OpCodes.Ldtoken, lb.LocalType); |
292 | | - methodILGen.Emit(OpCodes.Call, typeof(Type).GetMethod("GetTypeFromHandle")); |
293 | | - methodILGen.Emit(OpCodes.Callvirt, getMethod); |
294 | | - methodILGen.Emit(OpCodes.Unbox_Any, lb.LocalType); |
295 | | - } |
296 | | - else |
297 | | - { |
298 | | - methodILGen.Emit(OpCodes.Ldnull); |
299 | | - } |
300 | | - methodILGen.Emit(OpCodes.Ret); |
301 | | - } |
302 | | - typeBuilder.DefineMethodOverride(methodBuilder, methodInfo); |
303 | | - } |
304 | | - |
305 | | - public static void BindProperty(TypeBuilder typeBuilder, MethodInfo methodInfo) |
306 | | - { |
307 | | - // Backing Field |
308 | | - string propertyName = methodInfo.Name.Replace("get_", ""); |
309 | | - Type propertyType = methodInfo.ReturnType; |
310 | | - FieldBuilder backingField = typeBuilder.DefineField("_" + propertyName, propertyType, FieldAttributes.Private); |
311 | | - |
312 | | - //Getter |
313 | | - MethodBuilder backingGet = typeBuilder.DefineMethod("get_" + propertyName, MethodAttributes.Public | |
314 | | - MethodAttributes.SpecialName | MethodAttributes.Virtual | |
315 | | - MethodAttributes.HideBySig, propertyType, EmptyTypes); |
316 | | - ILGenerator getIl = backingGet.GetILGenerator(); |
317 | | - |
318 | | - getIl.Emit(OpCodes.Ldarg_0); |
319 | | - getIl.Emit(OpCodes.Ldfld, backingField); |
320 | | - getIl.Emit(OpCodes.Ret); |
321 | | - |
322 | | - |
323 | | - //Setter |
324 | | - MethodBuilder backingSet = typeBuilder.DefineMethod("set_" + propertyName, MethodAttributes.Public | |
325 | | - MethodAttributes.SpecialName | MethodAttributes.Virtual | |
326 | | - MethodAttributes.HideBySig, null, new[] { propertyType }); |
327 | | - |
328 | | - ILGenerator setIl = backingSet.GetILGenerator(); |
329 | | - |
330 | | - setIl.Emit(OpCodes.Ldarg_0); |
331 | | - setIl.Emit(OpCodes.Ldarg_1); |
332 | | - setIl.Emit(OpCodes.Stfld, backingField); |
333 | | - setIl.Emit(OpCodes.Ret); |
334 | | - |
335 | | - // Property |
336 | | - PropertyBuilder propertyBuilder = typeBuilder.DefineProperty(propertyName, PropertyAttributes.None, propertyType, null); |
337 | | - propertyBuilder.SetGetMethod(backingGet); |
338 | | - propertyBuilder.SetSetMethod(backingSet); |
339 | | - } |
340 | | - } |
341 | | -#endif |
342 | | - |
343 | 188 | } |
344 | 189 | #endif |
0 commit comments