Esempio n. 1
0
        public override Delegate CreateDelegate(Type delegateType, object target)
        {
            ILTypeInstance ilTypeInstance;

            if (target is ILTypeInstance)
            {
                ilTypeInstance = target as ILTypeInstance;
            }
            else if (target is CrossBindingAdaptorType adaptor)
            {
                ilTypeInstance = adaptor.ILInstance;
            }
            else
            {
                return(CreateDelegate(delegateType));
            }

            IDelegateAdapter del;

            if (iDelegate == null)
            {
                iDelegate = appdomain.DelegateManager.FindDelegateAdapter(ilTypeInstance, method, method);
                del       = iDelegate;
            }
            else
            {
                del = iDelegate.Instantiate(appdomain, ilTypeInstance, iDelegate.Method);
            }
            return(del.Delegate);
        }
Esempio n. 2
0
        private IDelegateAdapter GetDelgateAdapter(string _Function, int _pramcount, IType _classtype, object _target, out bool _isNeedReg)
        {
            _isNeedReg = false;
            if (_classtype == null || _target == null)
            {
                return(null);
            }
            ILMethod methodctor = GetLMethod(_classtype, _Function, _pramcount) as ILMethod;

            if (methodctor == null)
            {
                return(null);
            }
            if (!typeof(ILTypeInstance).IsInstanceOfType(_target))
            {
                return(null);
            }
            ILTypeInstance tclrobj = _target as ILTypeInstance;

            _isNeedReg = !mApp.DelegateManager.IsRegToMethodDelegate(methodctor);
            if (_isNeedReg)
            {
                return(null);
            }
            IDelegateAdapter ret = tclrobj.GetDelegateAdapter(methodctor);

            if (ret == null)
            {
                ret = mApp.DelegateManager.FindDelegateAdapter(tclrobj, methodctor);
            }
            return(ret);
        }
Esempio n. 3
0
        private void MethodAdd <T1>(Dictionary <Type, IDelegateAdapter> method, IDelegateAdapter adapter)
        {
            var t1 = typeof(T1);

            if (!method.ContainsKey(t1))
            {
                method.Add(t1, adapter);
            }
        }
Esempio n. 4
0
 internal void SetDelegateAdapter(ILMethod method, IDelegateAdapter adapter)
 {
     if (!delegates.ContainsKey(method))
     {
         delegates[method] = adapter;
     }
     else
     {
         throw new NotSupportedException();
     }
 }
Esempio n. 5
0
 public virtual void Combine(IDelegateAdapter adapter)
 {
     if (next != null)
     {
         next.Combine(adapter);
     }
     else
     {
         next = adapter;
     }
 }
Esempio n. 6
0
 public virtual bool Equals(IDelegateAdapter adapter)
 {
     if (adapter is DelegateAdapter)
     {
         DelegateAdapter b = (DelegateAdapter)adapter;
         return(instance == b.instance && method == b.method);
     }
     else
     {
         return(false);
     }
 }
Esempio n. 7
0
        private void FunctionAdd <T1>(Dictionary <Type, IDelegateAdapter> function, IDelegateAdapter adapter)
        {
            var t1 = typeof(T1);

            if (function.ContainsKey(t1))
            {
            }
            else
            {
                function.Add(t1, adapter);
            }
        }
Esempio n. 8
0
        internal Delegate ConvertToDelegate(Type clrDelegateType, IDelegateAdapter adapter)
        {
            Func <Delegate, Delegate> func;

            if (clrDelegates.TryGetValue(clrDelegateType, out func))
            {
                return(func(adapter.Delegate));
            }
            else
            {
                throw new KeyNotFoundException("Cannot find convertor for " + clrDelegateType);
            }
        }
Esempio n. 9
0
 public virtual void Remove(IDelegateAdapter adapter)
 {
     if (next != null)
     {
         if (next.Equals(adapter))
         {
             next = ((DelegateAdapter)next).next;
         }
         else
         {
             next.Remove(adapter);
         }
     }
 }
Esempio n. 10
0
        override public K GetCSLEDelegate <K>(string _Function, IType _classtype, object _target)
        {
            if (_classtype == null || _target == null)
            {
                return(default(K));
            }
            object           ret      = null;
            bool             tneedreg = false;
            IDelegateAdapter tdelapt  = GetDelgateAdapter(_Function, 0, _classtype, _target, out tneedreg);

            if (tdelapt != null)
            {
                ret = tdelapt.Delegate;
            }
            return((K)ret);
        }
Esempio n. 11
0
 public virtual bool Equals(IDelegateAdapter adapter)
 {
     if (adapter is DelegateAdapter)
     {
         DelegateAdapter b = (DelegateAdapter)adapter;
         if (adapter is DummyDelegateAdapter)
         {
             return(instance == b.instance && next == b.next && method == b.method);
         }
         else
         {
             return(instance == b.instance && next == b.next && method == b.method && Delegate == b.Delegate);
         }
     }
     else
     {
         return(false);
     }
 }
