Esempio n. 1
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("});");
#if UNITY_EDITOR
                ILREditorExten.InsertDelegate(sb.ToString());
#endif
                //
                throw new KeyNotFoundException("已添加未注册Delegate,请重启游戏 + \n" + sb.ToString());
            }
        }
        public static void ThrowAdapterNotFound(IMethod method)
        {
            StringBuilder sb = new StringBuilder();
            //sb.Append("Cannot find Delegate Adapter for:");
            //sb.Append(method.ToString());
            string clsName, rName;
            bool   isByRef;

            if (method.ReturnType.Name != "Void" || method.ParameterCount > 0)
            {
                //sb.AppendLine(", Please add following code:");
                if (method.ReturnType.Name == "Void")
                {
                    sb.Append("appdomain.DelegateManager.RegisterMethodDelegate<");
                    bool first = true;
                    foreach (var i in method.Parameters)
                    {
                        if (first)
                        {
                            first = false;
                        }
                        else
                        {
                            sb.Append(", ");
                        }
                        i.TypeForCLR.GetClassName(out clsName, out rName, out isByRef);
                        sb.Append(rName);
                    }
                    sb.AppendLine(">();");
                }
                else
                {
                    sb.Append("appdomain.DelegateManager.RegisterFunctionDelegate<");
                    bool first = true;
                    foreach (var i in method.Parameters)
                    {
                        if (first)
                        {
                            first = false;
                        }
                        else
                        {
                            sb.Append(", ");
                        }
                        i.TypeForCLR.GetClassName(out clsName, out rName, out isByRef);
                        sb.Append(rName);
                    }
                    if (!first)
                    {
                        sb.Append(", ");
                    }
                    method.ReturnType.TypeForCLR.GetClassName(out clsName, out rName, out isByRef);
                    sb.Append(rName);
                    sb.AppendLine(">();");
                }
            }

#if UNITY_EDITOR
            ILREditorExten.InsertDelegate(sb.ToString());
#endif
            //
            throw new KeyNotFoundException("已添加未注册Delegate,请重启游戏");
        }