public static IImmutableSet<Type> AggregateProvidableTypes(this IEnumerable<ITypeProvider> providers)
        {
            IImmutableSet<Type> currentTypes = new HashSet<Type>().ToImmutableHashSet();

            if (providers == null)
                return currentTypes;

            foreach (var provider in providers)
            {
                currentTypes = currentTypes.Merge(provider.ProvidableTypes);
            }

            return currentTypes;
        }
Example #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;
		}
        private System.Type CreateUncachedProxyType(System.Type baseType, System.Type[] baseInterfaces)
        {
            var currentDomain = AppDomain.CurrentDomain;
            var typeName = string.Format("{0}Proxy", baseType.Name);
            var assemblyName = string.Format("{0}Assembly", typeName);
            var moduleName = string.Format("{0}Module", typeName);
            var name = new AssemblyName(assemblyName);
            //rippo: the [*]commented lines allow the serialize of the assembly containing the proxy
            //you can then run PEVERIFY to check the IL for type safety

            //[*]const AssemblyBuilderAccess access = AssemblyBuilderAccess.RunAndSave;

            const AssemblyBuilderAccess access = AssemblyBuilderAccess.Run;
            var assemblyBuilder = currentDomain.DefineDynamicAssembly(name, access);
            
            //[*]var moduleBuilder = assemblyBuilder.DefineDynamicModule("generatedAssembly.dll", "generatedAssembly.dll", true);
            
            var moduleBuilder = assemblyBuilder.DefineDynamicModule(moduleName);
            const TypeAttributes typeAttributes =
                TypeAttributes.AutoClass |
                TypeAttributes.Class |
                TypeAttributes.Public |
                TypeAttributes.BeforeFieldInit;
            var interfaces = new HashSet<System.Type>();
            interfaces.Merge(baseInterfaces);
            var parentType = baseType;
            if (baseType.IsInterface)
            {
                parentType = typeof (ProxyDummy);
                interfaces.Add(baseType);
            }
            var computedInterfaces = interfaces.ToArray();
            foreach (var interfaceType in computedInterfaces)
            {
                interfaces.Merge(GetInterfaces(interfaceType));
            }
            interfaces.Add(typeof (ISerializable));
            var typeBuilder = moduleBuilder.DefineType(typeName, typeAttributes, parentType, interfaces.ToArray());
            var implementor = new ProxyImplementor();
            implementor.ImplementProxy(typeBuilder);
            var interceptorField = implementor.InterceptorField;
            AddSerializationSupport(typeBuilder);
            var constructors = baseType.GetConstructors();
            foreach (var constructorInfo in constructors)
            {
                var constructorBuilder = DefineConstructor(typeBuilder, constructorInfo);
                DefineSerializationConstructor(typeBuilder, interceptorField, constructorBuilder, constructorInfo);	
            }
            ImplementGetObjectData(baseType, baseInterfaces, typeBuilder, interceptorField);
            foreach (var method in GetProxiableMethods(baseType, interfaces).Where(method => method.DeclaringType != typeof(ISerializable)))
            {
                ProxyMethodBuilder.CreateProxiedMethod(interceptorField, method, typeBuilder);
            }
            var proxyType = typeBuilder.CreateType();
            
            //[*]assemblyBuilder.Save("generatedAssembly.dll");
            
            return proxyType;
        }