public static Type GenerateType(ModuleScope scope, string proxyNamespace, Type sampleType) { var attributes = sampleType.GetCustomAttributes(typeof(AspectTemplateAttribute), true); if (attributes.Length < 1) { return(null); } // throw new ProxyGenerationException("Proxies must be decorated with an AspectTemplateAttribute"); var aspectAttribute = (AspectTemplateAttribute)attributes[0]; Type registrarGenericType = typeof(ProxyTemplateRegistrar <>); Type registrarType = registrarGenericType.MakeGenericType(aspectAttribute.TemplateType); var registrar = Activator.CreateInstance(registrarType); Activator.CreateInstance(aspectAttribute.TemplateType, registrar); var constructorContributor = new ConstructorContributor(); ITypeContributor templateContributorFactory = new TemplateTypeContributor(sampleType, innerType => new CastleContributors.ClassMembersCollector(innerType), (IProxyTemplateRegistrar)registrar, constructorContributor); // Create proxy generator var proxyGenerator = new ExtensibleClassProxyGenerator( scope, proxyNamespace, sampleType, templateContributorFactory, constructorContributor); // Generate type return(proxyGenerator.GenerateCode(new Type[] { }, new CastleProxy.ProxyGenerationOptions())); }
public static TClass GenerateType <TClass>(ModuleScope scope, params Type[] parts) { var sampleType = typeof(TClass); var constructorContributor = new ConstructorContributor(); ITypeContributor compositeContributor = new PartCompositeTypeContributor(constructorContributor, parts); // Create proxy generator var proxyGenerator = new ExtensibleClassProxyGenerator( scope, "PhillipScottGivens.CompositeProxies", sampleType, compositeContributor, constructorContributor); // Generate type Type proxiedType = proxyGenerator.GenerateCode(new Type[] { }, new CastleProxy.ProxyGenerationOptions()); return((TClass)Activator.CreateInstance(proxiedType)); }