public void OnEnter(ParameterInterceptionArgs e) { if ((int)e.Value > 10) { Console.WriteLine($"Invalid parameter {e.Parameter.Name}"); } }
/// <summary> /// Called when a method has been invoked, and executes before the method body and method interceptors. /// </summary> /// <param name="e">The interception event arguments.</param> public void OnEnter(ParameterInterceptionArgs e) { var type = e.Parameter.ParameterType; if (e.Value != null && !type.IsValueType) { return; } e.Value = Resolve(type); }
/// <summary> /// Initializes the members which will have been and will be referenced. /// </summary> private void InitializeReferencedMembers() { // interfaces IInjectAfterInitializer = ResolveType("IInjectAfterInitializer"); IInjectBeforeInitializer = ResolveType("IInjectBeforeInitializer"); IInstanceAware = ResolveType("IInstanceAware"); IMemberAware = ResolveType("IMemberAware"); IMethodInterceptor = ResolveType("IMethodInterceptor"); IMethodReturnInterceptor = ResolveType("IMethodReturnInterceptor"); IParameterInterceptor = ResolveType("IParameterInterceptor"); IPropertyGetInterceptor = ResolveType("IPropertyGetInterceptor"); IPropertySetInterceptor = ResolveType("IPropertySetInterceptor"); IRequireInitialization = ResolveType("IRequireInitialization"); // classes CompilationImplementsAttribute = ResolveType("CompilationImplementsAttribute"); CompilationOptionsAttribute = ResolveType("CompilationOptionsAttribute"); Exception = ResolveType("Exception"); MethodBase = ResolveType("MethodBase"); MethodInfo = ResolveType("MethodInfo"); MethodInterceptionArgs = ResolveType("MethodInterceptionArgs"); MethodReturnInterceptionArgs = ResolveType("MethodReturnInterceptionArgs"); ObjectArray = new ArrayType(ResolveType("Object")); ParameterInfo = ResolveType("ParameterInfo"); ParameterInterceptionArgs = ResolveType("ParameterInterceptionArgs"); PropertyInfo = ResolveType("PropertyInfo"); PropertyInterceptionArgs = ResolveType("PropertyInterceptionArgs"); Type = ResolveType("Type"); TypeArray = new ArrayType(Type); // constructors CompilerGeneratedAttributeCtor = ResolveType("CompilerGeneratedAttribute").GetConstructor(); MethodInterceptionArgsCtor = MethodInterceptionArgs.GetConstructor(); MethodReturnInterceptionArgsCtor = MethodReturnInterceptionArgs.GetConstructor(); NonSerializedAttributeCtor = ResolveType("NonSerializedAttribute").GetConstructor(); ParameterInterceptionArgsCtor = ParameterInterceptionArgs.GetConstructor(); PropertyInterceptionArgsCtor = PropertyInterceptionArgs.GetConstructor(); // methods MethodBaseGetMethodFromHandle = ResolveType("MethodBase").GetMethod("GetMethodFromHandle", parameters: new[] { ResolveType("RuntimeMethodHandle") }); MethodBaseGetMethodFromHandleAndType = ResolveType("MethodBase").GetMethod("GetMethodFromHandle", parameters: new[] { ResolveType("RuntimeMethodHandle"), ResolveType("RuntimeTypeHandle") }); MethodBaseGetParameters = ResolveType("MethodBase").GetMethod("GetParameters"); MethodInterceptorOnEnter = IMethodInterceptor.GetMethod("OnEnter"); MethodInterceptorOnException = IMethodInterceptor.GetMethod("OnException"); MethodInterceptorOnExit = IMethodInterceptor.GetMethod("OnExit"); MethodReturnInterceptorOnReturn = IMethodReturnInterceptor.GetMethod("OnReturn"); ParameterInterceptorOnEnter = IParameterInterceptor.GetMethod("OnEnter"); PropertyGetInterceptorOnExit = IPropertyGetInterceptor.GetMethod("OnExit"); PropertyGetInterceptorOnGet = IPropertyGetInterceptor.GetMethod("OnGet"); PropertySetInterceptorOnExit = IPropertySetInterceptor.GetMethod("OnExit"); PropertySetInterceptorOnSet = IPropertySetInterceptor.GetMethod("OnSet"); RequireInitializationInitialize = IRequireInitialization.GetMethod("Initialize"); TypeGetProperty = Type.GetMethod("GetProperty", parameters: new[] { ResolveType("String"), ResolveType("BindingFlags") }); TypeGetTypeFromHandle = Type.GetMethod("GetTypeFromHandle", parameters: new[] { ResolveType("RuntimeTypeHandle") }); TypeMakeGenericType = Type.GetMethod("MakeGenericType"); // properties InstanceAwareInstanceSet = IInstanceAware.GetProperty("Instance").Resolve().SetMethod.Import(); MemberAwareMemberSet = IMemberAware.GetProperty("Member").Resolve().SetMethod.Import(); MethodInterceptionArgsCancelGet = MethodInterceptionArgs.GetProperty("Cancel").Resolve().GetMethod.Import(); MethodInterceptionArgsReturnGet = MethodInterceptionArgs.GetProperty("Return").Resolve().GetMethod.Import(); MethodInterceptionArgsReturnSet = MethodInterceptionArgs.GetProperty("Return").Resolve().SetMethod.Import(); MethodReturnInterceptionArgsValueGet = MethodReturnInterceptionArgs.GetProperty("Value").Resolve().GetMethod.Import(); MethodReturnInterceptionArgsValueSet = MethodReturnInterceptionArgs.GetProperty("Value").Resolve().SetMethod.Import(); ParameterInterceptionArgsValueGet = ParameterInterceptionArgs.GetProperty("Value").Resolve().GetMethod.Import(); PropertyInterceptionArgsIsDirtyGet = PropertyInterceptionArgs.GetProperty("IsDirty").Resolve().GetMethod.Import(); PropertyInterceptionArgsValueGet = PropertyInterceptionArgs.GetProperty("Value").Resolve().GetMethod.Import(); PropertyInterceptionArgsValueSet = PropertyInterceptionArgs.GetProperty("Value").Resolve().SetMethod.Import(); TypeTypeHandleGet = Type.GetProperty("TypeHandle").Resolve().GetMethod.Import(); }