コード例 #1
0
        private TypeInfo CreateUncachedProxyType(System.Type baseType, IReadOnlyCollection <System.Type> baseInterfaces)
        {
            AppDomain currentDomain = AppDomain.CurrentDomain;
            string    typeName      = string.Format("{0}Proxy", baseType.Name);
            string    assemblyName  = string.Format("{0}Assembly", typeName);
            string    moduleName    = string.Format("{0}Module", typeName);

            var             name            = new AssemblyName(assemblyName);
            AssemblyBuilder assemblyBuilder = ProxyAssemblyBuilder.DefineDynamicAssembly(currentDomain, name);
            ModuleBuilder   moduleBuilder   = ProxyAssemblyBuilder.DefineDynamicModule(assemblyBuilder, moduleName);

            TypeAttributes typeAttributes = TypeAttributes.AutoClass | TypeAttributes.Class |
                                            TypeAttributes.Public | TypeAttributes.BeforeFieldInit;

            var interfaces = new HashSet <System.Type>();

            interfaces.UnionWith(baseInterfaces);
            interfaces.UnionWith(baseInterfaces.SelectMany(i => i.GetInterfaces()));

            // Use the proxy dummy as the base type
            // since we're not inheriting from any class type
            System.Type parentType = baseType;
            if (baseType.IsInterface)
            {
                parentType = typeof(ProxyDummy);
                interfaces.Add(baseType);
            }
            interfaces.UnionWith(baseType.GetInterfaces());

            // Add the ISerializable interface so that it can be implemented
            interfaces.Add(typeof(ISerializable));

            TypeBuilder typeBuilder = moduleBuilder.DefineType(typeName, typeAttributes, parentType, interfaces.ToArray());

            ConstructorBuilder defaultConstructor = DefineConstructor(typeBuilder, parentType);

            // Implement IProxy
            var implementor = new ProxyImplementor();

            implementor.ImplementProxy(typeBuilder);

            FieldInfo interceptorField = implementor.InterceptorField;

            // Provide a custom implementation of ISerializable
            // instead of redirecting it back to the interceptor
            foreach (MethodInfo method in GetProxiableMethods(baseType, interfaces).Where(method => method.DeclaringType != typeof(ISerializable)))
            {
                ProxyMethodBuilder.CreateProxiedMethod(interceptorField, method, typeBuilder);
            }

            // Make the proxy serializable
            AddSerializationSupport(baseType, baseInterfaces, typeBuilder, interceptorField, defaultConstructor);
            TypeInfo proxyType = typeBuilder.CreateTypeInfo();

            ProxyAssemblyBuilder.Save(assemblyBuilder);
            return(proxyType);
        }
コード例 #2
0
		private System.Type CreateUncachedProxyType(System.Type baseType, System.Type[] baseInterfaces)
		{
			AppDomain currentDomain = AppDomain.CurrentDomain;
			string typeName = string.Format("{0}Proxy", baseType.Name);
			string assemblyName = string.Format("{0}Assembly", typeName);
			string moduleName = string.Format("{0}Module", typeName);

			var name = new AssemblyName(assemblyName);
#if DEBUG
			AssemblyBuilderAccess access = AssemblyBuilderAccess.RunAndSave;
#else
      AssemblyBuilderAccess access = AssemblyBuilderAccess.Run;
#endif
			AssemblyBuilder assemblyBuilder = currentDomain.DefineDynamicAssembly(name, access);

#if DEBUG
			ModuleBuilder moduleBuilder =
				assemblyBuilder.DefineDynamicModule(moduleName, string.Format("{0}.mod", moduleName), true);
#else
       ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule(moduleName);
#endif

			TypeAttributes typeAttributes = TypeAttributes.AutoClass | TypeAttributes.Class |
			                                TypeAttributes.Public | TypeAttributes.BeforeFieldInit;

			var interfaces = new HashSet<System.Type>();
			interfaces.Merge(baseInterfaces);

			// Use the proxy dummy as the base type 
			// since we're not inheriting from any class type
			System.Type parentType = baseType;
			if (baseType.IsInterface)
			{
				parentType = typeof (ProxyDummy);
				interfaces.Add(baseType);
			}

			// Add any inherited interfaces
			System.Type[] computedInterfaces = interfaces.ToArray();
			foreach (System.Type interfaceType in computedInterfaces)
			{
				interfaces.Merge(GetInterfaces(interfaceType));
			}

			// Add the ISerializable interface so that it can be implemented
			interfaces.Add(typeof (ISerializable));

			TypeBuilder typeBuilder = moduleBuilder.DefineType(typeName, typeAttributes, parentType, interfaces.ToArray());

			ConstructorBuilder defaultConstructor = DefineConstructor(typeBuilder);

			// Implement IProxy
			var implementor = new ProxyImplementor();
			implementor.ImplementProxy(typeBuilder);

			FieldInfo interceptorField = implementor.InterceptorField;
			
			// Provide a custom implementation of ISerializable
			// instead of redirecting it back to the interceptor
			foreach (MethodInfo method in GetProxiableMethods(baseType, interfaces).Where(method => method.DeclaringType != typeof(ISerializable)))
			{
				ProxyMethodBuilder.CreateProxiedMethod(interceptorField, method, typeBuilder);
			}

			// Make the proxy serializable
			AddSerializationSupport(baseType, baseInterfaces, typeBuilder, interceptorField, defaultConstructor);
			System.Type proxyType = typeBuilder.CreateType();

#if DEBUG_PROXY_OUTPUT
     assemblyBuilder.Save("generatedAssembly.dll");
#endif
			return proxyType;
		}
