反射帮助类更新2015/11/07
来源:互联网 发布:高通kryo 知乎 编辑:程序博客网 时间:2024/06/15 01:49
运行结果
控制器ESApiController
using Panda.Lib;using Panda.Models;using System;using System.Collections.Generic;using System.Data;using System.Linq;using System.Reflection;using System.Web;using System.Web.Mvc;namespace Panda.Controllers{ public class ESApiController : Controller { // // GET: /ESApi/ EasyExpressContexts db = new EasyExpressContexts(); public ActionResult Index() { return View(); } public ActionResult Tool(string objName = "T_Users", string nameSpace = "Panda.Models") { ViewData["Properties"] = ReflectionHelper.GetProperties(objName, nameSpace); ViewBag.Tables = ReflectionHelper.GetProperties("EasyExpressContexts",nameSpace); ViewBag.Title = "Api测试页"; ViewBag.FormAction = "/ESApi/Tool"; ViewBag.objName = objName; ViewBag.nameSpace = nameSpace; return View("Tool"); } [HttpPost] public ActionResult Tool() { int result = 0; string objName = Request.Form["objName"].ToString(); string nameSpace = Request.Form["nameSpace"].ToString(); string method = Request.Form["method"].ToString(); // //ReflectionHelper.SelectSql(ReflectionHelper.GetFormObject(objName, nameSpace), new string[] { "RoleID" }, new string[] { "RoleID", "RoleName" }); if (method == "1")//添加 { //查到原来的数据【条件索引为1 查已存在的名字】 object old = db.Database.SqlQuery<object>(ReflectionHelper.SelectSql(ReflectionHelper.GetFormObject(objName, nameSpace), new int[] { 0 })).FirstOrDefault(); if (old == null) { db.Entry(ReflectionHelper.GetFormObject(objName, nameSpace)).State = EntityState.Added; result = db.SaveChanges(); } } else if (method == "2")//删除 { var formObj = ReflectionHelper.GetFormObject(objName, nameSpace); //查到原来的数据【条件索引为0 查已存在的ID】 var old = db.Database.SqlQuery(formObj.GetType(), ReflectionHelper.SelectSql(formObj, new int[] { 0 })); if (old != null) { foreach (var item in old) { db.Entry(item).State = EntityState.Deleted; // break; } result = db.SaveChanges(); } } else if (method == "3")//修改 { //查到原来的数据【条件索引为0 查已存在的ID】 object old = db.Database.SqlQuery<object>(ReflectionHelper.SelectSql(ReflectionHelper.GetFormObject(objName, nameSpace), new int[] { 0 })).FirstOrDefault(); if (old != null) { //保存新数据 db.Entry(ReflectionHelper.GetFormObject(objName, nameSpace)).State = EntityState.Modified; result = db.SaveChanges(); } } else { ; } ViewBag.Values = ReflectionHelper.GetFormInfo(objName,nameSpace)[1]; ViewData["Properties"] = ReflectionHelper.GetProperties(objName,nameSpace); ViewBag.Tables = new string[] { objName }; ViewBag.Title = "Api测试页-查看"; return View("Tool"); } }}
视图
@{ string[] Properties = (string[])ViewData["Properties"];}<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head> <script src="~/Scripts/Bootstrap/jquery.js"></script> <script src="~/Scripts/Bootstrap/bootstrap.min.js"></script> <link href="~/Content/Bootstrap/bootstrap.min.css" rel="stylesheet" /> <title>@ViewBag.Title</title> <script type="text/javascript"> function changeTable() { location.href = "/ESApi/Tool?objName=" + $("#TableName").val(); alert('请稍后..') } </script> <style> .top-offset-20px { margin-top: 400px; } .centerTitle { margin-left: 43%; margin-top: 20px; } </style></head><body> <h1 class="centerTitle">@ViewBag.Title</h1> <div class="control-group row col-lg-12 col-lg-offset-5 "> <div class="controls"> <label for="TableName">数据库表名</label><br/> <select onchange="changeTable()" id="TableName"> @foreach (var item in ViewBag.Tables) { <option value="@item">@item</option> } </select> </div> </div> <h4 class="centerTitle">表单信息如下</h4> <form class="form-horizontal" method="post" action="@ViewBag.FormAction"> <input name="objName" value="@ViewBag.objName" type="text" hidden="hidden" class="dfinput" /> <input name="nameSpace" value="@ViewBag.nameSpace" type="text" hidden="hidden" class="dfinput" /> @if (ViewBag.Values == null) { <div class="control-group row col-lg-12 col-lg-offset-5 "> <div class="controls"> <label for="TableName">选择操作</label><br /> <select name="method"> <option value="0">无操作</option> <option value="1">添加</option> <option value="2">删除</option> <option value="3">修改</option> </select> </div> </div> } @for (int i = 0; i < Properties.Length; i++) { if (ViewBag.Values == null) { <div class="control-group row col-lg-12 col-lg-offset-5 "> <div class="controls"> <input type="text" id="@Properties[i]" name="@Properties[i]" placeholder="@Properties[i]" /> </div> </div> } else { <div class="control-group row col-lg-12 col-lg-offset-5 "> <div class="controls"> <label for="@Properties[i]">@Properties[i]</label><br /> <input type="text" id="@Properties[i]" name="@Properties[i]" value="@ViewBag.Values[i]" placeholder="@Properties[i]" /> </div> </div> } } @if (ViewBag.Values == null) { <div class="control-group col-lg-12 col-lg-offset-5"> <div class="controls "> <button type="submit" class=" btn-success">submit</button> </div> </div> } </form> </body></html>
反射帮助类更新
using System;using System.Collections.Generic;using System.Linq;using System.Reflection;using System.Web;using System.Web.Mvc;namespace Panda.Lib{ public static class ReflectionHelper { #region 取到对象的属性集合 /// <summary> /// 取到对象的属性集合 /// </summary> /// <param name="FullobjName">对象的类全名</param> /// <returns>属性集合</returns> public static string[] GetProperties(string FullobjName) { var p = Assembly.GetExecutingAssembly().CreateInstance(FullobjName).GetType().GetProperties(); string[] result = new string[p.Length]; for (int i = 0; i < p.Length; i++) { result[i] = p[i].Name; } return result; } /// <summary> /// 取到对象的属性集合 /// </summary> /// <param name="objName">对象的类名</param> /// <param name="nameSpace">对象的命名空间</param> /// <returns>属性集合</returns> public static string[] GetProperties(string objName, string nameSpace) { return GetProperties(nameSpace + "." + objName); } #endregion #region 获取Form表单中提交的键值对 /// <summary> /// 获取Form表单中提交的键值对 /// </summary> /// <param name="FullobjName">对象的类全名</param> /// <returns></returns> public static string[][] GetFormInfo(string FullobjName) { //根据名称获取属性 string[] Properties = GetProperties(FullobjName); //存储属性值 string[] Values = new string[Properties.Length]; for (int i = 0; i < Properties.Length; i++) { Values[i] = (HttpContext.Current.Request.Form[Properties[i]] == null) ? "-1" : HttpContext.Current.Request.Form[Properties[i]]; } string[][] result = new string[][] { Properties, Values }; return result; } /// <summary> /// 获取Form表单中提交的键值对 /// </summary> /// <param name="objName">对象的类名</param> /// <param name="nameSpace">对象的命名空间</param> /// <returns></returns> public static string[][] GetFormInfo(string objName, string nameSpace) { return GetFormInfo(nameSpace + "." + objName); } #endregion #region 根据键值对转化为的对象 (实例化对象并给对象赋值) /// <summary> /// 获取表单中提交的对象 /// </summary> /// <param name="FullobjName">对象的类全名</param> /// <returns>赋值后的对象</returns> public static object GetFormObject(string FullobjName) { //实例化对象 object obj = Assembly.GetExecutingAssembly().CreateInstance(FullobjName); //获取对象属性集合 string[] Properties = GetProperties(FullobjName); //获取对象值数组 string[] Values = GetFormInfo(FullobjName)[1]; //给对象的属性挨个赋值 for (int i = 0; i < Properties.Length; i++) { var Property = obj.GetType().GetProperty(Properties[i]); //判断属性类型 if (Property.PropertyType.Name.Contains("Int")) { Property.SetValue(obj, int.Parse(Values[i])); } else if (Property.PropertyType.Name.Contains("Float")) { Property.SetValue(obj, float.Parse(Values[i])); } else { Property.SetValue(obj, Values[i]); } } return obj; } /// <summary> /// 获取表单中提交的对象 /// </summary> /// <param name="objName">对象的类名</param> /// <param name="nameSpace">对象的命名空间</param> /// <returns>赋值后的对象</returns> public static object GetFormObject(string objName, string nameSpace) { return GetFormObject(nameSpace + "." + objName); } #endregion #region 获取对象的属性键值对 /// <summary> /// 获取对象的属性键值对 /// </summary> /// <param name="obj">赋值后的对象</param> /// <returns>属性键值对 string[0]+string[1]</returns> public static string[][] GetObjInfo(object obj) { var t = obj.GetType(); var p = t.GetProperties(); string[] Properties = new string[p.Length]; string[] Values = new string[p.Length]; for (int i = 0; i < p.Length; i++) { Properties[i] = p[i].Name; var temp = t.GetProperty(p[i].Name).GetValue(obj); Values[i] = temp == null ? "未填写" : temp.ToString(); } string[][] result = new string[][] { Properties, Values }; return result; } #endregion #region 获取对象的指定属性值 /// <summary> /// 获取对象的属性值 /// </summary> /// <param name="obj">赋值后的对象</param> /// <param name="PropertieName">属性值</param> /// <returns>属性名+属性值 object[0]+object[1]</returns> public static object GetObjPropertieValue(object obj, string PropertieName) { if (obj != null) { return obj.GetType().GetProperty(PropertieName).GetValue(obj); } else { return null; } } /// <summary> /// 获取对象的属性值 /// </summary> /// <param name="obj">赋值后的对象</param> /// <param name="PropertieIndex">属性索引</param> /// <returns>属性名+属性值 object[0]+object[1]</returns> public static object GetObjPropertieValue(object obj, int PropertieIndex) { object[] result = null; if (obj != null) { result = new string[2]; var t = obj.GetType(); var p = t.GetProperties(); result[0] = p[PropertieIndex].Name; result[1]= obj.GetType().GetProperty(p[PropertieIndex].Name).GetValue(obj); return result; } else { return result; } } #endregion #region 根据对象的‘属性键值对’或‘已赋值对象’生成Sql语句 [规定表名和类名必须相同] /// <summary> /// 根据对象的‘属性键值对’生成Sql插入语句 [规定表名和类名必须相同] /// </summary> /// <param name="objName">类名</param> /// <param name="nameSpace">类命名空间</param> /// <param name="Data">对象的属性键值对</param> /// <returns>SQl插入语句</returns> public static string InsertSql(string objName, string nameSpace, string[][] Data) { //表名,默认等于类名 string tableName = objName; string colNames = "";//列名字符串 string colValues = "";//列名字符串 for (int i = 0; i < Data[0].Length; i++) { colNames += Data[0][i]; colValues += "'" + Data[1][i] + "'"; colNames += ","; colValues += ","; } char tttt = colNames[colNames.Length - 1]; if (colNames[colNames.Length - 1] == ',') { colNames = colNames.Substring(0, colNames.Length - 1); colValues = colValues.Substring(0, colValues.Length - 1); } string sql = "insert into " + tableName + "(" + colNames + ")values(" + colValues + ")"; return sql; } /// <summary> /// 根据‘已赋值对象’生成Sql插入语句 [规定表名和类名必须相同] /// </summary> /// <param name="Data">已赋值对象</param> /// <returns>SQl插入语句</returns> public static string InsertSql(object Data) { Type type = Data.GetType(); string tableName = type.Name;//表名 PropertyInfo[] Properties = type.GetProperties(); string colNames = "";//列名字符串 string colValues = "";//列名字符串 for (int i = 0; i < Properties.Length; i++) { //过滤外键 if (Properties[i].PropertyType.Name.Contains("String") || Properties[i].PropertyType.Name.Contains("Int") || Properties[i].PropertyType.Name.Contains("Float")) { colNames += Properties[i].Name; colValues += "'" + Properties[i].GetValue(Data) + "'"; colNames += ","; colValues += ","; } } char tttt = colNames[colNames.Length - 1]; if (colNames[colNames.Length - 1] == ',') { colNames = colNames.Substring(0, colNames.Length - 1); colValues = colValues.Substring(0, colValues.Length - 1); } string sql = "insert into " + tableName + "(" + colNames + ")values(" + colValues + ")"; return sql; } /// <summary> /// 根据‘已赋值对象’生成Sql查询语句 [规定表名和类名必须相同] /// </summary> /// <param name="Data">已赋值对象</param> /// <param name="conditions">条件字符串集</param> /// <param name="slectColNames">投影字符串集</param> /// <returns></returns> public static string SelectSql(object Data, string[] conditions, string[] slectColNames=null) { Type type = Data.GetType(); string tableName = type.Name;//表名 string slectColNameStr = "";//选择列字符串 if (slectColNames == null || slectColNames.Length == 0) { slectColNameStr = "*"; } else { for (int a = 0; a < slectColNames.Length; a++) { slectColNameStr += slectColNames[a]+","; } if (slectColNameStr[slectColNameStr.Length - 1] == ',') { slectColNameStr = slectColNameStr.Substring(0, slectColNameStr.Length - 1); } } string conditionStr = "";//条件字符串 for (int i = 0; i < conditions.Length; i++) { conditionStr += conditions[i] + " = " + "'" + type.GetProperty(conditions[i]).GetValue(Data) + "' And"; } if (conditionStr[conditionStr.Length - 1] == 'd') { conditionStr = conditionStr.Substring(0, conditionStr.Length - 3); } string sql = "select " + slectColNameStr + " from " + tableName + " where " + conditionStr; return sql; } /// <summary> /// 根据‘已赋值对象’生成Sql查询语句 [规定表名和类名必须相同] /// </summary> /// <param name="Data">已赋值对象</param> /// <param name="conditionIndexs">条件索引集</param> /// <param name="slectColNameIndexs">投影索引集</param> /// <returns></returns> public static string SelectSql(object Data, int[] conditionIndexs, int[] slectColNameIndexs = null) { Type type = Data.GetType(); string tableName = type.Name;//表名 PropertyInfo[] Properties = type.GetProperties(); string slectColNameStr = "";//选择列字符串 //选择列是否为空 if (slectColNameIndexs == null || slectColNameIndexs.Length == 0) { slectColNameStr = "*"; } else { for (int a = 0; a < slectColNameIndexs.Length; a++) { slectColNameStr += Properties[slectColNameIndexs[a]].Name + ","; } if (slectColNameStr[slectColNameStr.Length - 1] == ',') { slectColNameStr = slectColNameStr.Substring(0, slectColNameStr.Length - 1); } } string conditionStr = "";//条件字符串 for (int i = 0; i < conditionIndexs.Length; i++) { //过滤外键 if (Properties[conditionIndexs[i]].PropertyType.Name.Contains("String") || Properties[conditionIndexs[i]].PropertyType.Name.Contains("Int") || Properties[conditionIndexs[i]].PropertyType.Name.Contains("Float")) { conditionStr += Properties[conditionIndexs[i]].Name + " = " + "'" + Properties[conditionIndexs[i]].GetValue(Data) + "' And"; } } if (conditionStr[conditionStr.Length - 1] == 'd') { conditionStr = conditionStr.Substring(0, conditionStr.Length - 3); } string sql = "select " + slectColNameStr + " from " + tableName + " where " + conditionStr; return sql; } #endregion }}
1 0
- 反射帮助类更新2015/11/07
- 反射帮助类ReflectHelper
- android.os.SystemProperties反射帮助类
- 反射帮助ReflectionWraper
- 常用CHM帮助文档集锦下载[2007-11-18更新]
- 基于Android 下载文件时,更新UI简单帮助类
- 基于Android 下载文件时,更新UI简单帮助类
- PowerShell中更新帮助手册
- Ajax 省市联动更新,文末更新批量上传帮助类
- 反射难点 长期更新
- java连接接数据库实现数据库的添加 删除 更新 查询操作的一个帮助类
- 基于Android下载并解压Zip文件,更新UI简单帮助类
- 基于Android下载并解压Zip文件,更新UI简单帮助类
- Unity3D自学笔记——架构应用(四)JsonToEntity帮助类更新
- PHP jqGrid 表格数据更新帮助代码
- 反射常用功能-持续更新
- java反射机制基本用法(附:帮助文档)
- 反射(三)利用反射部分更新UI界面
- what happened when we don't set the namespace attrribute in Struts 2?
- 当推荐算法开源包多如牛毛,为什么我们还要专门的推荐算法工程师
- go 开发环境
- WPF 基础到企业应用系列5——WPF千年轮回 续前缘
- Caused by: java.lang.ClassNotFoundException: org.hibernate.cache.CacheProvider
- 反射帮助类更新2015/11/07
- OC内存管理分类
- hdu1671 Phone List (字典树)
- 数据绑定DBinder完全解析。轻松定制属于你自己的绑定框架。
- 维护数据库安全的方法和系统
- 一种聊天机器人系统
- 学习心得(三)
- leetcode 46:Permutations
- ubuntu14.04网易更新源