Пример #1
0
        public object Invoke(ThreadContext context, object _this, object[] _params)
        {
            if (_this is CLRSharp_Instance)
            {
                CLRSharp_Instance inst = _this as CLRSharp_Instance;
                if (inst.type.HasSysBase)
                {
                    var btype = inst.type.ContainBase(method_System.DeclaringType);
                    if (btype)
                    {
                        var CrossBind = context.environment.GetCrossBind(method_System.DeclaringType);
                        if (CrossBind != null)
                        {
                            _this = CrossBind.CreateBind(inst);
                        }
                        else
                        {
                            _this = (_this as CLRSharp_Instance).system_base;
                            //如果没有绑定器,尝试直接使用System_base;
                        }
                        //context.environment.logger.Log("这里有一个需要映射的类型");
                    }
                }
            }
            //委托是很特殊的存在
            //if(this.DeclaringType.IsDelegate)
            //{

            //}
            if (method_System is System.Reflection.ConstructorInfo)
            {
                if (method_System.DeclaringType.IsSubclassOf(typeof(Delegate)))
                {//创建委托
                    object  src    = _params[0];
                    RefFunc fun    = _params[1] as RefFunc;
                    IMethod method = fun._method;
                    MemoryPool.ReleaseRefFunc(fun);
                    ICLRType_Sharp clrtype = method.DeclaringType as ICLRType_Sharp;
                    if (clrtype != null)//onclr
                    {
                        CLRSharp_Instance inst = src as CLRSharp_Instance;
                        if (method.isStatic && clrtype != null)
                        {
                            inst = clrtype.staticInstance;
                        }
                        return(inst.GetDelegate(context, method_System.DeclaringType, method));
                    }
                    else//onsystem
                    {
                        ICLRType_System stype = method.DeclaringType as ICLRType_System;
                        return(stype.CreateDelegate(method_System.DeclaringType, src, method as IMethod_System));
                    }
                }
                object[] _outp = null;
                if (_params != null && _params.Length > 0)
                {
                    _outp = new object[_params.Length];
                    var _paramsdef = ParamList;
                    for (int i = 0; i < _params.Length; i++)
                    {
                        if (_params[i] == null)
                        {
                            _outp[i] = null;
                            continue;
                        }
                        Type tsrc    = _params[i].GetType();
                        Type ttarget = _paramsdef[i].TypeForSystem;
                        if (tsrc == ttarget)
                        {
                            _outp[i] = _params[i];
                        }
                        else if (tsrc.IsSubclassOf(ttarget))
                        {
                            _outp[i] = _params[i];
                        }
                        else if (ttarget.IsEnum)//特殊处理枚举
                        {
                            _outp[i] = Enum.ToObject(ttarget, _params[i]);
                        }
                        else if (tsrc.IsEnum)
                        {
                            _outp[i] = Enum.ToObject(ttarget, _params[i]);
                        }
                        else
                        {
                            if (ttarget == typeof(byte))
                            {
                                _outp[i] = (byte)Convert.ToDecimal(_params[i]);
                            }
                            else
                            {
                                _outp[i] = _params[i];
                            }
                            //var ms =_params[i].GetType().GetMethods();
                        }
                    }
                }
                var newobj = (method_System as System.Reflection.ConstructorInfo).Invoke(_outp);
                return(newobj);
            }
            else
            {
                object[] hasref = MemoryPool.GetArray(_params.Length);
                long     flag   = 0;
                object[] _outp  = null;
                if (_params != null && _params.Length > 0)
                {
                    _outp = new object[_params.Length];
                    var _paramsdef = ParamList;
                    for (int i = 0; i < _params.Length; i++)
                    {
                        var _targetType = _paramsdef[i].TypeForSystem;
                        if (_targetType.GetElementType() != null)
                        {
                            _targetType = _targetType.GetElementType();
                        }
                        if (_params[i] is CLRSharp.StackFrame.RefObj) //特殊处理outparam
                        {
                            object v = (_params[i] as CLRSharp.StackFrame.RefObj).Get();
                            MemoryPool.ReleaseRefObj(_params[i]);
                            if (v is VBox)
                            {
                                v = (v as VBox).BoxDefine();
                            }
                            else if (v == null)
                            {
                                v = null;
                            }
                            else if (v.GetType() != _targetType &&
                                     _targetType != typeof(object))
                            {
                                v = v.Convert(_targetType);
                            }
                            hasref[i] = v;
                            flag     |= (1 << i);
                            _outp[i]  = v;
                        }
                        else if (_targetType.IsEnum || _params[i] is Enum) //特殊处理枚举
                        {
                            _outp[i] = Enum.ToObject(_targetType, _params[i]);
                        }
                        else
                        {
                            var v = _params[i];
                            if (v != null && v.GetType() != _targetType &&
                                _targetType != typeof(object))
                            {
                                v = v.Convert(_targetType);
                            }
                            _outp[i] = v;
                        }
                    }
                }
                //if (method_System.DeclaringType.IsSubclassOf(typeof(Delegate)))//直接用Delegate.Invoke,会导致转到本机代码再回来
                ////会导致错误堆栈不方便观察,但是也没办法直接调用,只能手写一些常用类型
                //{
                //    //需要从Delegate转换成实际类型执行的帮助类
                //    Action<int> abc = _this as Action<int>;
                //    abc((int)_params[0]);
                //    return null;
                //}
                //else
                {
                    try
                    {
                        if (_this != null && _this.GetType() != method_System.DeclaringType)
                        {
                            _this = _this.Convert(method_System.DeclaringType);
                        }

                        var _out = method_System.Invoke(_this, _outp);
                        {
                            for (int i = 0; i < hasref.Length; i++)
                            {
                                var _ref = hasref[i];
                                if ((flag & (1 << i)) == 0)
                                {
                                    continue;
                                }

                                if (_ref is VBox)
                                {
                                    (_ref as VBox).SetDirect(_outp[i]);
                                }
                                else
                                {
                                    (_params[i] as CLRSharp.StackFrame.RefObj).Set(_outp[i]);
                                }
                            }
                        }
                        return(_out);
                    }
                    catch (Exception ex)
                    {
                        StringBuilder sb = new StringBuilder();
                        if (_outp != null)
                        {
                            sb.Append("real args: ");
                            {
                                var __array6       = _outp;
                                var __arrayLength6 = __array6.Length;
                                for (int __i6 = 0; __i6 < __arrayLength6; ++__i6)
                                {
                                    var o = __array6[__i6];
                                    {
                                        sb.Append(o.GetType().FullName);
                                        sb.Append("=");
                                        sb.Append(o);
                                        sb.Append(",");
                                    }
                                }
                            }
                        }

                        context.environment.logger.Log_Error(string.Format("name:{0} type:{1} ret:{2} {3} ex:{4}", method_System.ToString(),
                                                                           DeclaringType, ReturnType, sb, ex));
                        throw;
                    }
                    finally
                    {
                        MemoryPool.ReleaseArray(hasref);
                    }
                }
            }
        }