コード例 #3
0
        private System.Type CreateUncachedProxyType(System.Type baseType, System.Type[] baseInterfaces)
        {
            AppDomain currentDomain = AppDomain.CurrentDomain;
            string    typeName      = string.Format("{0}Proxy", baseType.Name);
            string    assemblyName  = string.Format("{0}Assembly", typeName);
            string    moduleName    = string.Format("{0}Module", typeName);

            var name = new AssemblyName(assemblyName);

#if DEBUG
            AssemblyBuilderAccess access = AssemblyBuilderAccess.RunAndSave;
#else
            AssemblyBuilderAccess access = AssemblyBuilderAccess.Run;
#endif
            AssemblyBuilder assemblyBuilder = currentDomain.DefineDynamicAssembly(name, access);

#if DEBUG
            ModuleBuilder moduleBuilder =
                assemblyBuilder.DefineDynamicModule(moduleName, string.Format("{0}.mod", moduleName), true);
#else
            ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule(moduleName);
#endif

            TypeAttributes typeAttributes = TypeAttributes.AutoClass | TypeAttributes.Class |
                                            TypeAttributes.Public | TypeAttributes.BeforeFieldInit;

            var interfaces = new HashSet <System.Type>();
            interfaces.Merge(baseInterfaces);

            // Use the proxy dummy as the base type
            // since we're not inheriting from any class type
            System.Type parentType = baseType;
            if (baseType.IsInterface)
            {
                parentType = typeof(ProxyDummy);
                interfaces.Add(baseType);
            }

            // Add any inherited interfaces
            System.Type[] computedInterfaces = interfaces.ToArray();
            foreach (System.Type interfaceType in computedInterfaces)
            {
                interfaces.Merge(GetInterfaces(interfaceType));
            }

            // Add the ISerializable interface so that it can be implemented
            interfaces.Add(typeof(ISerializable));

            TypeBuilder typeBuilder = moduleBuilder.DefineType(typeName, typeAttributes, parentType, interfaces.ToArray());

            ConstructorBuilder defaultConstructor = DefineConstructor(typeBuilder);

            // Implement IProxy
            var implementor = new ProxyImplementor();
            implementor.ImplementProxy(typeBuilder);

            FieldInfo interceptorField = implementor.InterceptorField;

            // Provide a custom implementation of ISerializable
            // instead of redirecting it back to the interceptor
            foreach (MethodInfo method in GetProxiableMethods(baseType, interfaces).Where(method => method.DeclaringType != typeof(ISerializable)))
            {
                ProxyMethodBuilder.CreateProxiedMethod(interceptorField, method, typeBuilder);
            }

            // Make the proxy serializable
            AddSerializationSupport(baseType, baseInterfaces, typeBuilder, interceptorField, defaultConstructor);
            System.Type proxyType = typeBuilder.CreateType();

#if DEBUG_PROXY_OUTPUT
            assemblyBuilder.Save("generatedAssembly.dll");
#endif
            return(proxyType);
        }
コード例 #4
0
 private Type CreateUncachedProxyType(Type baseType, Type[] baseInterfaces)
 {
     AppDomain currentDomain = AppDomain.CurrentDomain;
       string name1 = string.Format("{0}Proxy", (object) baseType.Name);
       string assemblyName = string.Format("{0}Assembly", (object) name1);
       string name2 = string.Format("{0}Module", (object) name1);
       AssemblyName name3 = new AssemblyName(assemblyName);
       AssemblyBuilderAccess access = AssemblyBuilderAccess.Run | AssemblyBuilderAccess.Save;
       ModuleBuilder moduleBuilder = currentDomain.DefineDynamicAssembly(name3, access).DefineDynamicModule(name2, string.Format("{0}.mod", (object) name2), true);
       TypeAttributes attr = TypeAttributes.Public | TypeAttributes.AutoClass | TypeAttributes.BeforeFieldInit;
       HashSet<Type> source = new HashSet<Type>();
       HashSetExtensions.Merge<Type>(source, (IEnumerable<Type>) baseInterfaces);
       Type parent = baseType;
       if (baseType.IsInterface)
       {
     parent = typeof (ProxyDummy);
     source.Add(baseType);
       }
       foreach (Type currentType in Enumerable.ToArray<Type>((IEnumerable<Type>) source))
     HashSetExtensions.Merge<Type>(source, this.GetInterfaces(currentType));
       source.Add(typeof (ISerializable));
       TypeBuilder typeBuilder = moduleBuilder.DefineType(name1, attr, parent, Enumerable.ToArray<Type>((IEnumerable<Type>) source));
       ConstructorBuilder defaultConstructor = ProxyFactory.DefineConstructor(typeBuilder);
       ProxyImplementor proxyImplementor = new ProxyImplementor();
       proxyImplementor.ImplementProxy(typeBuilder);
       FieldInfo fieldInfo = (FieldInfo) proxyImplementor.InterceptorField;
       foreach (MethodInfo method in Enumerable.Where<MethodInfo>(this.GetProxiableMethods(baseType, (IEnumerable<Type>) source), (Func<MethodInfo, bool>) (method => method.DeclaringType != typeof (ISerializable))))
     this.ProxyMethodBuilder.CreateProxiedMethod(fieldInfo, method, typeBuilder);
       ProxyFactory.AddSerializationSupport(baseType, baseInterfaces, typeBuilder, fieldInfo, defaultConstructor);
       return typeBuilder.CreateType();
 }