What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For checks that must run before a method body, derive an aspect from OnMethodBoundaryAspect and validate the invocation arguments in OnEntry(MethodExecutionArgs args). For validation attached to an individual parameter location, use PostSharp’s ILocationValidationAspect<T>. Choose based on whether the rule concerns the method call as a whole or a particular parameter value.

Choose the right validation point

Approach Attach to What it validates Important detail
OnMethodBoundaryAspect A method invocation The call’s arguments, available from the execution context OnEntry runs before the target method body. PostSharp method-boundary aspects and the OnEntry API.
ILocationValidationAspect<T> An individual location, including a parameter A typed value assigned to that location The interface’s generic type must exactly match the parameter type; it does not automatically convert compatible numeric types. PostSharp location validation API.

Use method-entry validation when a condition depends on multiple arguments or naturally belongs at the method boundary. Use location validation when the rule belongs to one parameter’s value. These are different extension points, not interchangeable ways to express the same validation.

Validate invocation arguments at method entry

PostSharp’s OnMethodBoundaryAspect provides advice around a method’s execution. Its OnEntry(MethodExecutionArgs args) hook runs before the method body, and args.Arguments exposes the invocation arguments. That makes OnEntry the relevant point for rejecting invalid input before user code runs.

A typical implementation shape is to derive an aspect from OnMethodBoundaryAspect and override OnEntry. Inspect the relevant values in args.Arguments, then throw an exception if the call violates the application’s rule. The exception type, message, and indexing logic depend on the contract you want to enforce; PostSharp’s cited API material establishes the hook and argument access, but does not prescribe a complete validation rule or exception policy.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

The method-boundary tutorial uses [PSerializable] on the aspect. Follow that pattern when using the tutorial’s approach, and check the documentation for the PostSharp release and project configuration you actually use. PostSharp method-boundary tutorial.

Attach validation to an individual parameter

Implement ILocationValidationAspect<T> when the rule should be associated with a parameter location. Its ValidateValue method validates a value assigned to the location; when validation fails, the interface contract calls for throwing the exception returned by that method. PostSharp’s API documentation lists parameters among the supported locations.

Choose T to match the parameter’s actual type exactly. For example, a validator defined for one numeric type does not automatically apply to another merely because C# permits a conversion between those types. If you need to support multiple parameter types, account for each type explicitly rather than relying on implicit conversion by the location-validation interface.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Reject unsupported aspect targets during the build

Runtime validation checks caller input; compile-time validation checks whether an aspect is being applied to a target it supports. PostSharp recommends implementing CompileTimeValidate for applicability checks. If the target is invalid, report an error or throw InvalidAnnotationException. A bare false return without an error or exception can cause the aspect to be silently ignored. PostSharp compile-time validation guidance.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For a custom ParameterValueConstraint, the documented ValidateTargetParameter(ParameterInfo) hook is intended to determine whether the constraint is appropriate for a reflected parameter. An override should call the base implementation. ParameterValueConstraint API.

Set up and verify the implementation

  1. Add PostSharp to the project. Use the package reference and integration appropriate to your project. The PostSharp tutorial’s method-boundary pattern marks the aspect with [PSerializable]. See the tutorial.
  2. Select the extension point. Use OnMethodBoundaryAspect for checks on the invocation’s argument set, or ILocationValidationAspect<T> for a rule attached to an individual parameter location.
  3. Implement the check at the documented hook. Inspect args.Arguments in OnEntry, or implement the typed location validator through ValidateValue.
  4. Constrain where the aspect can be applied. Implement CompileTimeValidate or, for a custom ParameterValueConstraint, use ValidateTargetParameter(ParameterInfo) as appropriate. Report unsupported use clearly rather than returning false silently.
  5. Check your actual toolchain. Confirm the installed PostSharp release and target .NET framework against the package and documentation used by the project. The cited API and tutorial pages do not establish compatibility for an unspecified project configuration.

What to test in your project

  • Confirm a valid call reaches the target method and an invalid call fails at the intended point.
  • For method-entry checks, verify that validation happens before any code in the target method body.
  • For location validation, verify that the aspect is applied to the intended parameter type and that a different numeric type is not assumed to match automatically.
  • Apply the aspect to an unsupported target deliberately and confirm the build reports the problem instead of silently ignoring the annotation.
  • Build and run these checks with the PostSharp package and .NET target used by your application; compatibility cannot be inferred without those version details.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.