Click here to Skip to main content
15,888,521 members
Articles / Programming Languages / C#

Introducing the LinFu Framework, Part III: LinFu.Delegates-Lambda Arguments & Universal Event Handling

Rate me:
Please Sign up or sign in to vote.
4.90/5 (27 votes)
12 Nov 2007LGPL313 min read 55.6K   707   43  
A library for currying delegates and for handling any event fired from any object instance
using System;
using System.Diagnostics;
using System.Reflection;
using System.Text;

namespace LinFu.DynamicProxy
{
    public class InvocationInfo
    {
        private object[] _args;
        private object _proxy;
        private MethodInfo _targetMethod;
        private StackTrace _trace;
        private Type[] _typeArgs;

        public InvocationInfo(object proxy, MethodInfo targetMethod,
                              StackTrace trace, Type[] genericTypeArgs, object[] args)
        {
            _proxy = proxy;
            _targetMethod = targetMethod;
            _typeArgs = genericTypeArgs;
            _args = args;
            _trace = trace;
        }

        public object Target
        {
            get { return _proxy; }
        }

        public MethodInfo TargetMethod
        {
            get { return _targetMethod; }
        }

        public StackTrace StackTrace
        {
            get { return _trace; }
        }

        public MethodInfo CallingMethod
        {
            get { return (MethodInfo) _trace.GetFrame(0).GetMethod(); }
        }

        public Type[] TypeArguments
        {
            get { return _typeArgs; }
        }

        public object[] Arguments
        {
            get { return _args; }
        }

        public void SetArgument(int position, object arg)
        {
            _args[position] = arg;
        }

        public override string ToString()
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat("Calling Method: {0,30:G}\n", GetMethodName(CallingMethod));
            builder.AppendFormat("Target Method:{0,30:G}\n", GetMethodName(_targetMethod));
            builder.AppendLine("Arguments:");

            foreach (ParameterInfo info in _targetMethod.GetParameters())
            {
                object currentArgument = _args[info.Position];
                if (currentArgument == null)
                    currentArgument = "(null)";
                builder.AppendFormat("\t{0,10:G}: {1}\n", info.Name, currentArgument.ToString());
            }
            builder.AppendLine();

            return builder.ToString();
        }

        private string GetMethodName(MethodInfo method)
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat("{0}.{1}", method.DeclaringType.Name, method.Name);
            builder.Append("(");

            ParameterInfo[] parameters = method.GetParameters();
            int parameterCount = parameters != null ? parameters.Length : 0;

            int index = 0;
            foreach (ParameterInfo param in parameters)
            {
                index++;
                builder.AppendFormat("{0} {1}", param.ParameterType.Name, param.Name);

                if (index < parameterCount)
                    builder.Append(", ");
            }
            builder.Append(")");

            return builder.ToString();
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


Written By
Software Developer (Senior) Readify
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions