0

Say we have a property like this:

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Method, AllowMultiple = false,  Inherited = true)]
public class LogScopeAttribute : Attribute
{ 
    public string Level { get; private set; }

    public LogScopeAttribute(string level)
    {
        Level   = level;
    }
}

Used in a context like this:

public class Cat
{
    [Log.Scope("Important")]
    public void Walk()
    {
    }

    [Log.Scope("Trivial")]
    public void Sit()
    {
    }
}

How can I use the property Level in my Before and After methods? It seems like you can only use the fluent interface, but because of that I can't refer to the LogScope attribute.

public class LoggingAmender : Amendment<object, object>
{
    public LoggingAmender()
    {
        Methods.Where( m => [...] )
            .Before(LogScope.LogMethodBefore); // how can I refer to 'Level' of LogScope here?
    }
}
Dirk Boer
  • 8,522
  • 13
  • 63
  • 111

1 Answers1

0

You can access method.Attributes where all attribute instances of that method are stored.

Maybe its easier for you NOT to use Lambdas. i could not prove that code, i am still learning Afterthought, but here is what i have got:

TDeclaringAttribute in your case is LogScopeAttribute .

   /// <summary>
  /// Pointcut definition for Amendments that are targeted to Methods that have a declaring Attribute defined.
/// </summary>
abstract class MethodAmendment<TType, TDeclaringAttribute> : Amendment<TType, TType>
    where TDeclaringAttribute : Attribute
{
    public MethodAmendment()
    {
        foreach (Method method in Methods)
        {
            //todo ?? look for base class versions of method ? should be covered by afterthought itself ?
            TDeclaringAttribute attribute = method.Attributes.OfType<TDeclaringAttribute>().FirstOrDefault();
            if (attribute != null)
            {
                ApplyMethodChanges(method, attribute);
            }
        }
    }

    protected abstract void ApplyMethodChanges(Method method, TDeclaringAttribute attribute);
}
Thomas Haller
  • 199
  • 1
  • 11