static void Main(string[] args) { Console.Clear(); Console.WriteLine("##### Menu: premere il numero corrispondente per accedere al test ######"); Console.WriteLine("Premi 0 - Uscita"); Console.WriteLine("Premi 1 - Triangle Categorisation."); Console.WriteLine("Premi 2 - Word Reversal."); Console.WriteLine("Premi 3 - Linked List Traversal."); Console.WriteLine(Environment.NewLine); bool isNumber = int.TryParse(Console.ReadLine(), out int intResult); if (isNumber) { Console.Clear(); switch (intResult) { case 0: Console.WriteLine("Uscita"); return; case 1: Console.WriteLine("##### Triangle Categorisation #####"); Console.WriteLine(Environment.NewLine); ActionExecute.StartTriangleCategorisation(); break; case 2: Console.WriteLine("##### Word Reversal #####"); Console.WriteLine(Environment.NewLine); ActionExecute.StartWordReversal(); break; case 3: Console.WriteLine("##### Linked List Traversal #####"); Console.WriteLine(Environment.NewLine); ActionExecute.StartLinkedListTraversal(); break; } Console.WriteLine("Premi un pulsante qualsiasi per tornare al menu."); Console.ReadKey(); Main(args); } else { Console.WriteLine("Errore: scelta non valida!"); } }
/// <summary> /// 获取执行控制器行为的代理函数 /// </summary> /// <typeparam name="TContext">Http上下文信息类型</typeparam> /// <typeparam name="TRequest">Http请求信息类型</typeparam> /// <param name="assembly">程序集</param> /// <param name="controllerType">控制器类型</param> /// <param name="actionMethodInfo">控制器行为对应方法的反射信息</param> /// <returns></returns> public static ActionExecute <TContext, TRequest> GetRunActionDelegate <TContext, TRequest>(Assembly assembly, Type controllerType, System.Reflection.MethodInfo actionMethodInfo, ActionData <TContext, TRequest> actionData) { var TContextType = typeof(TContext); if (StringContainerOpImplicitMethodInfoDic == null) { StringContainerOpImplicitMethodInfoDic = new Dictionary <RuntimeTypeHandle, System.Reflection.MethodInfo>(); System.Reflection.MethodInfo[] methodInfos = typeof(StringContainer).GetMethods(); foreach (var methodInfo in methodInfos) { if (methodInfo.Name == "op_Implicit" && methodInfo.ReturnType != typeof(StringContainer)) { StringContainerOpImplicitMethodInfoDic.Add(methodInfo.ReturnType.TypeHandle, methodInfo); } } } DynamicMethod method = new DynamicMethod("RunActionX", typeof(void), new Type[] { typeof(TContext), typeof(NFinal.Action.ActionData <TContext, TRequest>), typeof(TRequest), typeof(NameValueCollection) }); ILGenerator methodIL = method.GetILGenerator(); var methodEnd = methodIL.DefineLabel(); var request = methodIL.DeclareLocal(typeof(TRequest)); var controller = methodIL.DeclareLocal(controllerType); var defaultConstructor = controllerType.GetConstructor(Type.EmptyTypes); methodIL.Emit(OpCodes.Newobj, defaultConstructor); methodIL.Emit(OpCodes.Stloc, controller); //0.context,1.actionData,2.request,3.parameters //try methodIL.BeginExceptionBlock(); { //controller methodIL.Emit(OpCodes.Ldloc, controller); //controller,environment methodIL.Emit(OpCodes.Ldarg_0); //controller,environment,"methodName" methodIL.Emit(OpCodes.Ldstr, actionMethodInfo.Name); //controller,environment,"methodName",null methodIL.Emit(OpCodes.Ldnull); //controller,environment,"methodName",null,Request methodIL.Emit(OpCodes.Ldarg_2); //controller,environment,"methodName",null,Request,CompressMode methodIL.Emit(OpCodes.Ldc_I4, (int)CompressMode.Deflate); //controller,environment,"methodName",null,Request,CompressMode,actionData methodIL.Emit(OpCodes.Ldarg_1); //controller,environment,"methodName",null,Request,CompressMode,actionData.plugConfig methodIL.Emit(OpCodes.Ldfld, typeof(NFinal.Action.ActionData <TContext, TRequest>).GetField("plugConfig")); methodIL.Emit(OpCodes.Callvirt, controllerType.GetMethod("Initialization", new Type[] { typeof(TContext), typeof(string), typeof(System.IO.Stream), typeof(TRequest), typeof(CompressMode), typeof(NFinal.Config.Plug.PlugConfig) })); #region 执行用户权限过滤器 //actionData methodIL.Emit(OpCodes.Ldarg_1); //actionData.IAuthorizationFilters//泛型的类型必须固定 methodIL.Emit(OpCodes.Ldfld, typeof(NFinal.Action.ActionData <TContext, TRequest>).GetField("IAuthorizationFilters")); //controller methodIL.Emit(OpCodes.Ldloc, controller);//泛型的类型必须固定 methodIL.Emit(OpCodes.Call, AuthorizationFilterMethodInfo.MakeGenericMethod(new Type[] { typeof(TContext), typeof(TRequest) })); var BeforeAuthorizationEnd = methodIL.DefineLabel(); methodIL.Emit(OpCodes.Brtrue_S, BeforeAuthorizationEnd); methodIL.Emit(OpCodes.Leave, methodEnd);//离函数体较远,使用leave_s会报错。 methodIL.MarkLabel(BeforeAuthorizationEnd); #endregion #region 执行Action执行之前的过滤器 //actionData methodIL.Emit(OpCodes.Ldarg_1); //actionData.IBeforeActionFilters methodIL.Emit(OpCodes.Ldfld, typeof(NFinal.Action.ActionData <TContext, TRequest>).GetField("IBeforeActionFilters")); //controller methodIL.Emit(OpCodes.Ldloc, controller); //FilterHelper.BeforeActionFilter(); methodIL.Emit(OpCodes.Call, BeforeActionFilterMethodInfo.MakeGenericMethod(new Type[] { typeof(TContext), typeof(TRequest) })); var BeforeActionEnd = methodIL.DefineLabel(); methodIL.Emit(OpCodes.Brtrue_S, BeforeActionEnd); methodIL.Emit(OpCodes.Leave, methodEnd); methodIL.MarkLabel(BeforeActionEnd); #endregion //controller methodIL.Emit(OpCodes.Ldloc, controller); //controller.Before(); methodIL.Emit(OpCodes.Callvirt, controllerType.GetMethod("Before", Type.EmptyTypes)); //bool,0 var BeforeEnd = methodIL.DefineLabel(); methodIL.Emit(OpCodes.Brtrue_S, BeforeEnd); methodIL.Emit(OpCodes.Leave, methodEnd); methodIL.MarkLabel(BeforeEnd); //ViewBag初始化 if (true) { var ControllerViewBagFieldInfo = controllerType.GetField("ViewBag"); Type ViewBagType = Type.GetTypeFromHandle(actionData.viewBagType); //if (ControllerViewBagFieldInfo.FieldType == typeof(object)) //{ // string modelTypeName = controllerType.Namespace + "." + controllerType.Name + "_Model"; // modelTypeName += "." + actionMethodInfo.Name; // ViewBagType = assembly.GetType(modelTypeName); // if (ViewBagType == null) // { // throw new NFinal.Exceptions.ModelNotFoundException(modelTypeName); // } //} //else //{ // ViewBagType = ControllerViewBagFieldInfo.FieldType; //} var ViewBagContructorInfo = ViewBagType.GetConstructor(Type.EmptyTypes); var ViewBag = methodIL.DeclareLocal(ViewBagType); bool viewBagTypeIsExpandoObject = false; if (ViewBagType == typeof(System.Dynamic.ExpandoObject)) { viewBagTypeIsExpandoObject = true; } //controller methodIL.Emit(OpCodes.Newobj, ViewBagContructorInfo); methodIL.Emit(OpCodes.Stloc, ViewBag); List <FieldInfo> viewBagFiledInfoList = new List <FieldInfo>(); List <FieldInfo> controllerFieldInfoList = new List <FieldInfo>(); //获取所有字段 var controllerFieldInfos = controllerType.GetFields(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.FlattenHierarchy); foreach (var controllerFieldInfo in controllerFieldInfos) { //查找Controller中具有ViewBagMember特性的字段 var viewBagMemberAttribute = controllerFieldInfo.GetCustomAttributes(typeof(ViewBagMemberAttribute), true); if (viewBagMemberAttribute.Count() > 0) { if (viewBagTypeIsExpandoObject) { controllerFieldInfoList.Add(controllerFieldInfo); } else { var ViewBagFiledInfo = ViewBagType.GetField( controllerFieldInfo.Name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static); //查找到ViewBag中具有相同名字的字段 if (ViewBagFiledInfo != null && ViewBagFiledInfo.FieldType == controllerFieldInfo.FieldType) { viewBagFiledInfoList.Add(ViewBagFiledInfo); controllerFieldInfoList.Add(controllerFieldInfo); } } } } List <MethodInfo> controllerProperyInfoGetMethodList = new List <MethodInfo>(); List <MethodInfo> viewBagPropertyInfoSetMethodList = new List <MethodInfo>(); var controllerPropertyInfos = controllerType.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.FlattenHierarchy); foreach (var controllerPropertyInfo in controllerPropertyInfos) { var viewBagMemberAttribute = controllerPropertyInfo.GetCustomAttributes(typeof(ViewBagMemberAttribute), true); if (viewBagMemberAttribute.Count() > 0) { if (viewBagTypeIsExpandoObject) { controllerProperyInfoGetMethodList.Add(controllerPropertyInfo.GetGetMethod()); } else { var viewBagPropertyInfo = ViewBagType.GetProperty( controllerPropertyInfo.Name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static); if (viewBagPropertyInfo != null && viewBagPropertyInfo.PropertyType == controllerPropertyInfo.PropertyType) { MethodInfo controllerPropertyInfoGetMethod = controllerPropertyInfo.GetGetMethod(); MethodInfo viewBagPropertyInfoSetMethod = viewBagPropertyInfo.GetSetMethod(); controllerProperyInfoGetMethodList.Add(controllerPropertyInfoGetMethod); viewBagPropertyInfoSetMethodList.Add(viewBagPropertyInfoSetMethod); } } } } Type viewBagOperateStaticType = null; if (viewBagTypeIsExpandoObject) { } for (int i = 0; i < viewBagFiledInfoList.Count; i++) { FieldInfo controllerFieldInfo = controllerFieldInfoList[i]; FieldInfo viewBagFiledInfo = viewBagFiledInfoList[i]; if (controllerFieldInfo.IsStatic) { methodIL.Emit(OpCodes.Ldloc, ViewBag); methodIL.Emit(OpCodes.Ldsfld, controllerFieldInfo); methodIL.Emit(OpCodes.Stfld, viewBagFiledInfo); } else { //赋值操作 methodIL.Emit(OpCodes.Ldloc, ViewBag); methodIL.Emit(OpCodes.Ldloc, controller); methodIL.Emit(OpCodes.Ldfld, controllerFieldInfo); methodIL.Emit(OpCodes.Stfld, viewBagFiledInfo); } } for (int i = 0; i < viewBagPropertyInfoSetMethodList.Count; i++) { MethodInfo controllerPropertyInfoGetMethod = controllerProperyInfoGetMethodList[i]; MethodInfo viewBagPropertyInfoSetMethod = viewBagPropertyInfoSetMethodList[i]; if (controllerPropertyInfoGetMethod.IsStatic) { methodIL.Emit(OpCodes.Ldloc, ViewBag); methodIL.Emit(OpCodes.Call, controllerPropertyInfoGetMethod); methodIL.Emit(OpCodes.Callvirt, viewBagPropertyInfoSetMethod); } else { methodIL.Emit(OpCodes.Ldloc, ViewBag); methodIL.Emit(OpCodes.Ldloc, controller); methodIL.Emit(OpCodes.Callvirt, controllerPropertyInfoGetMethod); methodIL.Emit(OpCodes.Callvirt, viewBagPropertyInfoSetMethod); } } methodIL.Emit(OpCodes.Ldloc, controller); methodIL.Emit(OpCodes.Ldloc, ViewBag); methodIL.Emit(OpCodes.Stfld, ControllerViewBagFieldInfo); } //controller methodIL.Emit(OpCodes.Ldloc, controller); System.Reflection.ParameterInfo[] parameterInfos = actionMethodInfo.GetParameters(); //添加参数 if (parameterInfos.Length > 0) { for (int i = 0; i < parameterInfos.Length; i++) { Type parameterType = parameterInfos[i].ParameterType; if (parameterInfos[i].ParameterType #if (NET40 || NET451 || NET461) .IsGenericType #endif #if NETCORE .GetTypeInfo().IsGenericType #endif && parameterInfos[i].ParameterType.GetGenericTypeDefinition() == typeof(Nullable <>)) { parameterType = parameterInfos[i].ParameterType.GetGenericArguments()[0]; } if (parameterType == typeof(System.String) || parameterType == typeof(System.Int32) || parameterType == typeof(System.Int16) || parameterType == typeof(System.Int64) || parameterType == typeof(System.UInt32) || parameterType == typeof(System.UInt16) || parameterType == typeof(System.UInt64) || parameterType == typeof(System.Byte) || parameterType == typeof(System.SByte) || parameterType == typeof(System.Single) || parameterType == typeof(System.Double) || parameterType == typeof(System.Decimal) || parameterType == typeof(System.DateTime) || parameterType == typeof(System.DateTimeOffset) || parameterType == typeof(System.Char) || parameterType == typeof(System.Boolean) || parameterType == typeof(System.Guid)) { //parameters methodIL.Emit(OpCodes.Ldarg_3); //parameters,name methodIL.Emit(OpCodes.Ldstr, parameterInfos[i].Name); //parameters[name] methodIL.Emit(OpCodes.Callvirt, nameValueCollectionGetItemMethodInfo); //ParameterType op_Implicit(request.parameters[name]);隐式转换 methodIL.Emit(OpCodes.Call, StringContainerOpImplicitMethodInfoDic[parameterInfos[i].ParameterType.TypeHandle]); } else { //new ParameterModel(); //methodIL.Emit(OpCodes.Newobj, parameterInfos[i].ParameterType.GetConstructor(Type.EmptyTypes)); //parameters methodIL.Emit(OpCodes.Ldarg_3); //ModelHelper.GetModel(new ParameterModel(),parameters); methodIL.Emit(OpCodes.Call, modelHelperGetModelMethodInfo.MakeGenericMethod(parameterType)); } } } //controller.Action(par1,par2,par3.....); methodIL.Emit(OpCodes.Callvirt, actionMethodInfo); //controller methodIL.Emit(OpCodes.Ldloc, controller); //controller.After(); methodIL.Emit(OpCodes.Callvirt, controllerType.GetMethod("After", Type.EmptyTypes)); #region 执行Action执行之后的过滤器 //actionData methodIL.Emit(OpCodes.Ldarg_1); //actionData.IBeforeActionFilters methodIL.Emit(OpCodes.Ldfld, typeof(NFinal.Action.ActionData <TContext, TRequest>).GetField("IAfterActionFilters")); //controller methodIL.Emit(OpCodes.Ldloc, controller); //FilterHelper.BeforeActionFilter(); methodIL.Emit(OpCodes.Call, AfterActionFilterMethodInfo.MakeGenericMethod(new Type[] { typeof(TContext), typeof(TRequest) })); var AfterActionEnd = methodIL.DefineLabel(); methodIL.Emit(OpCodes.Brtrue_S, AfterActionEnd); methodIL.Emit(OpCodes.Leave, methodEnd); methodIL.MarkLabel(AfterActionEnd); #endregion //actionData methodIL.Emit(OpCodes.Ldarg_1); //actionData.IResponseFilters methodIL.Emit(OpCodes.Ldfld, typeof(NFinal.Action.ActionData <TContext, TRequest>).GetField("IResponseFilters")); //actionData.IResponseFilters,controller methodIL.Emit(OpCodes.Ldloc, controller); //actionData.IResponseFilters,controller.response methodIL.Emit(OpCodes.Callvirt, controllerType.GetProperty("response").GetGetMethod()); //FilterHelper.Filter(actionData.IResponseFilters,controller.response) methodIL.Emit(OpCodes.Call, ResponseFiltersMethodInfo); var ifResponseFiltersEnd = methodIL.DefineLabel(); methodIL.Emit(OpCodes.Brtrue_S, ifResponseFiltersEnd); methodIL.Emit(OpCodes.Leave, methodEnd); methodIL.MarkLabel(ifResponseFiltersEnd); //controller methodIL.Emit(OpCodes.Ldloc, controller); //controller.Close(); methodIL.Emit(OpCodes.Callvirt, controllerType.GetMethod("Close")); //跳出异常 methodIL.Emit(OpCodes.Leave, methodEnd); } if (!Config.Configration.globalConfig.debug.enable) { methodIL.BeginCatchBlock(typeof(System.Exception)); { methodIL.Emit(OpCodes.Throw); } } //finally methodIL.BeginFinallyBlock(); { var finallyEnd = methodIL.DefineLabel(); //controller methodIL.Emit(OpCodes.Ldloc, controller); //controller==null methodIL.Emit(OpCodes.Brfalse_S, finallyEnd); //controller methodIL.Emit(OpCodes.Ldloc, controller); //controller.Dispose(); methodIL.Emit(OpCodes.Callvirt, controllerType.GetMethod("Dispose", Type.EmptyTypes)); //methodIL.Emit(OpCodes.Callvirt,typeof(System.IDisposable).GetMethod("Dispose",Type.EmptyTypes)); methodIL.MarkLabel(finallyEnd); } //end methodIL.EndExceptionBlock(); methodIL.MarkLabel(methodEnd); methodIL.Emit(OpCodes.Ret); ActionExecute <TContext, TRequest> getRunActionDelegate = (ActionExecute <TContext, TRequest>)method.CreateDelegate(typeof(ActionExecute <TContext, TRequest>)); return(getRunActionDelegate); }