Esempio n. 12
0
        override public K GetCSLEDelegate <K, T1, T2, T3, T4>(string _Function, IType _classtype, object _target)
        {
            if (_classtype == null || _target == null)
            {
                return(default(K));
            }
            object           ret      = null;
            bool             tneedreg = false;
            IDelegateAdapter tdelapt  = GetDelgateAdapter(_Function, 4, _classtype, _target, out tneedreg);

            if (tneedreg)
            {
                mApp.DelegateManager.RegisterMethodDelegate <T1, T2, T3, T4>();
                tdelapt = GetDelgateAdapter(_Function, 4, _classtype, _target, out tneedreg);
            }
            if (tdelapt != null)
            {
                ret = tdelapt.Delegate;
            }
            return((K)ret);
        }
Esempio n. 13
0
        private IDelegateAdapter GetFunctionAdapter(ILTypeInstance instance, ILMethod method)
        {
            IDelegateAdapter adapter = null;
            int paramCount           = method.ParameterCount;

            switch (paramCount)
            {
            case 0:
            {
                if (m_functions_1.ContainsKey(method.ReturnType.TypeForCLR))
                {
                    adapter = m_functions_1[method.ReturnType.TypeForCLR];
                }
                break;
            }

            case 1:
            {
                if (m_functions_2.ContainsKey(method.Parameters[0].TypeForCLR))
                {
                    var dic = m_functions_2[method.Parameters[0].TypeForCLR];
                    adapter = GetFunctionAdapter(instance, method, 0, dic);
                }
                break;
            }

            case 2:
            {
                if (m_functions_3.ContainsKey(method.Parameters[0].TypeForCLR))
                {
                    var dic = m_functions_3[method.Parameters[0].TypeForCLR];
                    adapter = GetFunctionAdapter(instance, method, 0, dic);
                }
                break;
            }

            case 3:
            {
                if (m_functions_4.ContainsKey(method.Parameters[0].TypeForCLR))
                {
                    var dic = m_functions_4[method.Parameters[0].TypeForCLR];
                    adapter = GetFunctionAdapter(instance, method, 0, dic);
                }
                break;
            }

            case 4:
            {
                if (m_functions_5.ContainsKey(method.Parameters[0].TypeForCLR))
                {
                    var dic = m_functions_5[method.Parameters[0].TypeForCLR];
                    adapter = GetFunctionAdapter(instance, method, 0, dic);
                }
                break;
            }

            case 5:
            {
                if (m_functions_6.ContainsKey(method.Parameters[0].TypeForCLR))
                {
                    var dic = m_functions_6[method.Parameters[0].TypeForCLR];
                    adapter = GetFunctionAdapter(instance, method, 0, dic);
                }
                break;
            }
            }
            if (adapter == null)
            {
                return(null);
            }
            var res = adapter.Instantiate(appdomain, instance, method);

            if (instance != null)
            {
                instance.SetDelegateAdapter(method, res);
            }
            return(res);
        }
Esempio n. 14
0
        private void FunctionAdd <T1, T2, T3, T4, T5, T6>(Dictionary <Type, Dictionary <Type, Dictionary <Type, Dictionary <Type, Dictionary <Type, Dictionary <Type, IDelegateAdapter> > > > > > function, IDelegateAdapter adapter)
        {
            var t1 = typeof(T1);

            if (!function.ContainsKey(t1))
            {
                Dictionary <Type, Dictionary <Type, Dictionary <Type, Dictionary <Type, Dictionary <Type, IDelegateAdapter> > > > > m = new Dictionary <Type, Dictionary <Type, Dictionary <Type, Dictionary <Type, Dictionary <Type, IDelegateAdapter> > > > >();
                function.Add(t1, m);
                FunctionAdd <T2, T3, T4, T5, T6>(m, adapter);
            }
            else
            {
                FunctionAdd <T2, T3, T4, T5, T6>(function[t1], adapter);
            }
        }
Esempio n. 15
0
        private void MethodAdd <T1, T2, T3, T4, T5>(Dictionary <Type, Dictionary <Type, Dictionary <Type, Dictionary <Type, Dictionary <Type, IDelegateAdapter> > > > > method, IDelegateAdapter adapter)
        {
            var t1 = typeof(T1);

            if (!method.ContainsKey(t1))
            {
                Dictionary <Type, Dictionary <Type, Dictionary <Type, Dictionary <Type, IDelegateAdapter> > > > m = new Dictionary <Type, Dictionary <Type, Dictionary <Type, Dictionary <Type, IDelegateAdapter> > > >();
                method.Add(t1, m);
                MethodAdd <T2, T3, T4, T5>(m, adapter);
            }
            else
            {
                MethodAdd <T2, T3, T4, T5>(method[t1], adapter);
            }
        }
Esempio n. 16
0
        internal IDelegateAdapter FindDelegateAdapter(ILTypeInstance instance, ILMethod method)
        {
            IDelegateAdapter res = null;

            if (method.ReturnType == appdomain.VoidType)
            {
                if (method.ParameterCount == 0)
                {
                    res = zeroParamMethodAdapter.Instantiate(appdomain, instance, method);
                    if (instance != null)
                    {
                        instance.SetDelegateAdapter(method, res);
                    }
                    return(res);
                }

                res = GetMethodAdapter(instance, method);
                if (res != null)
                {
                    return(res);
                }
                else if (false)
                {
                    foreach (var i in methods)
                    {
                        if (i.ParameterTypes.Length == method.ParameterCount)
                        {
                            bool match = true;
                            for (int j = 0; j < method.ParameterCount; j++)
                            {
                                if (i.ParameterTypes[j] != method.Parameters[j].TypeForCLR)
                                {
                                    match = false;
                                    break;
                                }
                            }
                            if (match)
                            {
                                UnityEngine.Debug.Log("快速查找没有找到,但是老的查找却找到了的!");
                                res = i.Adapter.Instantiate(appdomain, instance, method);
                                if (instance != null)
                                {
                                    instance.SetDelegateAdapter(method, res);
                                }
                                return(res);
                            }
                        }
                    }
                }
            }
            else
            {
                res = GetFunctionAdapter(instance, method);
                if (res != null)
                {
                    return(res);
                }
                else if (false)
                {
                    foreach (var i in functions)
                    {
                        if (i.ParameterTypes.Length == method.ParameterCount + 1)
                        {
                            bool match = true;
                            for (int j = 0; j < method.ParameterCount; j++)
                            {
                                if (i.ParameterTypes[j] != method.Parameters[j].TypeForCLR)
                                {
                                    match = false;
                                    break;
                                }
                            }
                            if (match)
                            {
                                if (method.ReturnType.TypeForCLR == i.ParameterTypes[method.ParameterCount])
                                {
                                    UnityEngine.Debug.Log("快速查找没有找到,但是老的查找却找到了的!");
                                    res = i.Adapter.Instantiate(appdomain, instance, method);
                                    if (instance != null)
                                    {
                                        instance.SetDelegateAdapter(method, res);
                                    }
                                    return(res);
                                }
                            }
                        }
                    }
                }
            }

            res = dummyAdapter.Instantiate(appdomain, instance, method);
            if (instance != null)
            {
                instance.SetDelegateAdapter(method, res);
            }
            return(res);
        }
Esempio n. 17
0
        internal Delegate ConvertToDelegate(Type clrDelegateType, IDelegateAdapter adapter)
        {
            Func <Delegate, Delegate> func;

            if (adapter is DummyDelegateAdapter)
            {
                DelegateAdapter.ThrowAdapterNotFound(adapter.Method);
                return(null);
            }
            if (clrDelegates.TryGetValue(clrDelegateType, out func))
            {
                return(func(adapter.Delegate));
            }
            else
            {
                StringBuilder sb = new StringBuilder();
                string        clsName, rName;
                bool          isByRef;
                clrDelegateType.GetClassName(out clsName, out rName, out isByRef);
                sb.AppendLine("Cannot find convertor for " + rName);
                sb.AppendLine("Please add following code:");
                sb.Append("appdomain.DelegateManager.RegisterDelegateConvertor<");
                sb.Append(rName);
                sb.AppendLine(">((act) =>");
                sb.AppendLine("{");
                sb.Append("    return new ");
                sb.Append(rName);
                sb.Append("((");
                var  mi    = clrDelegateType.GetMethod("Invoke");
                bool first = true;
                foreach (var i in mi.GetParameters())
                {
                    if (first)
                    {
                        first = false;
                    }
                    else
                    {
                        sb.Append(", ");
                    }
                    sb.Append(i.Name);
                }
                sb.AppendLine(") =>");
                sb.AppendLine("    {");
                if (mi.ReturnType != appdomain.VoidType.TypeForCLR)
                {
                    sb.Append("        return ((Func<");
                    first = true;
                    foreach (var i in mi.GetParameters())
                    {
                        if (first)
                        {
                            first = false;
                        }
                        else
                        {
                            sb.Append(", ");
                        }
                        i.ParameterType.GetClassName(out clsName, out rName, out isByRef);
                        sb.Append(rName);
                    }
                    if (!first)
                    {
                        sb.Append(", ");
                    }
                    mi.ReturnType.GetClassName(out clsName, out rName, out isByRef);
                    sb.Append(rName);
                }
                else
                {
                    sb.Append("        ((Action<");
                    first = true;
                    foreach (var i in mi.GetParameters())
                    {
                        if (first)
                        {
                            first = false;
                        }
                        else
                        {
                            sb.Append(", ");
                        }
                        i.ParameterType.GetClassName(out clsName, out rName, out isByRef);
                        sb.Append(rName);
                    }
                }
                sb.Append(">)act)(");
                first = true;
                foreach (var i in mi.GetParameters())
                {
                    if (first)
                    {
                        first = false;
                    }
                    else
                    {
                        sb.Append(", ");
                    }
                    sb.Append(i.Name);
                }
                sb.AppendLine(");");
                sb.AppendLine("    });");
                sb.AppendLine("});");
                throw new KeyNotFoundException(sb.ToString());
            }
        }