Note that there are some explanatory texts on larger screens.

plurals
  1. POIntercepting method calls in C# using Proxies
    primarykey
    data
    text
    <p>What I'm trying to do is to be able to intercept calls to an object's methods and properties for cross-cutting concerns. I'm using proxy-based AOP using <code>ContextBoundObject</code>.</p> <p>However this doesn't work for recursive method calls, The first call against the target will be intercepted by the proxy and successfully invoked, allowing me to do cross-cut here. However subsequent method calls from within the first method will stay within the target class and are <strong>not intercepted</strong> by the proxy as if no marshaling occurs!</p> <p>Is there any way I can make it work? (I'm trying to avoid third-party libraries like PostSharp, Unity or Spring.Net)</p> <pre><code>class Program { static void Main(string[] args) { var t = new SimpleObject(); t.TestMethod1(); } } [Intercept] class SimpleObject : ContextBoundObject { public string TestMethod1() { return TestMethod2(); } public string TestMethod2() { return "test"; } } [AttributeUsage(AttributeTargets.Class)] public class InterceptAttribute : ContextAttribute, IContributeObjectSink { public InterceptAttribute() : base("Intercept") { } public override bool IsContextOK(Context ctx, IConstructionCallMessage ctorMsg) { return false; } public IMessageSink GetObjectSink(MarshalByRefObject obj, IMessageSink nextSink) { return new InterceptSink(nextSink); } } public class InterceptSink : IMessageSink { public IMessageSink NextSink { get; private set; } public InterceptSink(IMessageSink nextSink) { this.NextSink = nextSink; } public IMessage SyncProcessMessage(IMessage msg) { IMethodCallMessage mcm = (msg as IMethodCallMessage); // { cross-cut here } IMessage rtnMsg = this.NextSink.SyncProcessMessage(msg); IMethodReturnMessage mrm = (rtnMsg as IMethodReturnMessage); // { cross-cut here } return mrm; } public IMessageCtrl AsyncProcessMessage(IMessage msg, IMessageSink replySink) { return null; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload