Esempio n. 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;
                    ICLRType_Sharp clrtype = fun._method.DeclaringType as ICLRType_Sharp;
                    if (clrtype != null)//onclr
                    {
                        CLRSharp_Instance inst = src as CLRSharp_Instance;
                        if (fun._method.isStatic && clrtype != null)
                        {
                            inst = clrtype.staticInstance;
                        }
                        return(inst.GetDelegate(context, method_System.DeclaringType, fun._method));
                    }
                    else//onsystem
                    {
                        ICLRType_System stype = fun._method.DeclaringType as ICLRType_System;
                        return(stype.CreateDelegate(method_System.DeclaringType, src, fun._method as IMethod_System));
                    }
                }
                object[] _outp = null;
                if (_params != null && _params.Length > 0)
                {
                    _outp = new object[_params.Length];
                    var _paramsdef = method_System.GetParameters();
                    for (int i = 0; i < _params.Length; i++)
                    {
                        if (_params[i] == null)
                        {
                            _outp[i] = null;
                        }
                        Type tsrc    = _params[i].GetType();
                        Type ttarget = _paramsdef[i].ParameterType;
                        if (tsrc == ttarget)
                        {
                            _outp[i] = _params[i];
                        }
                        else if (tsrc.IsSubclassOf(ttarget))
                        {
                            _outp[i] = _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
            {
                Dictionary <int, object> hasref = new Dictionary <int, object>();
                object[] _outp = null;
                if (_params != null && _params.Length > 0)
                {
                    _outp = new object[_params.Length];
                    var _paramsdef = method_System.GetParameters();
                    for (int i = 0; i < _params.Length; i++)
                    {
                        if (_params[i] is CLRSharp.StackFrame.RefObj)//特殊处理outparam
                        {
                            object v = (_params[i] as CLRSharp.StackFrame.RefObj).Get();
                            if (v is VBox)
                            {
                                v = (v as VBox).BoxDefine();
                            }
                            hasref[i] = v;
                            _outp[i]  = v;
                        }
                        else if (_paramsdef[i].ParameterType.IsEnum)//特殊处理枚举
                        {
                            var ms = _paramsdef[i].ParameterType.GetMethods();
                            _outp[i] = Enum.ToObject(_paramsdef[i].ParameterType, _params[i]);
                        }
                        else
                        {
                            _outp[i] = _params[i];
                        }
                    }
                }
                //if (method_System.DeclaringType.IsSubclassOf(typeof(Delegate)))//直接用Delegate.Invoke,会导致转到本机代码再回来
                ////会导致错误堆栈不方便观察,但是也没办法直接调用,只能手写一些常用类型
                //{
                //    //需要从Delegate转换成实际类型执行的帮助类
                //    Action<int> abc = _this as Action<int>;
                //    abc((int)_params[0]);
                //    return null;
                //}
                //else
                {
                    var _out = method_System.Invoke(_this, _outp);
                    foreach (var _ref in hasref)
                    {
                        if (_ref.Value is VBox)
                        {
                            (_ref.Value as VBox).SetDirect(_outp[_ref.Key]);
                        }
                        else
                        {
                            (_params[_ref.Key] as CLRSharp.StackFrame.RefObj).Set(_outp[_ref.Key]);
                        }
                    }
                    return(_out);
                }
            }
        }
Esempio n. 2
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);
                    }
                }
            }
        }
