c# - How do you intercept method calls on a base class using PostSharp? -


i want provide implementation of system.object.tostring various classes using postsharp. i've created aspect inheriting methodinterceptionaspect oninvoke method isn't getting invoked when call echodto.tostring takes place.

how can oninvoke called when tostring called?

[datacontract] [implementjsontostringaspect()] public class echodto {      [datamember]     public string text { get; set; }  }  [serializable] [multicastattributeusage(multicasttargets.method)] public class implementjsontostringaspect : methodinterceptionaspect {      public override void oninvoke(methodinterceptionargs args)     {         base.oninvoke(args); // never gets called     }      public override bool compiletimevalidate(methodbase method)     {         return method.name == "tostring";     }  } 

inherit instancelevelaspect , decorate method [introducemember(overrideaction=memberoverrideaction.overrideorfail)]. reference this on target object, use this.instance.

/// <summary> /// implements tostring method on target class serializes members json. /// </summary> [serializable] public class implementjsontostringaspect : instancelevelaspect {      #region methods      /// <summary>     /// provides implementation of <see cref="system.object.tostring"/> serializes instance's     /// public members json.     /// </summary>     /// <returns></returns>     [introducemember(overrideaction=memberoverrideaction.overrideorfail)]     public override string tostring()     {         return jsonconvert.serializeobject(this.instance);     }      #endregion  } 

note: requires paid version of postsharp instancelevelaspect not supported free version.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -