Click here to Skip to main content
15,897,518 members
Articles / Programming Languages / C#

Introducing the LinFu Framework, Part II: LinFu.DynamicObject – Adding Dynamic Language Features to Statically Typed Languages

Rate me:
Please Sign up or sign in to vote.
4.97/5 (50 votes)
12 Nov 2007LGPL316 min read 145.1K   882   67  
Using LinFu.DynamicObject to add mixins, duck typing and multiple dispatch to your favorite .NET languages
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using LinFu.Common;

namespace LinFu.Reflection
{
    internal class DelegateMixin : IMethodMissingCallback
    {
        private MulticastDelegate _target;
        private string _methodName;
        public DelegateMixin(string methodname, MulticastDelegate targetDelegate)
        {
            _methodName = methodname;
            _target = targetDelegate;
        }
        #region IMethodMissingCallback Members

        public void MethodMissing(object source, 
            MethodMissingParameters missingParameters)
        {
            PredicateBuilder builder = new PredicateBuilder();

            // The current method name must match the given method name
            if (_methodName != missingParameters.MethodName)
                return;

            if (missingParameters.Arguments != null)
                builder.RuntimeArguments.AddRange(missingParameters.Arguments);

            builder.MatchRuntimeArguments = true;

            Predicate<MethodInfo> finderPredicate = builder.CreatePredicate();
            FuzzyFinder<MethodInfo> finder = new FuzzyFinder<MethodInfo>();
            finder.Tolerance = .60;

            // Match the criteria against the target delegate
            List<MethodInfo> searchList = new List<MethodInfo>(new MethodInfo[] {_target.Method});

            // Determine if the signature is compatible
            MethodInfo match = finder.Find(finderPredicate, searchList);
            if (match == null)
                return;

            // If the signature is compatible, then execute the method
            MethodInfo targetMethod = _target.Method;

            object result = null;
            try
            {
                result = targetMethod.Invoke(_target.Target, missingParameters.Arguments);
                missingParameters.Handled = true;
            }
            catch (TargetInvocationException ex)
            {
                missingParameters.Handled = false;
                throw ex.InnerException;
            }

            
            missingParameters.ReturnValue = result;
        }

        #endregion

        #region IMethodMissingCallback Members


        public bool CanHandle(MethodInfo method)
        {
            Predicate<MethodInfo> predicate = PredicateBuilder.CreatePredicate(method);
            FuzzyFinder<MethodInfo> finder = new FuzzyFinder<MethodInfo>();
            finder.Tolerance = .66;

            MethodInfo[] searchPool = new MethodInfo[] {_target.Method};
            MethodInfo match = finder.Find(predicate, searchPool);

            return match != null;
        }

        #endregion
    }
}

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