/// <summary> /// Creates a subclass proxy. /// This is primary used by .NET 1.x users or where you need to create proxies of dynamic types in .NET 2.0. /// </summary> /// <param name="state"></param> /// <param name="type">Type to proxify</param> /// <param name="args">Object array of boxed parameter values</param> /// <returns>The proxy instance</returns> /// <example> /// <code lang="CS"> /// Foo myFoo = (Foo)engine.CreateProxyWithState(typeof(Foo),"I can be used in an ctor interceptor"); /// </code> /// </example> public object CreateProxyWithState(object state, Type type, params object[] args) { if (args == null) { args = new object[] { null } } ; bool useCtorState = true; #if NET2 if (typeof(ServicedComponent).IsAssignableFrom(type)) { if (args.Length > 0) { throw new NotSupportedException("Types inheriting ServicedComponent may not have ctor args"); } if (state != null) { throw new NotSupportedException("Types inheriting ServicedComponent can not use state args"); } useCtorState = false; } #endif LogMessage message = new LogMessage("Creating context bound wrapper for type {0}", type.FullName); LogManager.Info(this, message); ProxyTypeInfo typeInfo = CreateProxyTypeInfo(type, useCtorState); Type proxyType = typeInfo.Type; object[] proxyArgs; if (typeInfo.IsProxied && useCtorState) { proxyArgs = AddStateToCtorParams(state, args); } else //base or non proxied type { proxyArgs = args; } object proxyObject = Activator.CreateInstance(proxyType, proxyArgs); return(proxyObject); }
private ProxyTypeInfo CreateProxyTypeInfo(Type type, bool useCtorState) { bool wasProxied = false; bool wasExtended = false; ProxyTypeInfo typeInfo = null; lock (proxyLookup.SyncRoot) { Type proxyType = null; //incase a proxy for this type does not exist , generate it if (proxyLookup[type] == null) { Type extendedType = type; IList typeAspects = AspectMatcher.MatchAspectsForType(type, Configuration.Aspects); IList typeMixins = GetMixinsForType(type, typeAspects); typeMixins.Add(typeof(AopProxyMixin)); #if NET2 if (SerializerIsAvailable()) { AddSerializerMixin(typeMixins); } #endif foreach (object aspect in typeAspects) { if (aspect is GenericAspectBase) { GenericAspectBase genericAspect = (GenericAspectBase)aspect; foreach (TypeExtender typeExtender in genericAspect.TypeExtenders) { wasExtended = true; extendedType = typeExtender.Extend(extendedType); } } } proxyType = SubclassProxyFactory.CreateProxyType(extendedType, typeAspects, typeMixins, this, useCtorState); if (proxyType == null) { throw new NullReferenceException( string.Format("Could not generate proxy for type '{0}'", type.FullName)); } if (proxyType != extendedType) { wasProxied = true; } typeInfo = new ProxyTypeInfo(); typeInfo.Type = proxyType; typeInfo.IsExtended = wasExtended; typeInfo.IsProxied = wasProxied; proxyLookup[type] = typeInfo; LogMessage message = new LogMessage("Emitting new proxy type for type {0}", type.FullName); LogManager.Info(this, message); } else { typeInfo = proxyLookup[type] as ProxyTypeInfo; //fetch the proxy type from the lookup proxyType = typeInfo.Type; LogMessage message = new LogMessage("Fetching proxy type from cache for type {0}", type.FullName); LogManager.Info(this, message); } return(typeInfo); } }