Skip to content

Navigation Menu

Sign in
Sign up
Peter Bons edited this page Nov 21, 2019 · 3 revisions

Using advices

As an attribute

Usually, you will just set the attributes to target method or property

[ADummyPropertyAdvice]
public int AnAdvisedProperty { get; set; }
[ADummyMethodAdvice]
public void MethodTest()
{
}

to target a type set the attribute at type level

[ADummyTypeAdvice]
public class SomeClass
{
 ...
}

to target all types in an assembly set the attribute at assembly level

using System;
[assembly: ADummyAdvice]
namespace SomeNameSpace
{
 public class SomeClass
 {
 ...
 }
}

Advise an external interface

You can also intercept all calls to a given interface, even if it is external to your code. There is a special extension method named Handle<TInterface>() which applies to advices.

public class ADummyAdvice: Attribute, IMethodAdvice
{
 public void Advise(MethodAdviceContext context)
 {
 // DO what you want, but...
 // DON'T call context.Proceed(), because there is no implementation
 }
}
public void DoSomethingWithIt()
{
 var advice = new ADummyAdvice();
 ISomeInterface advisedInterface = a.Handle<ISomeInterface>(); 
 // all calls to advisedInterface will go through the ADummyAdvice, and you'd better intercept them.
}

Clone this wiki locally

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /