Is it possible to access member variables from ICallHandler.Invoke?

Oct 18, 2011 at 9:47 PM

Hi,

I have implemented my call handler and I want to know if I can get access to members of the method being invoked after the call. i.e. if I call a method A(){ var t="test";}, i need to access value of t.

Is it possible? I can decorate the member with any attribute if required. Please suggest if anyone has any idea of how to implement this.

thanks in advance

Oct 23, 2011 at 9:16 AM

From your example code, it looks as though you want to access a local variable after the call has returned.  You won't be able to do that.

In terms of accessing member variables in your call handler you can do that.  You could create a HandlerAttribute with the member or property name and then use reflection to access it.

    public class LogHandlerAttribute : HandlerAttribute
    {
        public string PropertyName
        {
            get;
            set;
        }

        public override ICallHandler CreateHandler(IUnityContainer container)
        {
            var handler = new LogCallHandler(container, PropertyName);
            
            return handler;
        }

    }


    public class LogCallHandler : ICallHandler
    {
        private string property;
        IUnityContainer container;

        public LogCallHandler(IUnityContainer container, string property)
        {
            this.container = container;
            this.property = property;
        }

        public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
        {
            var returnValue = getNext()(input, getNext);
            
            PropertyInfo pi = input.Target.GetType().GetProperty(property);
            object propVal = pi.GetValue(input.Target, null);
            
            Console.WriteLine(propVal);
            
            return returnValue;
        }
 
        public int Order { get; set; }
    }

 

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com