-1

i want use custom attribute on my n-tier project. for

-Caching- -Logging -Validation -Exception

2 Answers2

1

First of all you should search for AOP. this is some kind of developing methodology

Aspect-Oriented Programming (AOP) is a programming paradigm that aims to increase modularity by allowing the separation of cross-cutting concerns. Wikipedia

It is not as simple as you think,in C# you have to use some 3rd party Library.

I suggest to start with MrAdvice, it will helps you.

msh
  • 15
  • 7
  • Thank you for your answer. Alright is this 3.rd party best way for the aop ? What is the best and simple alternative postsharp ? – Halil ŞAHİN May 04 '20 at 00:29
  • is this 3.rd party best way for the aop ? No and Yes. What is the best and simple alternative postsharp ? It depends, i think it is a good start, and it is free – msh May 04 '20 at 23:13
0

Writing your custom attributes would look a little something like this:

using System;

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method)]
public class LogAttribute : Attribute
{
    public bool _loggingEnabled;

    public LogAttribute(loggingEnabled)
    {
        _loggingEnabled = loggingEnabled;
    }
}

Usage example:

[Log(true)]
public class SampleClass
{
}

If you require further clarification if you share a little bit more about your intended usage of the attributes I'd be able to clarify a bit more. For instance, I'm not sure if you wanted your logging attribute to give you a means to explicitly state if that class/struct would be logged, but that's how I decided to demonstrate this. Also, note you can apply that to properties, and various other members via the AttributeUsage attribute on your attribute class declaration. I hope that helps.

Update:

Save method example:

[Log(true)]
public void Save(Company company)
{
   _context.Save();
}

Then you'll use the static method Attribute.GetCustomAttribute(MemberInfo element, Type attributeType) to retrieve information stored in that attribute. Since in this case, you'll likely want to use logging in many places you'd write another method (most likely static as well) somewhere else in your application that'd look something like this (see the link to the System.Reflection reference below for more information).

public static void Log(MemberInfo element)
{
     LogAttribute attribute = Attribute.GetCustomAttribute(element, typeof(LogAttribute);

     if (attribute._loggingEnabled)
     {
         // Create log file and add information from here.
     }
}

Attribute Reference

Reflection Reference

Hawkeye4040
  • 126
  • 10
  • Can not trigger this method. – Halil ŞAHİN May 03 '20 at 20:55
  • What exactly do you mean by your comment? I've read it over a few times and I still don't understand what you mean by not being able to trigger a method. My best guess is you're trying to write an attribute to trigger a custom action on a method that was not at all mentioned in the question but again that's my best guess. Is that what you're talking about? – Hawkeye4040 May 03 '20 at 21:36
  • If that is the case let me know and I can edit with an example for that as well. If that's not it please elaborate more and I should still be able to help. – Hawkeye4040 May 03 '20 at 21:37
  • Thank you very much for your answer. My mistake.. Sorry. I want to make custom attribute for the for the AOP process. manuel or 3. pary how can i . do it ? Example:I want to: Run my attribute before or after for the business method. Like this [Log()] public void Save(Company company) { return _context.Save(); } – Halil ŞAHİN May 04 '20 at 00:38
  • Oh good, I'm glad that helped. I'll add what I think you're asking for with the Save method there to my answer. It should only take a few minutes. – Hawkeye4040 May 04 '20 at 00:46