public static ModelSample GetModelTest(NFinal.NameValueCollection parameters) { ModelSample sample = new Model.ModelHelper.ModelSample(); sample.a = parameters["a"]; sample.b = parameters["b"]; ModelSample.c = parameters["c"]; sample.d = parameters["d"]; sample.e = parameters["e"]; return(sample); }
/// <summary> /// 获取将parameters中的参数复制进自定义Model的函数代理 /// </summary> /// <typeparam name="TModel"></typeparam> /// <param name="parameters"></param> /// <returns></returns> public static Delegate GetDelegate <TModel>(NFinal.NameValueCollection parameters) { System.Reflection.MethodInfo[] methodInfos = typeof(StringContainer).GetMethods(); foreach (var methodInfo in methodInfos) { if (methodInfo.Name == "op_Implicit" && methodInfo.ReturnType != typeof(StringContainer)) { StringContainerOpImplicitMethodInfoDic.TryAdd(methodInfo.ReturnType.TypeHandle, methodInfo); } } Delegate getModelDelegate = null; Type modelType = typeof(TModel); DynamicMethod method = new DynamicMethod("GetModelX", modelType, new Type[] { typeof(NFinal.NameValueCollection) }); ILGenerator methodIL = method.GetILGenerator(); var model = methodIL.DeclareLocal(modelType); var result = methodIL.DeclareLocal(modelType); var modelConstructor = modelType.GetConstructor(Type.EmptyTypes); //methodIL.Emit(OpCodes.Nop); methodIL.Emit(OpCodes.Newobj, modelConstructor); methodIL.Emit(OpCodes.Stloc, model); //var localStringVar = methodIL.DeclareLocal(typeof(string)); Type parametersType = typeof(NameValueCollection); var parametersGetItem = parametersType.GetMethod("get_Item"); System.Reflection.PropertyInfo[] modelProperties = modelType.GetProperties(); for (int i = 0; i < modelProperties.Length; i++) { ConvertPropertyString(methodIL, model, modelProperties[i], parametersGetItem); } System.Reflection.FieldInfo[] modelFieldInfos = modelType.GetFields(); for (int i = 0; i < modelFieldInfos.Length; i++) { ConvertFieldString(methodIL, model, modelFieldInfos[i], parametersGetItem); } //model放入栈顶 var methodEnd = methodIL.DefineLabel(); methodIL.Emit(OpCodes.Ldloc, model); methodIL.Emit(OpCodes.Stloc, result); methodIL.Emit(OpCodes.Br_S, methodEnd); methodIL.MarkLabel(methodEnd); methodIL.Emit(OpCodes.Ldloc, result); methodIL.Emit(OpCodes.Ret); getModelDelegate = method.CreateDelegate(typeof(GetModelDelegate <>).MakeGenericType(typeof(TModel))); return(getModelDelegate); }
/// <summary> /// 获取自定义类型,并把prameters中的值复制到相应字段中 /// </summary> /// <typeparam name="TModel"></typeparam> /// <param name="parameters"></param> /// <returns></returns> public static TModel GetModel <TModel>(NFinal.NameValueCollection parameters) { GetModelDelegateData getModelDelegateData; if (!GetModelDictionary.TryGetValue(typeof(TModel).TypeHandle, out getModelDelegateData)) { getModelDelegateData.modelType = typeof(TModel).TypeHandle; getModelDelegateData.getModelDelegate = GetDelegate <TModel>(parameters); GetModelDictionary.TryAdd(typeof(TModel).TypeHandle, getModelDelegateData); } TModel model = ((GetModelDelegate <TModel>)getModelDelegateData.getModelDelegate)(parameters); return(model); }
/// <summary> /// Http参数解析函数 /// </summary> /// <param name="requestMethod"></param> /// <param name="contentTypeString"></param> /// <param name="requestQueryString"></param> /// <param name="requestBody"></param> /// <returns></returns> public NameValueCollection GetParameters(string requestMethod, string contentTypeString, string requestQueryString, Stream requestBody) { //请求参数 var parameters = new NFinal.NameValueCollection(); MethodType methodType = MethodType.NONE; //获取POST方法 switch (requestMethod) { case NFinal.Constant.MethodTypePOST: methodType = MethodType.POST; break; case NFinal.Constant.MethodTypeGET: methodType = MethodType.GET; break; case NFinal.Constant.MethodTypeDELETE: methodType = MethodType.DELETE; break; case NFinal.Constant.MethodTypePUT: methodType = MethodType.PUT; break; case NFinal.Constant.MethodTypeAJAX: methodType = MethodType.AJAX; break; default: methodType = MethodType.NONE; break; } //提取内容类型 if (methodType == MethodType.POST) { ContentType contentType = ContentType.NONE; switch (contentTypeString.Split(NFinal.Constant.CharSemicolon)[0]) { case NFinal.Constant.ContentType_Multipart_form_data: contentType = ContentType.Multipart_form_data; break; case NFinal.Constant.ContentType_Text_json: case NFinal.Constant.ContentType_Application_json: contentType = ContentType.Application_json; break; case NFinal.Constant.ContentType_Application_x_www_form_urlencoded: contentType = ContentType.Application_x_www_form_urlencoded; break; case NFinal.Constant.ContentType_Application_xml: case NFinal.Constant.ContentType_Text_xml: contentType = ContentType.Text_xml; break; default: contentType = ContentType.NONE; break; } //提取form参数 if (requestBody != System.IO.Stream.Null) { if (contentType == ContentType.Application_x_www_form_urlencoded) { System.IO.StreamReader sr = new System.IO.StreamReader(requestBody); string body = sr.ReadToEnd(); sr.Dispose(); if (!string.IsNullOrEmpty(body)) { string[] formArray = body.Split(NFinal.Constant.CharAnd, NFinal.Constant.CharEqual); if (formArray.Length > 1 && (formArray.Length & 1) == 0) { for (int i = 0; i < formArray.Length; i += 2) { parameters.Add(formArray[i], formArray[i + 1].UrlDecode()); } } } } //else if (contentType == NFinal.ContentType.Text_xml) //{ // System.IO.StreamReader sr = new System.IO.StreamReader(stream); // string body = sr.ReadToEnd(); // sr.Dispose(); // if (!string.IsNullOrEmpty(body)) // { // System.Xml.XmlDocument doc = new System.Xml.XmlDocument(); // doc.LoadXml(body); // if (doc.DocumentElement != null) // { // foreach (System.Xml.XmlElement xmlNode in doc.DocumentElement.ChildNodes) // { // get.Add(xmlNode.Name, xmlNode.Value); // } // } // } //} else if (contentType == ContentType.Application_json) { System.IO.StreamReader sr = new System.IO.StreamReader(requestBody); string body = sr.ReadToEnd(); sr.Dispose(); NFinal.Json.IJsonSerialize serializable = new NFinal.Json.NewtonsoftJsonSerialize(); IDictionary <string, object> data = serializable.DeserializeObject <IDictionary <string, object> >(body); foreach (var ele in data) { parameters.Add(ele.Key, ele.Value.ToString()); } } //else if (contentType == ContentType.Multipart_form_data) //{ // //multipart/form-data // string boundary = NFinal.Http.HttpMultipart.HttpMultipart.boundaryReg.Match(contentTypeString).Value; // var multipart = new NFinal.Http.HttpMultipart.HttpMultipart(requestBody, boundary); // files = new NFinal.Collections.FastDictionary<string, NFinal.Http.HttpMultipart.HttpFile>(StringComparer.Ordinal); // foreach (var httpMultipartBoundary in multipart.GetBoundaries()) // { // if (string.IsNullOrEmpty(httpMultipartBoundary.Filename)) // { // string name = httpMultipartBoundary.Name; // if (!string.IsNullOrEmpty(name)) // { // string value = new System.IO.StreamReader(httpMultipartBoundary.Value).ReadToEnd(); // parameters.Add(name, value); // } // } // else // { // files.Add(httpMultipartBoundary.Name, new NFinal.Http.HttpMultipart.HttpFile(httpMultipartBoundary)); // } // } //} //提取URL?后的参数 if (requestQueryString.Length > 0) { string[] queryArray = requestQueryString.Split(NFinal.Constant.CharAnd, NFinal.Constant.CharEqual); if (queryArray.Length > 1 && (queryArray.Length & 1) == 0) { for (int i = 0; i < queryArray.Length; i += 2) { parameters.Add(queryArray[i], queryArray[i + 1].UrlDecode()); } } } } } return(parameters); }
public static ValidObject GetValidObject(this NFinal.NameValueCollection nvc, string name) { return(new ValidObject(name, nvc[name])); }