0

I would like to perform a C# interception based with Attribute by example:

[CheckSomething]
public void Test()
{
}

And I want to cancel the execution of the method by example, i've alreay tried by creating an Attribute, an other problem is that i don't arrive to Console.WriteLine() in the constructor of the Attribute, does anyone know why ?

Thank you

  • can you provide the code that you use for attribute ? – Vishnu Feb 15 '19 at 19:14
  • Yes I just use this: internal class CheckSomethingAttribute : Attribute – Clint.Network Feb 15 '19 at 19:34
  • Please read this: https://stackoverflow.com/questions/4879521/how-to-create-a-custom-attribute-in-c-sharp. Attributes themselves are not "active code" so to speak, nor do they change or otherwise influence the behavior of code in a method. They just represent some meta-data associated with some type, method, other type member, etc... –  Feb 15 '19 at 19:36
  • okey I understand, but what is the simplest solution to make an interception with an Attribute/Decorator. – Clint.Network Feb 15 '19 at 19:38
  • Define "interception". What exactly do you mean with this? –  Feb 15 '19 at 19:39
  • No problem, look this https://doc.postsharp.net/method-interception – Clint.Network Feb 15 '19 at 19:41
  • 1
    I suggest to use a library that offers the functionality you are looking for. There is no "simple" solution to this. (Like PostSharp, you would need to write code that inspects types and their members for particular attributes, and you would need to write additional code that takes control and does method invocations and alike based on the presence of those attributes. Postsharp goes even further than that, transforming the built program/assembly by injecting Postsharp specific code here and there...) –  Feb 15 '19 at 19:51
  • yeah i see, so do you know a postsharp alternative for net core, because postsharp does not work for me :/ – Clint.Network Feb 15 '19 at 20:43
  • @Clint.Network You can try to create your own interceptor. See my [answer](https://stackoverflow.com/a/48140906/4685428) – Aleks Andreev Feb 16 '19 at 09:50
  • ha yes thanks @AleksAndreev but why a new project is needed ? – Clint.Network Feb 17 '19 at 03:05

0 Answers0