4

I want access base class member in our Log Aspect Class. I have one base controller & that controller inherit by Test controller & in Test Controller i implemented AOP Aspect.

In BaseContoller i have a member _userSession. I initializing _userSession when BaseContoller's Constructor is call. And after call TestController first AOP Aspect is called. I want Access _userSession on OnEntry method of AOP.

LogAspect Class

[Serializable]
    [MulticastAttributeUsage(MulticastTargets.Method)]
    public class LogAspect:PostSharp.Aspects.OnMethodBoundaryAspect
    {
        public object UserData;

        public override void OnEntry(PostSharp.Aspects.MethodExecutionArgs args)
        {
            LogManager.Info(string.Format("Starting - {0}-{0}",args.Instance, new StackTrace().GetFrame(1).GetMethod().Name));

// want access PCX.Portal.Controllers.BaseController._userSession member here its showing in quick watch like this
//((PCX.Portal.Controllers.BaseController)(args.Instance))._userSession

           LogManager.Info(string.Format("User data - {0}", FrameworkHelper.Helper.JSONHelper.GetJSON(UserData)));


            if(args.Arguments.Count>0)
            {
                foreach (var item in  args.Arguments)
                {
                    LogManager.Info(string.Format("arguments - {0}", FrameworkHelper.Helper.JSONHelper.GetJSON(item)));
                }
            }

            base.OnEntry(args);
        }

Base controller

public class BaseController : Controller
    {
        public UserSession _userSession { set; get; }
        AuthenticationManager _authenticationManager = new AuthenticationManager();

        public BaseController()
        {
//initializing _userSession here  
                 _userSession.userid=4 ;
_userSession.customerId=5 ;
        }
}

Test Controller

[LogAspect]
    public class TestController : BaseController
    {
        public ActionResult Index()
        {
            return View();            
        }
}
Mayur Prajapati
  • 5,454
  • 7
  • 41
  • 70
virender
  • 4,539
  • 5
  • 27
  • 31

1 Answers1

4

As documentation states:

MethodExecutionArgs class contains property Instance that:

Gets or sets the object instance on which the method is being executed.

as long as your method is not static you will get the object that is the this inside that method. Now you need to just cast it to BaseController as your property is public you will be able to access it.

if(args.Instance != null){
    var baseController = (BaseController)args.Instance;
    baseController._userSession
}

Although this is what you asked for I feel a need to remark that this approach limits your aspects usability to only instance methods of classes that inherit from BaseController. If you are able to create/retrieve form somewhere the session data in that parameterless constructor you can do it in aspect as well.

Rafal
  • 12,391
  • 32
  • 54
  • Type baseType = args.Instance.GetType().BaseType; It return base type But facing problem to get member after this code. I tried but not working, I also tried using reflection but facing problem . Can you explain more . Thanks – virender Mar 18 '15 at 06:34
  • and while debugging what types of values are in the `Instance` property and `Method` property of args. – Rafal Mar 18 '15 at 06:38
  • Type baseType = args.Instance.GetType().BaseType; returning "baseType = {Name = "BaseController" FullName = "PCX.Portal.Controllers.BaseController"}" . Now what I need to do ? – virender Mar 18 '15 at 06:57
  • As in my answer copy past the if statement and use `_userSession` as you see fit. – Rafal Mar 18 '15 at 08:27
  • Actually problem is that LogAspect is not a part of this project its a library which using in MVC project and in Repository project. Like FrameworkHelper (Contains CacheAspect and LogAspect) , PCX.Core.Repositories (Contains DAL), PCX.Portal (MVC Project). So, BaseController can't accessible inside FrameworkHelper utility. – virender Mar 18 '15 at 08:53
  • In that case you can either change the approach considering my remark, or use reflection to access this property, or create an interface that will be in you dll and will be implemented by `BaseController`. This interface can be in Repositories dll named like `IUserSessionProvider` or in third dll that is common for both Repositories and MVC. – Rafal Mar 18 '15 at 11:02