private IInterceptor[] SelectMethodInterceptors(IInterceptorSelector selector, IInterceptor[] methodInterceptors, Type targetType) { return methodInterceptors ?? selector.SelectInterceptors(targetType, Method, interceptors) ?? new IInterceptor[0]; }
private ProxyGenerationOptions(SerializationInfo info, StreamingContext context) { hook = (IProxyGenerationHook)info.GetValue("hook", typeof(IProxyGenerationHook)); selector = (IInterceptorSelector)info.GetValue("selector", typeof(IInterceptorSelector)); mixins = (List<object>)info.GetValue("mixins", typeof(List<object>)); baseTypeForInterfaceProxy = Type.GetType(info.GetString("baseTypeForInterfaceProxy.AssemblyQualifiedName")); }
static Freezable() { InterceptorSelector = new FreezableInterceptorSelector(); Generator = new ProxyGenerator(); FreezableProxyGenerationHook = new FreezableProxyGenerationHook(); }
public AspectBuilderProvider(IInterceptorSelector interceptorSelector) { if (interceptorSelector == null) { throw new ArgumentNullException(nameof(interceptorSelector)); } this.interceptorSelector = interceptorSelector; }
static Core() { _ProxyGenerator = new ProxyGenerator(); _Selector = new ChangeTrackingInterceptorSelector(); _Options = new ConcurrentDictionary <Type, ProxyGenerationOptions>(); _SetValueMethodInfo = typeof(FieldInfo).GetMethod(nameof(FieldInfo.SetValue), new[] { typeof(object), typeof(object) }); _FieldCopiers = new ConcurrentDictionary <Type, Action <object, object> >(); }
private IInterceptor[] SelectMethodInterceptors(IInterceptorSelector selector, IInterceptor[] methodInterceptors, Type targetType) { return(methodInterceptors ?? selector.SelectInterceptors(targetType, Method, interceptors) ?? new IInterceptor[0]); }
private ProxyGenerationOptions(SerializationInfo info, StreamingContext context) { hook = (IProxyGenerationHook) info.GetValue ("hook", typeof (IProxyGenerationHook)); selector = (IInterceptorSelector) info.GetValue ("selector", typeof (IInterceptorSelector)); mixins = (ArrayList) info.GetValue ("mixins", typeof (ArrayList)); baseTypeForInterfaceProxy = Type.GetType (info.GetString ("baseTypeForInterfaceProxy.AssemblyQualifiedName")); useSingleInterfaceProxy = info.GetBoolean ("useSingleInterfaceProxy"); useSelector = info.GetBoolean ("useSelector"); }
public ChangeTrackingFactory(bool makeComplexPropertiesTrackable, bool makeCollectionPropertiesTrackable) { MakeComplexPropertiesTrackable = makeComplexPropertiesTrackable; MakeCollectionPropertiesTrackable = makeCollectionPropertiesTrackable; _ProxyGenerator = new ProxyGenerator(); _Selector = new ChangeTrackingInterceptorSelector(); _Options = new ConcurrentDictionary <Type, ProxyGenerationOptions>(); _SetValueMethodInfo = typeof(FieldInfo).GetMethod(nameof(FieldInfo.SetValue), new[] { typeof(object), typeof(object) }); _FieldCopiers = new ConcurrentDictionary <Type, Action <object, object> >(); }
protected CompositionInvocation( object target, object proxy, IInterceptor[] interceptors, MethodInfo proxiedMethod, object[] arguments, IInterceptorSelector selector, ref IInterceptor[] methodInterceptors) : base(proxy, GetTargetType(target), interceptors, proxiedMethod, arguments, selector, ref methodInterceptors) { this.target = target; }
protected InheritanceInvocation( Type targetType, object proxy, IInterceptor[] interceptors, MethodInfo proxiedMethod, object[] arguments, IInterceptorSelector selector, ref IInterceptor[] methodInterceptors) : base(proxy, targetType, interceptors, proxiedMethod, arguments, selector, ref methodInterceptors) { this.targetType = targetType; }
protected AbstractInvocation( object proxy, Type targetType, IInterceptor[] interceptors, MethodInfo proxiedMethod, object[] arguments, IInterceptorSelector selector, ref IInterceptor[] methodInterceptors) : this(proxy, interceptors, proxiedMethod, arguments) { methodInterceptors = SelectMethodInterceptors(selector, methodInterceptors, targetType); this.interceptors = methodInterceptors; }
protected AbstractInvocation( object target, object proxy, IInterceptor[] interceptors, Type targetType, MethodInfo targetMethod, object[] arguments, IInterceptorSelector selector, ref IInterceptor[] methodInterceptors) { this.proxy = proxy; this.target = target; this.interceptors = interceptors; this.targetType = targetType; this.targetMethod = targetMethod; this.arguments = arguments; methodInterceptors = SelectMethodInterceptors(selector, methodInterceptors); this.interceptors = methodInterceptors; }
protected AbstractInvocation( object target, object proxy, IInterceptor[] interceptors, Type targetType, MethodInfo targetMethod, MethodInfo interfMethod, object[] arguments, IInterceptorSelector selector, ref IInterceptor[] methodInterceptors) : this(target, proxy, interceptors, targetType, targetMethod, arguments) { this.interfMethod = interfMethod; methodInterceptors = SelectMethodInterceptors(selector, methodInterceptors); this.interceptors = methodInterceptors; }
protected object[] GetArguments(ProxyTypeDefinition typeDefinition, IInterceptor[] interceptors, object target, Type proxyType) { List <object> args = new List <object>(); // interceptors args.Add(interceptors); // target if (target != null) { args.Add(target); } // interceptorSelector ProxyGeneratorOptions options = typeDefinition.Options; IInterceptorSelector interceptorSelector = options?.InterceptorSelector; if (interceptorSelector != null) { args.Add(interceptorSelector); } // mixins if (options != null && options.MixinInstances.Count > 0) { args.AddRange(options.MixinInstances); } // base ctor dependencies ConstructorInfo constructor = proxyType.GetConstructors()[0]; var parameters = constructor.GetParameters(); int length = parameters.Length; if (length > args.Count) { for (int i = args.Count; i < length; i++) { args.Add(ConstructorInjectionResolver.Resolve(parameters[i])); } } return(args.ToArray()); }
Expression <Action <object> > MakeInitExpression(Type proxyType, IInterceptorSelector selector, MixinData mixins) { var fields = proxyType.GetFields().ToDictionary(f => f.Name); var parameter = Expression.Parameter(typeof(object)); var variable = Expression.Variable(proxyType); var expressions = new List <Expression>(); expressions.Add(Expression.Assign(variable, Expression.Convert(parameter, proxyType))); expressions.Add(Expression.Assign( Expression.MakeMemberAccess(variable, fields["__selector"]), Expression.Constant(selector) )); expressions.AddRange(mixins.MixinInterfaces.Zip(mixins.Mixins, (t, o) => Expression.Assign( Expression.MakeMemberAccess(variable, fields["__mixin_" + t.FullName.Replace(".", "_")]), Expression.New(o.GetType()) ) )); return(Expression.Lambda <Action <object> >(Expression.Block(new[] { variable }, expressions), parameter)); }
public TProxyType CreateInterfaceProxy <TProxyType>( Type interfaceToProxy, TProxyType instance, Type[] typesToProxy, IInterceptorSelector selector, IInterceptor[] interceptors) { var options = new ProxyGenerationOptions(); if (selector != null) { options.Selector = selector; } return((TProxyType)Generator.CreateInterfaceProxyWithTarget( interfaceToProxy, typesToProxy = typesToProxy != null ? typesToProxy.Where(it => it != ProxyTargetType).ToArray() : null, instance, options, interceptors)); }
public object CreateClassProxy( Type classToProxy, Type[] typesToProxy, object[] arguments, IInterceptorSelector selector, IInterceptor[] interceptors) { var options = new ProxyGenerationOptions(); if (selector != null) { options.Selector = selector; } return(Generator.CreateClassProxy( classToProxy, typesToProxy = typesToProxy != null ? typesToProxy.Where(it => it != ProxyTargetType).ToArray() : null, options, arguments, interceptors)); }
/// <summary> /// Applies the synchronization support to the model. /// </summary> /// <param name = "model">The model.</param> /// <param name = "kernel">The kernel.</param> private void ApplySynchronization(ComponentModel model, IKernel kernel) { var options = model.ObtainProxyOptions(); model.Interceptors.Add(new InterceptorReference(typeof(SynchronizeInterceptor))); var metaInfo = metaStore.GetMetaFor(model.Implementation); if (metaInfo != null) { IInterceptorSelector userSelector = null; if (options.Selector != null) { userSelector = options.Selector.Resolve(kernel, CreationContext.CreateEmpty()); } options.Selector = new InstanceReference <IInterceptorSelector>(new SynchronizeInterceptorSelector(metaInfo, userSelector)); foreach (var reference in metaInfo.GetUniqueSynchContextReferences()) { reference.Attach(model); } } }
/// <summary> /// Sets the interceptor selector for this component. /// </summary> /// <param name = "selector"></param> /// <returns></returns> public ComponentRegistration <TService> SelectInterceptorsWith(IInterceptorSelector selector) { return(SelectInterceptorsWith(s => s.Instance(selector))); }
private IInterceptor[] SelectMethodInterceptors(IInterceptorSelector selector, IInterceptor[] methodInterceptors, Type targetType) { return (methodInterceptors ?? (selector.SelectInterceptors(targetType, this.Method, this.interceptors) ?? new IInterceptor[0])); }
public WcfInterceptorSelector(Type proxiedType, IInterceptorSelector userProvidedSelector) { this.proxiedType = proxiedType; this.userProvidedSelector = userProvidedSelector; }
private IInterceptor[] SelectMethodInterceptors(IInterceptorSelector selector, IInterceptor[] methodInterceptors) { if (methodInterceptors == null) { //NOTE: perhaps instead of passing this.Method we should call this.GetConcreteMethod() methodInterceptors = selector.SelectInterceptors(targetType, Method, interceptors) ?? new IInterceptor[0]; } return methodInterceptors; }
public CoreInterceptorSelector(IInterceptorSelector parentInterceptorSelector, IPointcut pointcut) { ParentInterceptorSelector = parentInterceptorSelector; Pointcut = pointcut; }
/// <summary> /// Constructs the selector with the existing selector. /// </summary> /// <param name = "metaInfo">The sync metadata.</param> /// <param name = "existingSelector">The existing selector.</param> public SynchronizeInterceptorSelector(SynchronizeMetaInfo metaInfo, IInterceptorSelector existingSelector) { this.metaInfo = metaInfo; this.existingSelector = existingSelector; }
Tuple <string, Expression <Func <object[], object> > > MakeCreateExpression(Type proxyType, IInterceptorSelector selector, MixinData mixins, ConstructorInfo ctor, ParameterInfo[] ctorArgs) { var key = new StringBuilder(); var ctorArgsParameter = Expression.Parameter(typeof(object[])); var arguments = mixins.Mixins.Select(m => Expression.New(m.GetType())).Cast <Expression>().ToList(); arguments.Add(Expression.Convert(Expression.Constant(null), typeof(IInterceptor[]))); arguments.Add(Expression.Constant(selector)); for (int i = arguments.Count, j = 0; i < ctorArgs.Length; i++, j++) { key.Append(ctorArgs[i].ParameterType.ToString()); var argExpr = Expression.ArrayIndex(ctorArgsParameter, Expression.Constant(j)); arguments.Add(Expression.ConvertChecked(argExpr, ctorArgs[i].ParameterType)); } var body = Expression.New(ctor, arguments); var method = Expression.Lambda <Func <object[], object> >(body, ctorArgsParameter); return(Tuple.Create(key.ToString(), method)); }
protected WCFInvocation(object target, object proxy, IInterceptor[] interceptors, MethodInfo proxiedMethod, object[] arguments, IInterceptorSelector selector, ref IInterceptor[] methodInterceptors) : base(target, proxy, interceptors, proxiedMethod, arguments, selector, ref methodInterceptors) { }
public virtual MethodBuilder CreateMethod(ProxyScope proxyScope, MethodDefinition methodDefinition, MemberInfo member, FieldBuilder memberField, FieldBuilder callerMethodField) { MethodInfo method = methodDefinition.Method; // callback method MethodBuilder callbackMethodBuilder = CreateCallbackMethod(proxyScope, methodDefinition.CallbackMethodDefinition); // invocation type Type invocationType = GetOrCreateInvocationType(proxyScope, methodDefinition, callbackMethodBuilder); // method MethodBuilder methodBuilder = proxyScope.DefineMethod(methodDefinition.Name, methodDefinition.MethodAttributes); methodBuilder.SetReturnType(methodDefinition.ReturnType); // parameters methodBuilder.DefineGenericParameters(methodDefinition.GenericArguments); if (methodDefinition.ParameterDefinitions.Length > 0) { DefineParameters(methodBuilder, methodDefinition); } // attributes DefineAttributes(methodBuilder, methodDefinition); // method body var il = methodBuilder.GetILGenerator(); // locals IInterceptorSelector interceptorSelector = proxyScope.TypeDefinition.Options?.InterceptorSelector; FieldBuilder interceptorSelectorField = null; FieldBuilder interceptorMethodField = null; if (interceptorSelector != null) { interceptorSelectorField = FindConstructorField(proxyScope, ProxyScope.InterceptorSelectorFieldName); interceptorMethodField = proxyScope.DefineField(methodDefinition.InterceptorSelectorFieldName, typeof(IInterceptor[]), FieldAttributes.Private); } var returnType = methodDefinition.ReturnType; Type targetType = methodDefinition.TypeDefinition.TargetType; Type localBuilderType = targetType != null ? targetType : typeof(object); LocalBuilder targetLocalBuilder = il.DeclareLocal(localBuilderType); // target LocalBuilder interceptorsLocalBuilder = il.DeclareLocal(typeof(IInterceptor[])); // interceptors LocalBuilder memberLocalBuilder = il.DeclareLocal(typeof(MemberInfo)); // MemberInfo LocalBuilder callerMethodLocalBuilder = il.DeclareLocal(typeof(MethodInfo)); // proxy method LocalBuilder proxyLocalBuilder = il.DeclareLocal(typeof(object)); // proxy LocalBuilder parametersLocalBuilder = il.DeclareLocal(typeof(object[])); // parameters LocalBuilder invocationLocalBuilder = il.DeclareLocal(invocationType); // invocation LocalBuilder returnValueLocalBuilder = null; if (returnType != typeof(void)) { returnValueLocalBuilder = il.DeclareLocal(returnType); } FieldBuilder interceptorsField = proxyScope.ConstructorFields[0]; if (targetType != null) { FieldBuilder targetField = FindConstructorField(proxyScope, methodDefinition.TypeDefinition.TargetFieldName); EmitHelper.StoreFieldToLocal(il, targetField, targetLocalBuilder); } EmitHelper.StoreFieldToLocal(il, memberField, memberLocalBuilder); EmitHelper.StoreFieldToLocal(il, callerMethodField, callerMethodLocalBuilder); EmitHelper.StoreThisToLocal(il, proxyLocalBuilder); if (interceptorSelector != null) { LocalBuilder isNullLocal = il.DeclareLocal(typeof(bool)); Label isNullLabel = il.DefineLabel(); il.Emit(OpCodes.Ldarg_0); il.Emit(OpCodes.Ldfld, interceptorMethodField); il.Emit(OpCodes.Ldnull); il.Emit(OpCodes.Ceq); il.Emit(OpCodes.Stloc, isNullLocal); il.Emit(OpCodes.Ldloc, isNullLocal); il.Emit(OpCodes.Brfalse_S, isNullLabel); il.Emit(OpCodes.Ldarg_0); il.Emit(OpCodes.Ldarg_0); il.Emit(OpCodes.Ldfld, interceptorSelectorField); il.Emit(OpCodes.Ldtoken, member.DeclaringType); il.Emit(OpCodes.Call, Methods.GetTypeFromHandle); il.Emit(OpCodes.Ldloc, callerMethodLocalBuilder); il.Emit(OpCodes.Ldarg_0); il.Emit(OpCodes.Ldfld, interceptorsField); il.Emit(OpCodes.Callvirt, Methods.SelectInterceptorsMethod); il.Emit(OpCodes.Stfld, interceptorMethodField); il.MarkLabel(isNullLabel); EmitHelper.StoreFieldToLocal(il, interceptorMethodField, interceptorsLocalBuilder); } else { EmitHelper.StoreFieldToLocal(il, interceptorsField, interceptorsLocalBuilder); } StoreArgsToArray(il, methodDefinition.ParameterDefinitions, parametersLocalBuilder); EmitHelper.CreateInvocation(il, invocationType, targetLocalBuilder, interceptorsLocalBuilder, memberLocalBuilder, callerMethodLocalBuilder, proxyLocalBuilder, parametersLocalBuilder, invocationLocalBuilder); EmitHelper.CallProceed(il, invocationLocalBuilder); // set ref parameters values after Proceed called SetByRefArgs(il, methodDefinition.ParameterDefinitions, invocationLocalBuilder); EmitReturnValue(il, method, invocationLocalBuilder, returnValueLocalBuilder); if (method.DeclaringType.IsInterface) { proxyScope.DefineMethodOverride(methodBuilder, method); } return(methodBuilder); }
public InterceptorGroup <S> SelectedWith(IInterceptorSelector selector) { Registration.SelectInterceptorsWith(selector); return(this); }