public Base_IEnumerator(CLRSharp_Instance inst) { ThreadContext activeContext = ThreadContext.activeContext; this.inst = inst; string[] methodNames = this.inst.type.GetMethodNames(); string[] array = methodNames; for (int i = 0; i < array.Length; i++) { string text = array[i]; bool flag = text.Contains("MoveNext"); if (flag) { this._MoveNext = this.inst.type.GetMethod(text, MethodParamList.constEmpty()); } bool flag2 = text.Contains(".get_Current"); if (flag2) { this._get_Current = this.inst.type.GetMethod(text, MethodParamList.constEmpty()); } bool flag3 = text.Contains(".Reset"); if (flag3) { this._Reset = this.inst.type.GetMethod(text, MethodParamList.constEmpty()); } } }
public object Get(object _this) { CLRSharp_Instance sins = null; if (_this == null) { sins = _DeclaringType.staticInstance; } else { sins = _this as CLRSharp_Instance; } object v = null; if (sins.Fields.TryGetValue(field.Name, out v)) { return(v); } v = field.FieldType.IsValueType ? (object)0 : null; sins.Fields.Add(field.Name, v); return(v); }
public Base_IEnumerator(CLRSharp_Instance inst) { var context = ThreadContext.activeContext; this.inst = inst; var ms = this.inst.type.GetMethodNames(); { var __array1 = ms; var __arrayLength1 = __array1.Length; for (int __i1 = 0; __i1 < __arrayLength1; ++__i1) { var name = (string)__array1[__i1]; { if (name.Contains("MoveNext")) { _MoveNext = this.inst.type.GetMethod(name, MethodParamList.constEmpty()); } if (name.Contains(".get_Current")) { _get_Current = this.inst.type.GetMethod(name, MethodParamList.constEmpty()); } if (name.Contains(".Reset")) { _Reset = this.inst.type.GetMethod(name, MethodParamList.constEmpty()); } } } } }
public bool IsInst(object obj) { if (obj is CLRSharp_Instance) { CLRSharp_Instance ins = obj as CLRSharp_Instance; if (ins.type == this) { return(true); } //这里还要实现继承关系 if (ins.type is ICLRType_Sharp) { Type_Common_CLRSharp sharps = ins.type as Type_Common_CLRSharp; if (sharps._Interfaces.Count > 0) { foreach (var i in sharps._Interfaces) { if (i == this) { return(true); } } } } } return(false); }
private string getDataString(CLRSharp_Instance inst) { string str = "{"; ICLRType type = CLRSharpManager.instance.getCLRType("Freamwork.ProtocolData"); bool boo = CLRSharpManager.instance.isExtend(inst.type as Type_Common_CLRSharp, type as Type_Common_CLRSharp); if(boo) { HashList<string, string> fieldInfos = CLRSharpManager.instance.Invoke(inst.type, "getFieldInfos", inst) as HashList<string, string>; string[] filedNames = fieldInfos.keys.ToArray(); for (int i = 0, len = filedNames.Length; i < len; i++) { str += getFieldString(inst.Fields[filedNames[i]], filedNames[i] + ":"); if (i < len - 1) { str += ", "; } } } else { str += inst.ToString(); } str += "}"; return str; }
public Delegate CreateDele(Type deletype, ThreadContext context, CLRSharp_Instance _this, IMethod _method) { Delegate_BindTool.Action action = delegate { _method.Invoke(context, _this, new object[0], true, true); }; return(Delegate.CreateDelegate(deletype, action.Target, action.Method)); }
public Delegate CreateDele(Type deletype, ThreadContext context, CLRSharp_Instance _this, IMethod _method) { Action act = () => { _method.Invoke(context, _this, new object[] { }, true, true); }; return(Delegate.CreateDelegate(deletype, act.Target, act.Method)); }
void InvokeMethod(CLRSharp.ThreadContext context, string type, string method) { CLRSharp.ICLRType wt = env.GetType(type); //用全名称,包括命名空间 CLRSharp.CLRSharp_Instance to = new CLRSharp.CLRSharp_Instance(wt as CLRSharp.ICLRType_Sharp); //创建实例 CLRSharp.IMethod methodctor = wt.GetMethod(".ctor", CLRSharp.MethodParamList.constEmpty()); //取得构造函数 methodctor.Invoke(context, to, null); //执行构造函数 CLRSharp.IMethod mt = wt.GetMethod(method, CLRSharp.MethodParamList.constEmpty()); mt.Invoke(context, to, null); }
public static object CreateObject(ThreadContext context, string name) { var wt = HotFixScript.env.GetType(name); CLRSharp.CLRSharp_Instance to = new CLRSharp.CLRSharp_Instance(wt as CLRSharp.ICLRType_Sharp); //创建实例 CLRSharp.IMethod methodctor = wt.GetMethod(".ctor", CLRSharp.MethodParamList.constEmpty()); //取得构造函数 methodctor.Invoke(context, to, null); //执行构造函数 return(to); }
public bool IsInst(object obj) { if (obj is CLRSharp_Instance) { CLRSharp_Instance ins = obj as CLRSharp_Instance; if (ins.type == this) { return(true); } //这里还要实现继承关系 } return(false); }
public void ResetStaticInstace() { this._staticInstance = null; foreach (MethodDefinition current in this.type_CLRSharp.Methods) { bool flag = current.Name == ".cctor"; if (flag) { this.NeedCCtor = true; break; } } }
//============================================================================================== /// <summary> /// 将实例转化为ByteBuffer /// </summary> public static ByteBuffer clrObjectToByteBuffer(CLRSharp_Instance inst) { ByteBuffer buff = new ByteBuffer(); HashList<string, string> fieldInfos = CLRSharpManager.instance.Invoke(inst.type, "getFieldInfos", inst) as HashList<string, string>; string[] filedNames = fieldInfos.keys.ToArray(); for (int i = 0, len = filedNames.Length; i < len; i++) { appendField(inst.Fields[filedNames[i]], ref buff); } return buff; }
public object Invoke(ThreadContext context, object _this, object[] _params, bool bVisual) { if (EmptyBody) { return(null); } if (context == null) { context = ThreadContext.activeContext; } if (context == null) { throw new Exception("这个线程上没有CLRSharp:ThreadContext"); } if (bVisual && method_CLRSharp.IsVirtual) { CLRSharp_Instance inst = _this as CLRSharp_Instance; if (inst.type != this.DeclaringType) { IMethod impl = inst.type.GetVMethod(this);// .GetMethod(this.Name, this.ParamList); if (impl != this) { return(impl.Invoke(context, _this, _params)); } } } if (method_CLRSharp.Name == ".ctor") { CLRSharp_Instance inst = _this as CLRSharp_Instance; if (inst == null) { inst = new CLRSharp_Instance(_DeclaringType); } //if (_DeclaringType.BaseType is ICLRType_System) context.ExecuteFunc(this, inst, _params); return(inst); } var obj = context.ExecuteFunc(this, _this, _params); if (obj is CLRSharp_Instance && ReturnType is ICLRType_System) { var bind = context.environment.GetCrossBind((ReturnType as ICLRType_System).TypeForSystem); if (bind != null) { obj = bind.CreateBind(obj as CLRSharp_Instance); } } return(obj); }
public Delegate CreateDele(Type deletype, ThreadContext context, CLRSharp_Instance _this, IMethod _method) { Action act = () => { var o = _method.Invoke(context, _this, new object[] { }, true, true); if (o is VBox) { o = (o as VBox).BoxDefine(); } return((TRet)o); }; return(Delegate.CreateDelegate(deletype, act.Target, act.Method)); }
public Delegate CreateDele(Type deletype, ThreadContext context, CLRSharp_Instance _this, IMethod _method) { Delegate_BindTool_Ret <TRet> .Action action = delegate { object obj = _method.Invoke(context, _this, new object[0], true, true); bool flag = obj is VBox; if (flag) { obj = (obj as VBox).BoxDefine(); } return((TRet)((object)obj)); }; return(Delegate.CreateDelegate(deletype, action.Target, action.Method)); }
public Base_IEnumerator(CLRSharp_Instance inst) { var context = ThreadContext.activeContext; this.inst = inst; var ms = this.inst.type.GetMethodNames(); foreach(string name in ms) { if(name.Contains("MoveNext")) _MoveNext = this.inst.type.GetMethod(name, MethodParamList.constEmpty()); if (name.Contains(".get_Current")) _get_Current = this.inst.type.GetMethod(name, MethodParamList.constEmpty()); if (name.Contains(".Reset")) _Reset = this.inst.type.GetMethod(name, MethodParamList.constEmpty()); } }
/// <summary> /// 构造函数只是为了把脚本实例传进来,用其他的形式也可以 /// </summary> /// <param name="inst"></param> public Base_IMyType(CLRSharp.CLRSharp_Instance inst) { var context = ThreadContext.activeContext; this.inst = inst; //这里先把需要的脚本方法记下来,只是为了后面调用起来方便 var ms = this.inst.type.GetMethodNames(); foreach (string name in ms) { if (name.Contains("GetName")) _GetName = this.inst.type.GetMethod(name, MethodParamList.constEmpty()); if (name.Contains("GetDesc")) _GetDesc = this.inst.type.GetMethod(name, MethodParamList.constEmpty()); if (name.Contains("SetName")) _SetName = this.inst.type.GetMethod(name, MethodParamList.Make(context.environment.GetType(typeof(string)))); } }
public void Set(object _this, object value) { CLRSharp_Instance sins = null; if (_this == null) { sins = _DeclaringType.staticInstance; } else { sins = _this as CLRSharp_Instance; } sins.Fields[field.Name] = value; }
public object Invoke(ThreadContext context, object _this, object[] _params) { if (context == null) { context = ThreadContext.activeContext; } if (context == null) { throw new Exception("这个线程上没有CLRSharp:ThreadContext"); } if (method_CLRSharp.Name == ".ctor") { CLRSharp_Instance inst = new CLRSharp_Instance(_DeclaringType); context.ExecuteFunc(method_CLRSharp, inst, _params); return(inst); } return(context.ExecuteFunc(method_CLRSharp, _this, _params)); }
public Delegate CreateDele(Type deletype, ThreadContext context, CLRSharp_Instance _this, IMethod _method) { Action act = () => { var o = _method.Invoke(context, _this, new object[] { }, true, true); if (o is VBox) { o = (o as VBox).BoxDefine(); } try { return((TRet)o); } catch (Exception ex) { var type = typeof(TRet); if (type == typeof(bool) && o is int) { return((TRet)(object)((int)o == 1)); } else { context.environment.logger.Log_Error("Delegate_BindTool_Ret<TRet> can not cast from " + o.GetType() + " to " + type + " for return value."); throw; } } }; try { return(Delegate.CreateDelegate(deletype, act.Target, act.Method)); } catch (Exception ex) { context.environment.logger.Log_Error("CreateDelegate error. " + deletype + " method:" + act.Method); throw; } }
public Base_IEnumerator(CLRSharp_Instance inst) { var context = ThreadContext.activeContext; this.inst = inst; var ms = this.inst.type.GetMethodNames(); foreach (string name in ms) { if (name.Contains("MoveNext")) { _MoveNext = this.inst.type.GetMethod(name, MethodParamList.constEmpty()); } if (name.Contains(".get_Current")) { _get_Current = this.inst.type.GetMethod(name, MethodParamList.constEmpty()); } if (name.Contains(".Reset")) { _Reset = this.inst.type.GetMethod(name, MethodParamList.constEmpty()); } } }
public object Invoke(ThreadContext context, object _this, object[] _params, bool bVisual) { if (context == null) { context = ThreadContext.activeContext; } if (context == null) { throw new Exception("这个线程上没有CLRSharp:ThreadContext"); } if (bVisual && method_CLRSharp.IsVirtual) { CLRSharp_Instance inst = _this as CLRSharp_Instance; if (inst.type != this.DeclaringType) { IMethod impl = inst.type.GetVMethod(this);// .GetMethod(this.Name, this.ParamList); if (impl != this) { return(impl.Invoke(context, _this, _params)); } } } if (method_CLRSharp.Name == ".ctor") { CLRSharp_Instance inst = _this as CLRSharp_Instance; if (inst == null) { inst = new CLRSharp_Instance(_DeclaringType); } //if (_DeclaringType.BaseType is ICLRType_System) context.ExecuteFunc(this, inst, _params); return(inst); } return(context.ExecuteFunc(this, _this, _params)); }
/// <summary> /// 构造函数只是为了把脚本实例传进来,用其他的形式也可以 /// </summary> /// <param name="inst"></param> public Base_IMyType(CLRSharp.CLRSharp_Instance inst) { var context = ThreadContext.activeContext; this.inst = inst; //这里先把需要的脚本方法记下来,只是为了后面调用起来方便 var ms = this.inst.type.GetMethodNames(); foreach (string name in ms) { if (name.Contains("GetName")) { _GetName = this.inst.type.GetMethod(name, MethodParamList.constEmpty()); } if (name.Contains("GetDesc")) { _GetDesc = this.inst.type.GetMethod(name, MethodParamList.constEmpty()); } if (name.Contains("SetName")) { _SetName = this.inst.type.GetMethod(name, MethodParamList.Make(context.environment.GetType(typeof(string)))); } } }
public bool IsInst(object obj) { bool flag = obj is CLRSharp_Instance; bool result; if (flag) { CLRSharp_Instance cLRSharp_Instance = obj as CLRSharp_Instance; bool flag2 = cLRSharp_Instance.type == this; if (flag2) { result = true; return(result); } bool flag3 = cLRSharp_Instance.type != null; if (flag3) { Type_Common_CLRSharp type_Common_CLRSharp = cLRSharp_Instance.type as Type_Common_CLRSharp; bool flag4 = type_Common_CLRSharp._Interfaces.Count > 0; if (flag4) { foreach (ICLRType current in type_Common_CLRSharp._Interfaces) { bool flag5 = current == this; if (flag5) { result = true; return(result); } } } } } result = false; return(result); }
public object CreateBind(CLRSharp_Instance inst) { return(new Base_IDisposable(inst)); }
public void ResetStaticInstace() { this._staticInstance = null; foreach (var m in this.type_CLRSharp.Methods) { if (m.Name == ".cctor") { NeedCCtor = true; break; } } }
public object Invoke(ThreadContext context, object _this, object[] _params, bool bVisual) { if (context == null) context = ThreadContext.activeContext; if (context == null) throw new Exception("这个线程上没有CLRSharp:ThreadContext"); if (bVisual && method_CLRSharp.IsVirtual) { CLRSharp_Instance inst = _this as CLRSharp_Instance; if (inst.type != this.DeclaringType) { IMethod impl = inst.type.GetVMethod(this);// .GetMethod(this.Name, this.ParamList); if (impl != this) { return impl.Invoke(context, _this, _params); } } } if (method_CLRSharp.Name == ".ctor") { CLRSharp_Instance inst = _this as CLRSharp_Instance; if (inst == null) inst = new CLRSharp_Instance(_DeclaringType); //if (_DeclaringType.BaseType is ICLRType_System) context.ExecuteFunc(this, inst, _params); return inst; } var obj = context.ExecuteFunc(this, _this, _params); if (obj is CLRSharp_Instance && ReturnType is ICLRType_System) { var bind = context.environment.GetCrossBind((ReturnType as ICLRType_System).TypeForSystem); if (bind != null) { obj = bind.CreateBind(obj as CLRSharp_Instance); } } return obj; }
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 Base_IEnumerable(CLRSharp_Instance inst) { this.inst = inst; }
public static Delegate MakeDelegate(Type deletype, CLRSharp_Instance _this_inst, IMethod __method) { IDelegate_BindTool delegate_BindTool = null; bool flag = Delegate_Binder.mapBind.TryGetValue(deletype, out delegate_BindTool); Delegate result; if (flag) { result = delegate_BindTool.CreateDele(deletype, null, _this_inst, __method); } else { MethodInfo method = deletype.GetMethod("Invoke"); bool isStatic = __method.isStatic; if (isStatic) { _this_inst = null; } ParameterInfo[] parameters = method.GetParameters(); bool flag2 = method.ReturnType == typeof(void); if (flag2) { bool flag3 = parameters.Length == 0; if (flag3) { delegate_BindTool = new Delegate_BindTool(); } else { bool flag4 = parameters.Length == 1; if (flag4) { Type type = typeof(Delegate_BindTool <>).MakeGenericType(new Type[] { parameters[0].ParameterType }); delegate_BindTool = (type.GetConstructor(new Type[0]).Invoke(new object[0]) as IDelegate_BindTool); } else { bool flag5 = parameters.Length == 2; if (flag5) { Type type2 = typeof(Delegate_BindTool <, >).MakeGenericType(new Type[] { parameters[0].ParameterType, parameters[1].ParameterType }); delegate_BindTool = (type2.GetConstructor(new Type[0]).Invoke(new object[0]) as IDelegate_BindTool); } else { bool flag6 = parameters.Length == 3; if (flag6) { Type type3 = typeof(Delegate_BindTool <, , >).MakeGenericType(new Type[] { parameters[0].ParameterType, parameters[1].ParameterType, parameters[2].ParameterType }); delegate_BindTool = (type3.GetConstructor(new Type[0]).Invoke(new object[0]) as IDelegate_BindTool); } else { bool flag7 = parameters.Length == 4; if (!flag7) { throw new Exception("还没有支持这么多参数的委托"); } Type type4 = typeof(Delegate_BindTool <, , , >).MakeGenericType(new Type[] { parameters[0].ParameterType, parameters[1].ParameterType, parameters[2].ParameterType, parameters[3].ParameterType }); delegate_BindTool = (type4.GetConstructor(new Type[0]).Invoke(new object[0]) as IDelegate_BindTool); } } } } } else { bool flag8 = parameters.Length == 0; if (flag8) { Type type5 = typeof(Delegate_BindTool_Ret <>).MakeGenericType(new Type[] { method.ReturnType }); delegate_BindTool = (type5.GetConstructor(new Type[0]).Invoke(new object[0]) as IDelegate_BindTool); } else { bool flag9 = parameters.Length == 1; if (flag9) { Type type6 = typeof(Delegate_BindTool_Ret <, >).MakeGenericType(new Type[] { method.ReturnType, parameters[0].ParameterType }); delegate_BindTool = (type6.GetConstructor(new Type[0]).Invoke(new object[0]) as IDelegate_BindTool); } else { bool flag10 = parameters.Length == 2; if (flag10) { Type type7 = typeof(Delegate_BindTool_Ret <, , >).MakeGenericType(new Type[] { method.ReturnType, parameters[0].ParameterType, parameters[1].ParameterType }); delegate_BindTool = (type7.GetConstructor(new Type[0]).Invoke(new object[0]) as IDelegate_BindTool); } else { bool flag11 = parameters.Length == 3; if (flag11) { Type type8 = typeof(Delegate_BindTool_Ret <, , , >).MakeGenericType(new Type[] { method.ReturnType, parameters[0].ParameterType, parameters[1].ParameterType, parameters[2].ParameterType }); delegate_BindTool = (type8.GetConstructor(new Type[0]).Invoke(new object[0]) as IDelegate_BindTool); } else { bool flag12 = parameters.Length == 4; if (!flag12) { throw new Exception("还没有支持这么多参数的委托"); } Type type9 = typeof(Delegate_BindTool_Ret <, , , >).MakeGenericType(new Type[] { method.ReturnType, parameters[0].ParameterType, parameters[1].ParameterType, parameters[2].ParameterType, parameters[3].ParameterType }); delegate_BindTool = (type9.GetConstructor(new Type[0]).Invoke(new object[0]) as IDelegate_BindTool); } } } } } Delegate_Binder.mapBind[deletype] = delegate_BindTool; result = delegate_BindTool.CreateDele(deletype, null, _this_inst, __method); } return(result); }
public object CreateBind(CLRSharp_Instance inst) { return new Base_IDisposable(inst); }
public Base_IDisposable(CLRSharp_Instance inst) { this.inst = inst; }
public object CreateBind(CLRSharp_Instance inst) { return(new CrossBind_IEnumerable.Base_IEnumerable(inst)); }
public object CreateBind(CLRSharp_Instance inst) { return(new Base_IEnumerator(inst)); }
public object CreateBind(CLRSharp_Instance inst) { return new Base_IEnumerator(inst); }
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); } } } }
public static Delegate MakeDelegate(Type deletype, CLRSharp_Instance _this_inst, IMethod __method) { IDelegate_BindTool btool = null; if (mapBind.TryGetValue(deletype, out btool)) { return(btool.CreateDele(deletype, null, _this_inst, __method)); } var method = deletype.GetMethod("Invoke"); if (__method.isStatic) { _this_inst = null; } var pp = method.GetParameters(); if (method.ReturnType == typeof(void)) { if (pp.Length == 0) { //var gtype = typeof(Delegate_BindTool).MakeGenericType(new Type[] { }); btool = new Delegate_BindTool(); } else if (pp.Length == 1) { var gtype = typeof(Delegate_BindTool <>).MakeGenericType(new Type[] { pp[0].ParameterType }); btool = gtype.GetConstructor(new Type[] { }).Invoke(new object[] { }) as IDelegate_BindTool; } else if (pp.Length == 2) { var gtype = typeof(Delegate_BindTool <,>).MakeGenericType(new Type[] { pp[0].ParameterType, pp[1].ParameterType }); btool = gtype.GetConstructor(new Type[] { }).Invoke(new object[] { }) as IDelegate_BindTool; } else if (pp.Length == 3) { var gtype = typeof(Delegate_BindTool <, ,>).MakeGenericType(new Type[] { pp[0].ParameterType, pp[1].ParameterType, pp[2].ParameterType }); btool = gtype.GetConstructor(new Type[] { }).Invoke(new object[] { }) as IDelegate_BindTool; } else if (pp.Length == 4) { var gtype = typeof(Delegate_BindTool <, , ,>).MakeGenericType(new Type[] { pp[0].ParameterType, pp[1].ParameterType, pp[2].ParameterType, pp[3].ParameterType }); btool = gtype.GetConstructor(new Type[] { }).Invoke(new object[] { }) as IDelegate_BindTool; } else { throw new Exception("还没有支持这么多参数的委托"); } } else { if (pp.Length == 0) { var gtype = typeof(Delegate_BindTool_Ret <>).MakeGenericType(new Type[] { method.ReturnType }); btool = gtype.GetConstructor(new Type[] { }).Invoke(new object[] { }) as IDelegate_BindTool; } else if (pp.Length == 1) { var gtype = typeof(Delegate_BindTool_Ret <,>).MakeGenericType(new Type[] { pp[0].ParameterType, method.ReturnType }); btool = gtype.GetConstructor(new Type[] { }).Invoke(new object[] { }) as IDelegate_BindTool; } else if (pp.Length == 2) { var gtype = typeof(Delegate_BindTool_Ret <, ,>).MakeGenericType(new Type[] { pp[0].ParameterType, pp[1].ParameterType, method.ReturnType }); btool = gtype.GetConstructor(new Type[] { }).Invoke(new object[] { }) as IDelegate_BindTool; } else if (pp.Length == 3) { var gtype = typeof(Delegate_BindTool_Ret <, , ,>).MakeGenericType(new Type[] { pp[0].ParameterType, pp[1].ParameterType, pp[2].ParameterType, method.ReturnType }); btool = gtype.GetConstructor(new Type[] { }).Invoke(new object[] { }) as IDelegate_BindTool; } else if (pp.Length == 4) { var gtype = typeof(Delegate_BindTool_Ret <, , , ,>).MakeGenericType(new Type[] { pp[0].ParameterType, pp[1].ParameterType, pp[2].ParameterType, pp[3].ParameterType, method.ReturnType }); btool = gtype.GetConstructor(new Type[] { }).Invoke(new object[] { }) as IDelegate_BindTool; } else if (pp.Length == 5) { var gtype = typeof(Delegate_BindTool_Ret <, , , , ,>).MakeGenericType(new Type[] { pp[0].ParameterType, pp[1].ParameterType, pp[2].ParameterType, pp[3].ParameterType, pp[4].ParameterType, method.ReturnType }); btool = gtype.GetConstructor(new Type[] { }).Invoke(new object[] { }) as IDelegate_BindTool; } else if (pp.Length == 6) { var gtype = typeof(Delegate_BindTool_Ret <, , , , , ,>).MakeGenericType(new Type[] { pp[0].ParameterType, pp[1].ParameterType, pp[2].ParameterType, pp[3].ParameterType, pp[4].ParameterType, pp[5].ParameterType, method.ReturnType }); btool = gtype.GetConstructor(new Type[] { }).Invoke(new object[] { }) as IDelegate_BindTool; } else { throw new Exception("还没有支持这么多参数的委托"); } } mapBind[deletype] = btool; return(btool.CreateDele(deletype, null, _this_inst, __method)); }
public object Invoke(ThreadContext context, object _this, object[] _params, bool bVisual) { if (context == null) context = ThreadContext.activeContext; if (context == null) throw new Exception("这个线程上没有CLRSharp:ThreadContext"); if (bVisual && method_CLRSharp.IsVirtual) { CLRSharp_Instance inst = _this as CLRSharp_Instance; if (inst.type != this.DeclaringType) { IMethod impl = inst.type.GetVMethod(this);// .GetMethod(this.Name, this.ParamList); if (impl != this) { return impl.Invoke(context, _this, _params); } } } if (method_CLRSharp.Name == ".ctor") { CLRSharp_Instance inst = _this as CLRSharp_Instance; if (inst == null) inst = new CLRSharp_Instance(_DeclaringType); //if (_DeclaringType.BaseType is ICLRType_System) context.ExecuteFunc(this, inst, _params); return inst; } return context.ExecuteFunc(this, _this, _params); }
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); } } }
/// <summary> /// 初始化时设置临时变量,勿要调用!!! /// </summary> /// <param name="clrInst"></param> public static void setProvisionalData(object clrInst) { s_clrInst = clrInst as CLRSharp_Instance; }