public virtual ICallableCore BindTypes(Types genericTypes)
 {
     if (genericTypes.Count == _GenericParams.Count)
     {
         UniqueCallableCore core = null;
         _BindedGenericCache.TryGetValue(genericTypes, out core);
         if (core != null)
         {
             return(core);
         }
         else
         {
             MethodInfo mi = null;
             try
             {
                 mi = ((MethodInfo)Method).MakeGenericMethod(genericTypes.ToArray());
             }
             catch { }
             if (mi != null)
             {
                 core = new UniqueCallableCore(mi);
                 _BindedGenericCache[genericTypes] = core;
                 return(core);
             }
             else
             {
                 if (GLog.IsLogInfoEnabled)
                 {
                     GLog.LogInfo("Unable to bind types to generic method.");
                 }
             }
         }
     }
     return(null);
 }
Ejemplo n.º 2
0
        public override object[] Call(params object[] args)
        {
            Types types = new Types();

            if (args != null)
            {
                for (int i = 0; i < args.Length; ++i)
                {
                    types.Add(args[i].UnwrapDynamic() as Type);
                    if (types[i] == null)
                    {
                        return(null);
                    }
                }
            }
            UniqueCallableCore ucore = _Core.FindAppropriate(types);

            if (ucore == null)
            {
                if (GLog.IsLogInfoEnabled)
                {
                    GLog.LogInfo("Cann't find method with appropriate params.");
                }
                return(null);
            }
            else
            {
                var rv = ObjectPool.GetReturnValueFromPool(1);
                rv[0] = ClrCallable.GetFromPool(ucore, Target);
                return(rv);
            }
        }
Ejemplo n.º 3
0
        public object[] Call(object target, params object[] args)
        {
            Types types = new Types();

            if (args != null)
            {
                for (int i = 0; i < args.Length; ++i)
                {
                    if (args[i] != null)
                    {
                        types.Add(args[i].GetType());
                    }
                    else
                    {
                        types.Add(null);
                    }
                }
            }
            UniqueCallableCore ucore = FindAppropriate(types);

            if (ucore == null)
            {
                if (GLog.IsLogInfoEnabled)
                {
                    GLog.LogInfo("Cann't find method with appropriate params.");
                }
                return(null);
            }
            return(ucore.Call(target, args));
        }
        public ICallableCore BindTypes(Types genericTypes)
        {
            //{
            //    ICallableCore core;
            //    if (_BindedGenericCache.TryGetValue(genericTypes, out core))
            //    {
            //        return core;
            //    }
            //}

            int cnt = genericTypes.Count;
            LinkedListNode <GenericCallableCore> node;

            _GenericParamCountCache.TryGetValue(cnt, out node);
            if (node == null)
            {
                //_BindedGenericCache[genericTypes] = null;
                return(null);
            }
            List <UniqueCallableCore> methods = new List <UniqueCallableCore>();

            do
            {
                UniqueCallableCore core = node.Value.BindTypes(genericTypes) as UniqueCallableCore;
                if (core != null)
                {
                    methods.Add(core);
                }
                node = node.Next;
            } while (node != null && node.Value._GenericParams.Count == cnt);
            if (methods.Count <= 0)
            {
                //_BindedGenericCache[genericTypes] = null;
                return(null);
            }
            else if (methods.Count == 1)
            {
                var core = methods[0];
                //_BindedGenericCache[genericTypes] = core;
                return(core);
            }
            else
            {
                var core = new GroupCallableCore(null);
                for (int i = 0; i < methods.Count; ++i)
                {
                    core._SeqCache.AddLast(methods[i]);
                }
                //_BindedGenericCache[genericTypes] = core;
                return(core);
            }
        }
Ejemplo n.º 5
0
        protected internal UniqueCallableCore FindAppropriate(Types pt)
        {
            UniqueCallableCore ucore = null;

            if (_TypedCache.TryGetValue(pt, out ucore))
            {
                if (ucore == null)
                {
                    return(null);
                }
                else
                {
                    return(ucore);
                }
            }
            var node = _SeqCache.First;
            LinkedListNode <UniqueCallableCore> found = null;
            int foundw = int.MaxValue;

            while (node != null)
            {
                var core    = node.Value;
                var cancall = core.CanCall(pt);
                if (cancall == 0)
                {
                    found  = node;
                    foundw = 0;
                    break;
                }
                if (cancall > 0 && cancall <= foundw)
                {
                    found  = node;
                    foundw = cancall;
                }
                node = node.Next;
            }
            if (found != null)
            {
                _TypedCache[pt] = found.Value;
                //_SeqCache.Remove(found);
                //_SeqCache.AddFirst(found);
                return(found.Value);
            }
            else
            {
                _TypedCache[pt] = null;
                return(null);
            }
        }
Ejemplo n.º 6
0
        public static ClrCallable WrapDelegate(this Delegate del)
        {
            if (del != null)
            {
#if NETFX_CORE
                var core = new UniqueCallableCore(del.GetType().GetMethod("Invoke"));
                var rv   = ClrCallable.GetFromPool(core, del);
#else
                var core = new UniqueCallableCore(del.GetDelegateMethod());
                var rv   = ClrCallable.GetFromPool(core, del.Target);
#endif
                rv.Binding = del;
                return(rv);
            }
            return(null);
        }
Ejemplo n.º 7
0
 protected internal GroupCallableCore(MethodBase[] minfos, Dictionary <Types, UniqueCallableCore> tcache)
 {
     if (minfos != null)
     {
         UniqueCallableCore[] callables = new UniqueCallableCore[minfos.Length];
         for (int i = 0; i < minfos.Length; ++i)
         {
             callables[i] = new UniqueCallableCore(minfos[i]);
         }
         Array.Sort(callables, (ca, cb) =>
         {
             return(Types.Compare(ca._MethodParamTypes, cb._MethodParamTypes));
         });
         for (int i = 0; i < minfos.Length; ++i)
         {
             _SeqCache.AddLast(callables[i]);
         }
     }
     if (tcache == null)
     {
         tcache = new Dictionary <Types, UniqueCallableCore>();
     }
     _TypedCache = tcache;
 }
Ejemplo n.º 8
0
 internal UniqueCallableOverloadSelector(UniqueCallableCore core, object tar)
 {
     _Core  = core;
     Target = tar;
 }