pamidur / aspect-injector

AOP framework for .NET (c#, vb, etc)
Apache License 2.0
742 stars 111 forks source link

Is there a way we can prevent a method from being executed through aspects? #172

Closed himanshukodwani closed 1 year ago

himanshukodwani commented 2 years ago

What I need is an aspect that will check if the current user has permission to execute the method, if not, then I just want this aspect to prevent the method from being executed without throwing an exception and just returns some default value if required.

pamidur commented 2 years ago

Hi @himanshukodwani , you'd need something like

[Advice(Kind.Around)]
public object CheckAccess(...){
//check access here

return null; // if check fails
}

for more advanced scenarios you can take a look here https://github.com/pamidur/aspect-injector/blob/master/samples/UniversalWrapper/UniversalWrapper.cs

Please let me know if it works for you?