vendredi 25 juin 2021

How to get Method-Meta information within a Method Attribute?

How to get Method-Meta information within a Method Attribute ?

I wont to avoid having to write the Event-Name both in the attribute and in the parameters of the method.

For the name of one property, the nameof(Type) can be acceptable, but for other cross-cutting-concerns, its simple not enough.

There are a lot of cross-cutting-concerns that need this - this is just one example:

 // From
        [SubscribeToEvent(nameof(SomeEventType))]
        public async Task SomeEventTypeEndpoint(SomeEventType someEvent)
        { (...) }
 // To
        [SubscribeToEvent]
        public async Task SomeEventTypeEndpoint(SomeEventType someEvent)
        { (...) }
// From
    [AttributeUsage(AttributeTargets.Method)]
    public class SubscribeToEventAttribute : TopicAttribute
    {
        public SubscribeToEventAttribute(string eventName) 
        {
           // Do: log name, analytics, ect...
        }
    }
// To
    [AttributeUsage(AttributeTargets.Method)]
    public class SubscribeToEventAttribute : TopicAttribute
    {
        public SubscribeToEventAttribute() 
        {
           // Some reflection on what this attribute is applied to ?
           // Getting something like System.Reflection.ParameterInfo
           // Do: log name, analytics, ect...
        }
    }

Some people





Aucun commentaire:

Enregistrer un commentaire