Esempio n. 3
0
        public object Invoke(ThreadContext context, object _this, object[] _params)
        {
            //委托是很特殊的存在
            //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;
                    ICLRType_Sharp clrtype = fun._method.DeclaringType as ICLRType_Sharp;
                    if (clrtype != null)//onclr
                    {
                        CLRSharp_Instance inst = src as CLRSharp_Instance;
                        if (fun._method.isStatic && clrtype != null)
                        {
                            inst = clrtype.staticInstance;
                        }
                        return(inst.GetDelegate(context, method_System.DeclaringType, fun._method));
                    }
                    else//onsystem
                    {
                        ICLRType_System stype = fun._method.DeclaringType as ICLRType_System;
                        return(stype.CreateDelegate(method_System.DeclaringType, src, fun._method as IMethod_System));
                    }
                }
                object[] _outp = null;
                if (_params != null && _params.Length > 0)
                {
                    _outp = new object[_params.Length];
                    var _paramsdef = method_System.GetParameters();
                    for (int i = 0; i < _params.Length; i++)
                    {
                        if (_params[i] == null)
                        {
                            _outp[i] = null;
                        }
                        Type tsrc    = _params[i].GetType();
                        Type ttarget = _paramsdef[i].ParameterType;
                        if (tsrc == ttarget)
                        {
                            _outp[i] = _params[i];
                        }
                        else if (tsrc.IsSubclassOf(ttarget))
                        {
                            _outp[i] = _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
            {
                //if (method_System.DeclaringType.IsSubclassOf(typeof(Delegate)))//直接用Delegate.Invoke,会导致转到本机代码再回来
                ////会导致错误堆栈不方便观察,但是也没办法直接调用,只能手写一些常用类型
                //{
                //    //需要从Delegate转换成实际类型执行的帮助类
                //    Action<int> abc = _this as Action<int>;
                //    abc((int)_params[0]);
                //    return null;
                //}
                //else
                {
                    return(method_System.Invoke(_this, _params));
                }
            }
        }
        public object Invoke(ThreadContext context, object _this, object[] _params)
        {
            bool flag = _this is CLRSharp_Instance;

            if (flag)
            {
                CLRSharp_Instance cLRSharp_Instance = _this as CLRSharp_Instance;
                bool hasSysBase = cLRSharp_Instance.type.HasSysBase;
                if (hasSysBase)
                {
                    bool flag2 = cLRSharp_Instance.type.ContainBase(this.method_System.DeclaringType);
                    bool flag3 = flag2;
                    if (flag3)
                    {
                        ICrossBind crossBind = context.environment.GetCrossBind(this.method_System.DeclaringType);
                        bool       flag4     = crossBind != null;
                        if (flag4)
                        {
                            _this = crossBind.CreateBind(cLRSharp_Instance);
                        }
                        else
                        {
                            _this = (_this as CLRSharp_Instance).system_base;
                        }
                    }
                }
            }
            bool   flag5 = this.method_System is ConstructorInfo;
            object result;

            if (flag5)
            {
                bool flag6 = this.method_System.DeclaringType.IsSubclassOf(typeof(Delegate));
                if (flag6)
                {
                    object         obj            = _params[0];
                    RefFunc        refFunc        = _params[1] as RefFunc;
                    ICLRType_Sharp iCLRType_Sharp = refFunc._method.DeclaringType as ICLRType_Sharp;
                    bool           flag7          = iCLRType_Sharp != null;
                    if (flag7)
                    {
                        CLRSharp_Instance cLRSharp_Instance2 = obj as CLRSharp_Instance;
                        bool flag8 = refFunc._method.isStatic && iCLRType_Sharp != null;
                        if (flag8)
                        {
                            cLRSharp_Instance2 = iCLRType_Sharp.staticInstance;
                        }
                        result = cLRSharp_Instance2.GetDelegate(context, this.method_System.DeclaringType, refFunc._method);
                    }
                    else
                    {
                        ICLRType_System iCLRType_System = refFunc._method.DeclaringType as ICLRType_System;
                        result = iCLRType_System.CreateDelegate(this.method_System.DeclaringType, obj, refFunc._method as IMethod_System);
                    }
                }
                else
                {
                    object[] array = null;
                    bool     flag9 = _params != null && _params.Length != 0;
                    if (flag9)
                    {
                        array = new object[_params.Length];
                        ParameterInfo[] parameters = this.method_System.GetParameters();
                        for (int i = 0; i < _params.Length; i++)
                        {
                            bool flag10 = _params[i] == null;
                            if (flag10)
                            {
                                array[i] = null;
                            }
                            Type type          = _params[i].GetType();
                            Type parameterType = parameters[i].ParameterType;
                            bool flag11        = type == parameterType;
                            if (flag11)
                            {
                                array[i] = _params[i];
                            }
                            else
                            {
                                bool flag12 = type.IsSubclassOf(parameterType);
                                if (flag12)
                                {
                                    array[i] = _params[i];
                                }
                                else
                                {
                                    bool isEnum = parameters[i].ParameterType.IsEnum;
                                    if (isEnum)
                                    {
                                        MethodInfo[] methods = parameters[i].ParameterType.GetMethods();
                                        array[i] = Enum.ToObject(parameters[i].ParameterType, _params[i]);
                                    }
                                    else
                                    {
                                        bool flag13 = parameterType == typeof(byte);
                                        if (flag13)
                                        {
                                            array[i] = (byte)Convert.ToDecimal(_params[i]);
                                        }
                                        else
                                        {
                                            array[i] = _params[i];
                                        }
                                    }
                                }
                            }
                        }
                    }
                    object obj2 = (this.method_System as ConstructorInfo).Invoke(array);
                    result = obj2;
                }
            }
            else
            {
                Dictionary <int, object> dictionary = new Dictionary <int, object>();
                object[] array2 = null;
                bool     flag14 = _params != null && _params.Length != 0;
                if (flag14)
                {
                    array2 = new object[_params.Length];
                    ParameterInfo[] parameters2 = this.method_System.GetParameters();
                    for (int j = 0; j < _params.Length; j++)
                    {
                        bool flag15 = _params[j] is StackFrame.RefObj;
                        if (flag15)
                        {
                            object obj3   = (_params[j] as StackFrame.RefObj).Get();
                            bool   flag16 = obj3 is VBox;
                            if (flag16)
                            {
                                obj3 = (obj3 as VBox).BoxDefine();
                            }
                            dictionary[j] = obj3;
                            array2[j]     = obj3;
                        }
                        else
                        {
                            bool isEnum2 = parameters2[j].ParameterType.IsEnum;
                            if (isEnum2)
                            {
                                MethodInfo[] methods2 = parameters2[j].ParameterType.GetMethods();
                                array2[j] = Enum.ToObject(parameters2[j].ParameterType, _params[j]);
                            }
                            else
                            {
                                bool flag17 = parameters2[j].ParameterType == typeof(ulong) && _params[j] is long;
                                if (flag17)
                                {
                                    array2[j] = (ulong)((long)_params[j]);
                                }
                                else
                                {
                                    bool flag18 = parameters2[j].ParameterType == typeof(uint) && _params[j] is int;
                                    if (flag18)
                                    {
                                        array2[j] = (uint)((int)_params[j]);
                                    }
                                    else
                                    {
                                        array2[j] = _params[j];
                                    }
                                }
                            }
                        }
                    }
                }
                object obj4 = this.method_System.Invoke(_this, array2);
                foreach (KeyValuePair <int, object> current in dictionary)
                {
                    bool flag19 = current.Value is VBox;
                    if (flag19)
                    {
                        (current.Value as VBox).SetDirect(array2[current.Key]);
                    }
                    else
                    {
                        (_params[current.Key] as StackFrame.RefObj).Set(array2[current.Key]);
                    }
                }
                result = obj4;
            }
            return(result);
        }