public static Type GetProxy(AppDomain domain, params Type[] interfaces) { ProxyKey proxyKey = new ProxyKey(domain, interfaces); Type proxy = null; try { #if (dotNET35 || dotNET4) proxyCacheLock.EnterReadLock(); #else proxyCacheLock.AcquireReaderLock(-1); #endif #if !(dotNET10 || dotNET11) if (proxyCache.TryGetValue(proxyKey, out proxy)) { return(proxy); #else if (proxyCache.ContainsKey(proxyKey)) { return((Type)proxyCache[proxyKey]); #endif } } finally { #if (dotNET35 || dotNET4) proxyCacheLock.ExitReadLock(); #else proxyCacheLock.ReleaseReaderLock(); #endif } try { #if (dotNET35 || dotNET4) proxyCacheLock.EnterWriteLock(); #else proxyCacheLock.AcquireWriterLock(-1); #endif #if !(dotNET10 || dotNET11) if (proxyCache.TryGetValue(proxyKey, out proxy)) { return(proxy); #else if (proxyCache.ContainsKey(proxyKey)) { return((Type)proxyCache[proxyKey]); #endif } proxy = GetProxyWithoutCache(domain, interfaces); proxyCache[proxyKey] = proxy; } finally { #if (dotNET35 || dotNET4) proxyCacheLock.ExitWriteLock(); #else proxyCacheLock.ReleaseWriterLock(); #endif } return(proxy); }
public static Type GetProxy(AppDomain domain, params Type[] interfaces) { lock (typeof(DynamicProxy)) { ProxyKey proxyKey = new ProxyKey(domain, interfaces); Type proxy = null; if (proxyCache.ContainsKey(proxyKey)) { proxy = (Type)proxyCache[proxyKey]; } if (proxy == null) { interfaces = SumUpInterfaces(interfaces); String dynamicAssemblyName; String dynamicModuleName; String dynamicProxyTypeName; String strNumber = countDymamicAssembly.ToString(NumberFormatInfo.InvariantInfo); dynamicAssemblyName = "$DynamicAssembly" + strNumber; dynamicModuleName = "$DynamicModule" + strNumber; dynamicProxyTypeName = "$Proxy" + strNumber; countDymamicAssembly++; AssemblyBuilder assemblyBuilder; AssemblyName assemblyName = new AssemblyName(); assemblyName.Name = dynamicAssemblyName; #if !SILVERLIGHT assemblyBuilder = domain.DefineDynamicAssembly(assemblyName, FlagCreateFile ? AssemblyBuilderAccess.RunAndSave : AssemblyBuilderAccess.Run); #else assemblyBuilder = domain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run); #endif ModuleBuilder moduleBuilder; #if !SILVERLIGHT if (FlagCreateFile) { moduleBuilder = assemblyBuilder.DefineDynamicModule(dynamicModuleName, dynamicModuleName + ".dll"); } else { moduleBuilder = assemblyBuilder.DefineDynamicModule(dynamicModuleName); } #else moduleBuilder = assemblyBuilder.DefineDynamicModule(dynamicModuleName); #endif TypeBuilder typeBuilder = moduleBuilder.DefineType(dynamicProxyTypeName, TypeAttributes.Public, typeof(DynamicProxy), interfaces); //build .ctor ConstructorBuilder ctorBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public | MethodAttributes.HideBySig, CallingConventions.Standard, Types_InvocationHandler); ILGenerator gen = ctorBuilder.GetILGenerator(); gen.Emit(OpCodes.Ldarg_0); gen.Emit(OpCodes.Ldarg_1); gen.Emit(OpCodes.Call, DynamicProxy_Ctor); gen.Emit(OpCodes.Ret); MakeMethods(typeBuilder, typeof(Object), true); foreach (Type interfac in interfaces) { MakeMethods(typeBuilder, interfac, false); } proxy = typeBuilder.CreateType(); proxyCache.Add(proxyKey, proxy); #if !SILVERLIGHT if (FlagCreateFile) { assemblyBuilder.Save(dynamicAssemblyName + ".dll"); } #endif } return proxy; } }
public static Type GetProxy(AppDomain domain, params Type[] interfaces) { lock (typeof(DynamicProxy)) { ProxyKey proxyKey = new ProxyKey(domain, interfaces); Type proxy = null; if (proxyCache.ContainsKey(proxyKey)) { proxy = (Type)proxyCache[proxyKey]; } if (proxy == null) { interfaces = SumUpInterfaces(interfaces); String dynamicAssemblyName; String dynamicModuleName; String dynamicProxyTypeName; String strNumber = countDymamicAssembly.ToString(NumberFormatInfo.InvariantInfo); dynamicAssemblyName = "$DynamicAssembly" + strNumber; dynamicModuleName = "$DynamicModule" + strNumber; dynamicProxyTypeName = "$Proxy" + strNumber; countDymamicAssembly++; AssemblyBuilder assemblyBuilder; AssemblyName assemblyName = new AssemblyName(); assemblyName.Name = dynamicAssemblyName; #if !SILVERLIGHT assemblyBuilder = domain.DefineDynamicAssembly(assemblyName, FlagCreateFile ? AssemblyBuilderAccess.RunAndSave : AssemblyBuilderAccess.Run); #else assemblyBuilder = domain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run); #endif ModuleBuilder moduleBuilder; #if !SILVERLIGHT if (FlagCreateFile) { moduleBuilder = assemblyBuilder.DefineDynamicModule(dynamicModuleName, dynamicModuleName + ".dll"); } else { moduleBuilder = assemblyBuilder.DefineDynamicModule(dynamicModuleName); } #else moduleBuilder = assemblyBuilder.DefineDynamicModule(dynamicModuleName); #endif TypeBuilder typeBuilder = moduleBuilder.DefineType(dynamicProxyTypeName, TypeAttributes.Public, typeof(DynamicProxy), interfaces); //build .ctor ConstructorBuilder ctorBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public | MethodAttributes.HideBySig, CallingConventions.Standard, Types_InvocationHandler); ILGenerator gen = ctorBuilder.GetILGenerator(); gen.Emit(OpCodes.Ldarg_0); gen.Emit(OpCodes.Ldarg_1); gen.Emit(OpCodes.Call, DynamicProxy_Ctor); gen.Emit(OpCodes.Ret); //MakeMethods(typeBuilder, typeof(Object), true); foreach (Type interfac in interfaces) { MakeMethods(typeBuilder, interfac, false); } proxy = typeBuilder.CreateType(); proxyCache.Add(proxyKey, proxy); #if !SILVERLIGHT if (FlagCreateFile) { assemblyBuilder.Save(dynamicAssemblyName + ".dll"); } #endif } return(proxy); } }