protected override bool InitInternal(MethodInfo method) { delegat = null; int cnt = paramDefinitions.Count; if (instanceDef.paramMode != ParamMode.Undefined) { cnt++; } if (resultDef.paramMode != ParamMode.Undefined) { cnt++; } delegateParams = new UniversalDelegateParam[cnt]; int i = 0; if (instanceDef.paramMode != ParamMode.Undefined) { tmpTypes[0] = instanceDef.paramType; delegateParams[i] = (UniversalDelegateParam)typeof(UniversalDelegateParam <>).RTMakeGenericType(tmpTypes).CreateObject(); delegateParams[i].paramDef = instanceDef; i++; } if (resultDef.paramMode != ParamMode.Undefined) { tmpTypes[0] = resultDef.paramType; delegateParams[i] = (UniversalDelegateParam)typeof(UniversalDelegateParam <>).RTMakeGenericType(tmpTypes).CreateObject(); delegateParams[i].paramDef = resultDef; i++; } for (int j = 0; j <= paramDefinitions.Count - 1; j++) { if (options.exposeParams && paramDefinitions[j].isParamsArray) { tmpTypes2[0] = paramDefinitions[j].paramType; tmpTypes2[1] = paramDefinitions[j].arrayType; delegateParams[i] = (UniversalDelegateParam)typeof(UniversalDelegateParam <,>).RTMakeGenericType(tmpTypes2).CreateObject(); delegateParams[i].paramsArrayNeeded = options.exposeParams; delegateParams[i].paramsArrayCount = options.exposedParamsCount; } else { tmpTypes[0] = paramDefinitions[j].paramType; delegateParams[i] = (UniversalDelegateParam)typeof(UniversalDelegateParam <>).RTMakeGenericType(tmpTypes).CreateObject(); } delegateParams[i].paramDef = paramDefinitions[j]; i++; } try { CreateDelegat(); } catch { return(false); } return(true); }
private void UpdateProgressBarIncrementValue() { if (InvokeRequired) { var upbiv = new UniversalDelegate(UpdateProgressBarIncrementValue); Invoke(upbiv); } else { progressBar.Value++; } }
protected override bool InitInternal(FieldInfo field) { isConstant = field.IsStatic && field.IsReadOnly(); if (isConstant) { delegateParams = new UniversalDelegateParam[1]; tmpTypes[0] = field.FieldType; delegateParams[0] = (UniversalDelegateParam)typeof(UniversalDelegateParam <>).RTMakeGenericType(tmpTypes).CreateObject(); delegateParams[0].paramDef = resultDef; delegateParams[0].SetFromValue(field.GetValue(null)); return(true); } getDelegat = null; setDelegat = null; int cnt = 0; if (instanceDef.paramMode != ParamMode.Undefined) { cnt++; } if (resultDef.paramMode != ParamMode.Undefined) { cnt++; } delegateParams = new UniversalDelegateParam[cnt]; int i = 0; if (instanceDef.paramMode != ParamMode.Undefined) { tmpTypes[0] = instanceDef.paramType; delegateParams[i] = (UniversalDelegateParam)typeof(UniversalDelegateParam <>).RTMakeGenericType(tmpTypes).CreateObject(); delegateParams[i].paramDef = instanceDef; i++; } if (resultDef.paramMode != ParamMode.Undefined) { tmpTypes[0] = resultDef.paramType; delegateParams[i] = (UniversalDelegateParam)typeof(UniversalDelegateParam <>).RTMakeGenericType(tmpTypes).CreateObject(); delegateParams[i].paramDef = resultDef; } try { CreateDelegates(); } catch { return(false); } return(true); }
public static Delegate CreateDelegate(Type delegateType, UniversalDelegate universalDelegate) { if (delegateType == null) { throw new ArgumentNullException("delegateType"); } if (universalDelegate == null) { throw new ArgumentNullException("universalDelegate"); } if (!typeof(Delegate).IsAssignableFrom(delegateType)) { throw new ArgumentException("Must be a delegate type", "delegateType"); } var method = delegateType.GetMethod("Invoke"); var parameters = method.GetParameters(); if (method.ReturnType == typeof(void)) { var genericAction = typeof(ActionWrappers).GetMethod("Create" + parameters.Length); var genericParams = parameters.Select(p => p.ParameterType); // Action0 is not generic var action = parameters.Length > 0 ? genericAction.MakeGenericMethod(genericParams.ToArray()) : genericAction; var actionDelegate = action.Invoke(null, new object[] { universalDelegate }); return(Delegate.CreateDelegate(delegateType, actionDelegate, "Invoke")); } else { var genericFunc = typeof(FuncWrappers).GetMethod("Create" + parameters.Length); var genericParams = parameters.Select(p => p.ParameterType) .Concat(new[] { method.ReturnType }); var func = genericFunc.MakeGenericMethod(genericParams.ToArray()); var funcDelegate = func.Invoke(null, new object[] { universalDelegate }); return(Delegate.CreateDelegate(delegateType, funcDelegate, "Invoke")); } }
public static Action <T1, T2> Create2 <T1, T2>(UniversalDelegate d) { return((a1, a2) => d(new object[] { a1, a2 })); }
public static Action <T1> Create1 <T1>(UniversalDelegate d) { return((a1) => d(new object[] { a1 })); }
public static Func <T1, T2, T3, T4, T5, TResult> Create5 <T1, T2, T3, T4, T5, TResult>(UniversalDelegate d) { return((a1, a2, a3, a4, a5) => (TResult)(d(new object[] { a1, a2, a3, a4, a5 }) ?? (object)default(TResult))); }
public static Action <T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11> Create11 <T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11>(UniversalDelegate d) { return((a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11) => d(new object[] { a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11 })); }
public static Action <T1, T2, T3, T4, T5, T6, T7> Create7 <T1, T2, T3, T4, T5, T6, T7>(UniversalDelegate d) { return((a1, a2, a3, a4, a5, a6, a7) => d(new object[] { a1, a2, a3, a4, a5, a6, a7 })); }
public static Action <T1, T2, T3, T4, T5> Create5 <T1, T2, T3, T4, T5>(UniversalDelegate d) { return((a1, a2, a3, a4, a5) => d(new object[] { a1, a2, a3, a4, a5 })); }
private void UpdateSomeControlsOnMainForm() { if (InvokeRequired) { var ud = new UniversalDelegate(UpdateSomeControlsOnMainForm); Invoke(ud); } else { panelWithProgressBar.Visible = false; if (resultBox.Items.Count != 0) { resultBox.Enabled = true; buttonDeleteAll.Enabled = true; } } }
public static Func <TResult> Create0 <TResult>(UniversalDelegate d) { return(() => (TResult)(d(new object[] {}) ?? (object)default(TResult))); }
public static Func <T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, TResult> Create16 <T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, TResult>(UniversalDelegate d) { return((a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16) => (TResult)(d(new object[] { a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16 }) ?? (object)default(TResult))); }
public static Func <T1, T2, T3, T4, T5, T6, T7, T8, T9, TResult> Create9 <T1, T2, T3, T4, T5, T6, T7, T8, T9, TResult>(UniversalDelegate d) { return((a1, a2, a3, a4, a5, a6, a7, a8, a9) => (TResult)(d(new object[] { a1, a2, a3, a4, a5, a6, a7, a8, a9 }) ?? (object)default(TResult))); }
public static Action <T1, T2, T3> Create3 <T1, T2, T3>(UniversalDelegate d) { return((a1, a2, a3) => d(new object[] { a1, a2, a3 })); }
public static Action <T1, T2, T3, T4> Create4 <T1, T2, T3, T4>(UniversalDelegate d) { return((a1, a2, a3, a4) => d(new object[] { a1, a2, a3, a4 })); }
private void CreateDelegates() { var key = ReflectedNodesHelper.GetGeneratedKey(fieldInfo); if (!GetDelegates.TryGetValue(key, out getDelegat) || getDelegat == null) { DynamicMethod dynamicMethod = new DynamicMethod(fieldInfo.Name + "_DynamicGet", null, DynParamTypes, typeof(JitFieldNode)); ILGenerator ilGen = dynamicMethod.GetILGenerator(); int instanceId = -1; int returnId = -1; int localIds = 0; int instanceLocId = -1; int returnLocId = -1; for (int i = 0; i <= delegateParams.Length - 1; i++) { var param = delegateParams[i]; var def = param.paramDef; if (def.paramMode == ParamMode.Instance) { instanceId = i; ilGen.DeclareLocal(def.paramType); instanceLocId = localIds; localIds++; } if (def.paramMode == ParamMode.Result) { returnId = i; ilGen.DeclareLocal(def.paramType); returnLocId = localIds; localIds++; } } if (instanceId >= 0) { //load first argument of method to stack, in our situation it "delegateParams" array ilGen.Emit(OpCodes.Ldarg, 0); //load current array index to stack ilGen.Emit(OpCodes.Ldc_I4, instanceId); //load reference for delegateParams[i] to stack ilGen.Emit(OpCodes.Ldelem_Ref); //load value for delegateParams[i].value to stack ilGen.Emit(OpCodes.Ldfld, delegateParams[instanceId].ValueField); //set local variable to loaded value ilGen.Emit(OpCodes.Stloc, instanceLocId); //load local var for get field value ilGen.Emit(OpCodes.Ldloc, instanceLocId); ilGen.Emit(OpCodes.Ldfld, fieldInfo); } else { ilGen.Emit(OpCodes.Ldsfld, fieldInfo); } if (returnId >= 0) { //Set local var to value of target field ilGen.Emit(OpCodes.Stloc, returnLocId); //load first argument of method to stack, in our situation it "delegateParams" array ilGen.Emit(OpCodes.Ldarg, 0); //load current array index to stack ilGen.Emit(OpCodes.Ldc_I4, returnId); //load reference for delegateParams[i] to stack ilGen.Emit(OpCodes.Ldelem_Ref); //load value for local val ilGen.Emit(OpCodes.Ldloc, returnLocId); //set local variable to loaded value ilGen.Emit(OpCodes.Stfld, delegateParams[returnId].ValueField); } ilGen.Emit(OpCodes.Ret); getDelegat = (UniversalDelegate)dynamicMethod.CreateDelegate(typeof(UniversalDelegate)); GetDelegates[key] = getDelegat; } if ((!SetDelegates.TryGetValue(key, out setDelegat) || setDelegat == null) && !fieldInfo.IsReadOnly()) { DynamicMethod dynamicMethod = new DynamicMethod(fieldInfo.Name + "_DynamicSet", null, DynParamTypes, typeof(JitFieldNode)); ILGenerator ilGen = dynamicMethod.GetILGenerator(); int instanceId = -1; int valueId = -1; int localIds = 0; int instanceLocId = -1; int valueLocId = -1; for (int i = 0; i <= delegateParams.Length - 1; i++) { var param = delegateParams[i]; var def = param.paramDef; //declare local variables in method if (def.paramMode == ParamMode.Instance) { instanceId = i; ilGen.DeclareLocal(def.paramType); instanceLocId = localIds; localIds++; } if (def.paramMode == ParamMode.Result) { valueId = i; ilGen.DeclareLocal(def.paramType); valueLocId = localIds; localIds++; } } if (instanceId >= 0) { //load first argument of method to stack, in our situation it "delegateParams" array ilGen.Emit(OpCodes.Ldarg, 0); //load current array index to stack ilGen.Emit(OpCodes.Ldc_I4, instanceId); //load reference for delegateParams[i] to stack ilGen.Emit(OpCodes.Ldelem_Ref); //load value for delegateParams[i].value to stack ilGen.Emit(OpCodes.Ldfld, delegateParams[instanceId].ValueField); //set local variable to loaded value ilGen.Emit(OpCodes.Stloc, instanceLocId); } if (valueId >= 0) { //load first argument of method to stack, in our situation it "delegateParams" array ilGen.Emit(OpCodes.Ldarg, 0); //load current array index to stack ilGen.Emit(OpCodes.Ldc_I4, valueId); //load reference for delegateParams[i] to stack ilGen.Emit(OpCodes.Ldelem_Ref); //load value for delegateParams[i].value to stack ilGen.Emit(OpCodes.Ldfld, delegateParams[valueId].ValueField); //set local variable to loaded value ilGen.Emit(OpCodes.Stloc, valueLocId); if (instanceId >= 0) { //set instance for set field value ilGen.Emit(delegateParams[instanceId].GetCurrentType().RTIsValueType() ? OpCodes.Ldloca : OpCodes.Ldloc, instanceLocId); //set new value for field ilGen.Emit(OpCodes.Ldloc, valueLocId); //set field value ilGen.Emit(OpCodes.Stfld, fieldInfo); } else { //set new value for field ilGen.Emit(OpCodes.Ldloc, valueLocId); //set field value ilGen.Emit(OpCodes.Stsfld, fieldInfo); } } if (instanceId >= 0) { //load first argument of method to stack, in our situation it "delegateParams" array ilGen.Emit(OpCodes.Ldarg, 0); //load current array index to stack ilGen.Emit(OpCodes.Ldc_I4, instanceId); //load reference for delegateParams[i] to stack ilGen.Emit(OpCodes.Ldelem_Ref); //load value for local val ilGen.Emit(OpCodes.Ldloc, instanceLocId); //set local variable to loaded value ilGen.Emit(OpCodes.Stfld, delegateParams[instanceId].ValueField); } ilGen.Emit(OpCodes.Ret); setDelegat = (UniversalDelegate)dynamicMethod.CreateDelegate(typeof(UniversalDelegate)); SetDelegates[key] = setDelegat; } }
public static Action <T1, T2, T3, T4, T5, T6> Create6 <T1, T2, T3, T4, T5, T6>(UniversalDelegate d) { return((a1, a2, a3, a4, a5, a6) => d(new object[] { a1, a2, a3, a4, a5, a6 })); }
private void CreateDelegat() { var key = ReflectedNodesHelper.GetGeneratedKey(constructorInfo); if (Delegates.TryGetValue(key, out delegat) && delegat != null) { return; } DynamicMethod dynamicMethod = new DynamicMethod("Constructor_Dynamic", null, DynParamTypes, typeof(JitMethodNode)); ILGenerator ilGen = dynamicMethod.GetILGenerator(); int returnId = -1; for (int i = 0; i <= delegateParams.Length - 1; i++) { var param = delegateParams[i]; var def = param.paramDef; //declare local variables in method ilGen.DeclareLocal(param.GetCurrentType()); if (def.paramMode == ParamMode.Result) { returnId = i; } } //store values to local variables for (int i = 0; i <= delegateParams.Length - 1; i++) { var param = delegateParams[i]; //load first argument of method to stack, in our situation it "delegateParams" array ilGen.Emit(OpCodes.Ldarg, 0); //load current array index to stack ilGen.Emit(OpCodes.Ldc_I4, i); //load reference for delegateParams[i] to stack ilGen.Emit(OpCodes.Ldelem_Ref); //load value for delegateParams[i].value to stack ilGen.Emit(OpCodes.Ldfld, param.ValueField); //set local variable to loaded value ilGen.Emit(OpCodes.Stloc, i); } for (int i = 0; i <= delegateParams.Length - 1; i++) { var param = delegateParams[i]; var def = param.paramDef; if (def.paramMode != ParamMode.Instance && def.paramMode != ParamMode.Result) { if (def.paramMode == ParamMode.In) { //load local variable for call if it simple param ilGen.Emit(OpCodes.Ldloc, i); } else { //load reference to local variable for call if it out or ref param ilGen.Emit(OpCodes.Ldloca, i); } } } ilGen.Emit(OpCodes.Newobj, constructorInfo); if (returnId >= 0) { //set result of code to local var ilGen.Emit(OpCodes.Stloc, returnId); } //return local vals to our array for (int i = 0; i <= delegateParams.Length - 1; i++) { var param = delegateParams[i]; //load first argument of method to stack, in our sytuation it "delegateParams" array ilGen.Emit(OpCodes.Ldarg, 0); //load current array index to stack ilGen.Emit(OpCodes.Ldc_I4, i); //load reference for delegateParams[i] to stack ilGen.Emit(OpCodes.Ldelem_Ref); //load value for local val ilGen.Emit(OpCodes.Ldloc, i); //set local variable to loaded value ilGen.Emit(OpCodes.Stfld, param.ValueField); } ilGen.Emit(OpCodes.Ret); delegat = (UniversalDelegate)dynamicMethod.CreateDelegate(typeof(UniversalDelegate)); Delegates[key] = delegat; }
public static Action <T1, T2, T3, T4, T5, T6, T7, T8, T9> Create9 <T1, T2, T3, T4, T5, T6, T7, T8, T9>(UniversalDelegate d) { return((a1, a2, a3, a4, a5, a6, a7, a8, a9) => d(new object[] { a1, a2, a3, a4, a5, a6, a7, a8, a9 })); }
/// <summary> /// Creates a delegate of the specified type that wraps a UniversalDelegate. /// </summary> /// <typeparam name="T">The delegate type.</typeparam> /// <param name="universalDelegate">The universal delegate to be wrapped.</param> /// <returns></returns> public static T Create <T>(UniversalDelegate universalDelegate) where T : class { return((T)(object)CreateDelegate(typeof(T), universalDelegate)); }
public static Action <T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16> Create16 <T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16>(UniversalDelegate d) { return((a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16) => d(new object[] { a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16 })); }
public static Action Create0(UniversalDelegate d) { return(() => d(new object[] {})); }
private void CreateDelegat() { var key = ReflectedNodesHelper.GetGeneratedKey(methodInfo); if (Delegates.TryGetValue(key, out delegat) && delegat != null) { return; } DynamicMethod dynamicMethod = new DynamicMethod(methodInfo.Name + "_Dynamic", null, DynParamTypes, typeof(JitMethodNode)); ILGenerator ilGen = dynamicMethod.GetILGenerator(); int instanceId = -1; int returnId = -1; for (int i = 0; i <= delegateParams.Length - 1; i++) { var param = delegateParams[i]; var def = param.paramDef; //declare local variables in method ilGen.DeclareLocal(param.GetCurrentType()); if (def.paramMode == ParamMode.Instance) { instanceId = i; } if (def.paramMode == ParamMode.Result) { returnId = i; } } //store values to local variables for (int i = 0; i <= delegateParams.Length - 1; i++) { var param = delegateParams[i]; //load first argument of method to stack, in our situation it "delegateParams" array ilGen.Emit(OpCodes.Ldarg, 0); //load current array index to stack ilGen.Emit(OpCodes.Ldc_I4, i); //load reference for delegateParams[i] to stack ilGen.Emit(OpCodes.Ldelem_Ref); //load value for delegateParams[i].value to stack ilGen.Emit(OpCodes.Ldfld, param.ValueField); //set local variable to loaded value ilGen.Emit(OpCodes.Stloc, i); } if (instanceId >= 0) { //load instance local to stack for use call on it ilGen.Emit(delegateParams[instanceId].GetCurrentType().RTIsValueType() ? OpCodes.Ldloca : OpCodes.Ldloc, instanceId); } for (int i = 0; i <= delegateParams.Length - 1; i++) { var param = delegateParams[i]; var def = param.paramDef; if (def.paramMode != ParamMode.Instance && def.paramMode != ParamMode.Result) { ilGen.Emit(def.paramMode == ParamMode.In ? OpCodes.Ldloc : OpCodes.Ldloca, i); } } if (instanceId < 0 || delegateParams[instanceId].GetCurrentType().RTIsValueType()) { //use Call opcode, because value types and statics methods cannot be virtual or overrided, result (if exist) will stored into stack ilGen.Emit(OpCodes.Call, methodInfo); } else { //use Callvirt opcode, because non value types and non statics methods can be virtual or overrided, result (if exist) will stored into stack ilGen.Emit(OpCodes.Callvirt, methodInfo); } if (returnId >= 0) { //set result of code to loacal var ilGen.Emit(OpCodes.Stloc, returnId); } //return local vals to our array for (int i = 0; i <= delegateParams.Length - 1; i++) { var param = delegateParams[i]; //load first argument of method to stack, in our sytuation it "delegateParams" array ilGen.Emit(OpCodes.Ldarg, 0); //load current array index to stack ilGen.Emit(OpCodes.Ldc_I4, i); //load reference for delegateParams[i] to stack ilGen.Emit(OpCodes.Ldelem_Ref); //load value for local val ilGen.Emit(OpCodes.Ldloc, i); //set local variable to loaded value ilGen.Emit(OpCodes.Stfld, param.ValueField); } ilGen.Emit(OpCodes.Ret); delegat = (UniversalDelegate)dynamicMethod.CreateDelegate(typeof(UniversalDelegate)); Delegates[key] = delegat; }
public static Func <T1, T2, TResult> Create2 <T1, T2, TResult>(UniversalDelegate d) { return((a1, a2) => (TResult)(d(new object[] { a1, a2 }) ?? (object)default(TResult))); }