打通前后台
一段时间全在用dojo做开发。没有用到服务端控件。怎么把前台的数据提交到后台。submit. no,刷新很糟。那就ajax吧。每个页面写一堆类似于ashx的请求链接,no,太麻烦。一个ashx搞定。包括了单据提交。切换到纯js提交,也是一样的。中间用到了csla.net
public class uieditserver : IHttpHandler
{
public static List<string> businessnames = new List<string>();
static uieditserver()
{
string businessnameconfig = (ConfigurationManager.AppSettings["businessnames"] ?? "").Trim();
string[] businessnameconfigs = businessnameconfig.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
foreach (var item in businessnameconfigs)
{
businessnames.Add(item);
}
}
private static Type createExeType(string cmdobj)
{
Type createtype = null;
foreach (var createtypename in businessnames)
{
createtype = Type.GetType(string.Format(createtypename, cmdobj));
if (createtype != null)
{
break;
}
}
if (createtype == null)
{
throw new Exception("此命令对象不存在");
}
return createtype;
}
public void ProcessRequest(HttpContext context)
{
string operation = context.Request["operation"] ?? "";
bool isview = (context.Request["view"] ?? "").Trim().Length > 0;
string outtxt = "";
try
{
checkuserrole(context);
string[] cmds = operation.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries);
//Type createtype = Type.GetType("YHWebCore.Business." + cmds[0] + ", YHWebCore");
Type createtype = createExeType(cmds[0]);
//if (createtype == null)
//{
// throw new Exception("此命令对象不存在");
//}
object coditionobject = null;
string inputStream = new StreamReader(context.Request.InputStream).ReadToEnd();
if (cmds.Length == 2 || (cmds.Length > 2 && cmds[0] == cmds[2]))//条件对象一样的时候
{
coditionobject = Csla.Reflection.MethodCaller.CallFactoryMethod(createtype, "New");
//coditionobject = Csla.Reflection.MethodCaller.CreateInstance(createtype);
}
if (cmds.Length > 2 && cmds[0] != cmds[2])
{
//Type coditiontype = Type.GetType("yzjxc.Business." + cmds[2] + ", yzjxcV1.Business");
Type coditiontype = createExeType(cmds[2]);
if (createtype == null)
{
throw new Exception("此命令对象不存在");
}
coditionobject = Csla.Reflection.MethodCaller.CallFactoryMethod(createtype, "New");
//coditionobject = Csla.Reflection.MethodCaller.CreateInstance(coditiontype);
//coditionobject = JsonConvert.DeserializeObject(inputStream, coditiontype);
}
object fs = null;
if (cmds[1] == "Builder")//
{
fs = Csla.Reflection.MethodCaller.CreateInstance(createtype);
}
else if (cmds[1] == "CreateObject")
{
fs = coditionobject;
}
else if (cmds[1] == "FindPage"
|| cmds[1] == "FindPage2")//分页
{
NameValueCollection condtions = new NameValueCollection();
condtions.Add(context.Request.Form);
condtions.Add(context.Request.QueryString);
condtions.Add(context.Request.Headers);
fs = Csla.Reflection.MethodCaller.CallFactoryMethod(createtype, cmds[1], new object[] { condtions });
var allcount = Csla.Reflection.MethodCaller.CallPropertyGetter(fs, "TotalRowCount");
var allcount2 = Csla.Reflection.MethodCaller.CallPropertyGetter(fs, "Count");
//分页处理
int skip = 0;
int take = 0;
string orderbyasc = "";
string orderbydesc = "";
WH.Business.Utilities.DojoPageUtility.pageorderpasers(condtions, out take, out skip,
out orderbyasc, out orderbydesc);
if ((int)allcount2 > (long)allcount)
{
allcount = allcount2;
}
if ((int)allcount2 > 0)
{
allcount2 = (int)allcount2 - 1;
}
context.Response.Headers.Add("Content-Range", "items " + skip.ToString() + "-" + allcount2 + "/" + allcount.ToString());
}
else if (cmds[1] == "DeleteAll"
|| cmds[1] == "FetchOne"
|| cmds[1] == "FindListByType"
|| cmds[1] == "FetchKeyOne")//删除全部
{
NameValueCollection condtions = new NameValueCollection();
condtions.Add(context.Request.Form);
condtions.Add(context.Request.QueryString);
fs = Csla.Reflection.MethodCaller.CallFactoryMethod(createtype, cmds[1], new object[] { condtions });
}
else
{
string dojojsondata = (context.Request.Params["dojojsondata"] ?? "").Trim();
if (dojojsondata.Length > 0)
{
JObject js = JObject.Parse(dojojsondata);
var oldobj = fecholdobj(coditionobject.GetType(), js);
//if (cmds[0] == "user" && cmds[1] == "Save")
//{
// if (oldobj != null)
// {
// password = ((yzjxc.Business.user)oldobj).user_password;
// }
//}
coditionobject = createnewobj(coditionobject, oldobj, js);
//if (oldobj != null && !((bool)Csla.Reflection.MethodCaller.CallPropertyGetter(oldobj, "IsNew")))
//setobj(coditionobject, oldobj);
//setsameobj(coditionobject);
//coditionobject = JsonConvert.DeserializeObject(dojojsondata, coditionobject.GetType());
}
else
{
UIUtility.UIHelper.FormToClass(coditionobject, context.Request.Form);//对象值
UIUtility.UIHelper.FormToClass(coditionobject, context.Request.QueryString);//对象值
}
//FormToClass(coditionobject, context.Request.Form);//对象值
//FormToClass(coditionobject, context.Request.QueryString);//对象值
//var sss = JsonConvert.DeserializeObject<WhereQueryParamters>(((carinfos)coditionobject).carno);
//var ms = wheresql(sss);
if (cmds[1] == "Save" || cmds[1] == "Confirme")
{
string brokenRules = "";
var isvalid = Csla.Reflection.MethodCaller.CallPropertyGetter(coditionobject, "IsValid");
if (!((bool)isvalid))
{
var getbrokenrules = Csla.Reflection.MethodCaller.CallMethod(coditionobject, "GetBrokenRules") as Csla.Rules.BrokenRulesCollection;
foreach (var brokenRule in getbrokenrules)
{
var lambdaBrokenRule = brokenRule;
var friendlyName =
Csla.Core.FieldManager.PropertyInfoManager.GetRegisteredProperties(coditionobject.GetType()).Find(
c => c.Name == lambdaBrokenRule.Property).FriendlyName;
brokenRules += string.Format("{0}: {1}{2}", friendlyName, brokenRule, Environment.NewLine);
}
throw new Exception(brokenRules);
}
}
if (cmds[1] == "Confirme" || cmds[1] == "Cancel")
{
if (Csla.Reflection.MethodCaller.IsMethodImplemented(coditionobject, cmds[1]))
{
useriddentity museriddentity = HttpContext.Current.User.Identity as useriddentity;
if (museriddentity == null)
{
throw new Exception("未登录");
}
var lateBound = new LateBoundObject(coditionobject);
fs = lateBound.CallMethod(cmds[1], int.Parse(museriddentity.Name));
//Csla.Reflection.MethodCaller.CallMethodIfImplemented(coditionobject, "Save");
}
else
{
throw new Exception("无法执行!");
//fs = Csla.Reflection.MethodCaller.CallFactoryMethod(coditionobject.GetType(), cmds[1]);
}
}
else
{
if (cmds.Length == 2)
{
if (Csla.Reflection.MethodCaller.IsMethodImplemented(coditionobject, cmds[1]))
{
var lateBound = new LateBoundObject(coditionobject);
fs = lateBound.CallMethod(cmds[1]);
}
else
{
fs = Csla.Reflection.MethodCaller.CallFactoryMethod(coditionobject.GetType(), cmds[1]);
}
}
if (cmds.Length > 2)
{
if (Csla.Reflection.MethodCaller.IsMethodImplemented(coditionobject, cmds[1]))
{
var lateBound = new LateBoundObject(createtype);
fs = lateBound.CallMethod(cmds[1], coditionobject);
//Csla.Reflection.MethodCaller.CallMethodIfImplemented(coditionobject, "Save");
}
else
{
fs = Csla.Reflection.MethodCaller.CallFactoryMethod(createtype, cmds[1], coditionobject);
}
}
}
}
string uiview = "";
if (fs is Csla.Core.IBusinessObject)
{
if (isview)
{
// uiview = Template.Parse(@"
//<script type=""text/javascript"">
//function uiloaddatas(){
// uiarray = new Object();
// uiarray[""vaild_date""] = { canwrite: false };
// uiarray[""branch_id""] = { canwrite: false };
// uiarray[""phone""] = { canwrite: false };
// uiarray[""contacts""] = { canwrite: false };
// uiarray[""memo""] = { canwrite: false };
// candoarray = new Object();
// candoarray[""toolbar1.save""] = { enable: false, canexecute: false };
// candoarray[""toolbar1.confirme""] = { enable: false, canexecute: false };
// candoarray[""toolbar1.cancel""] = { enable: false, canexecute: false };
// candoarray[""toolbar2.newitem""] = { enable: false, canexecute: false };
// candoarray[""toolbar2.deleteitem""] = { enable: false, canexecute: false };
// detailarray = new Object(); detailarray[""grid""] = { canwrite: false };
// {% if Model.uopartios.addlag && Model.orderbill.IsNew %}
// detailarray[""grid""] = { canwrite: true }; candoarray[""toolbar1.save""] = { enable: false, canexecute: false };
// uiarray[""vaild_date""] = { canwrite: true };
// uiarray[""branch_id""] = { canwrite: true };
// uiarray[""phone""] = { canwrite: true };
// uiarray[""contacts""] = { canwrite: true };
// uiarray[""memo""] = { canwrite: true };
// candoarray[""toolbar2.newitem""] = { enable: true, canexecute: true };
// candoarray[""toolbar2.deleteitem""] = { enable: false, canexecute: false };
// {% elsif Model.uopartios.modifyflag && Model.orderbill.IsNew == false && Model.orderbill.confirmflag == false %}
// candoarray[""toolbar1.save""] = { enable: false, canexecute: false };
// uiarray[""vaild_date""] = { canwrite: true };
// candoarray[""toolbar2.newitem""] = { enable: true, canexecute: true };
// candoarray[""toolbar2.deleteitem""] = { enable: true, canexecute: true };
// {% elsif Model.uopartios.modifyflag && Model.orderbill.IsNew == false && Model.orderbill.confirmflag %}
// candoarray[""toolbar1.confirme""] = { enable: false, canexecute: false };
// {% endif %}
// //alert(""sss"");
// }
// uiloaddatas();
//</script >").Render(Hash.FromAnonymousObject(new
// {
// Model = new
// {
// orderbill = new
// {
// IsNew = (bool)MethodCaller.CallPropertyGetter(coditionobject, "IsNew"),
// confirmflag = true//(bool)MethodCaller.CallPropertyGetter(coditionobject, "confirmflag"),
// },
// currentuser = context.User.Identity as useriddentity,
// uopartios = new
// {
// openflag = true,
// addlag = true,
// modifyflag = true,
// delflag = true,
// confirmflag = true,
// exportflag = true,
// printflag = true
// }
// }
// }));
var localpath = context.Request.UrlReferrer.LocalPath;//获取url
if (HttpContext.Current.Cache["allpages"] == null)
{
ICriteria<v_sys_menu_privilegeInfo> criteria = CriteriaFactory.Create<v_sys_menu_privilegeInfo>();
v_sys_menu_privilegeInfoList mpagelist = v_sys_menu_privilegeInfoList.Fetch(criteria);
if (mpagelist.Count > 0)
{
}
HttpContext.Current.Cache.Insert("allpages", mpagelist, null,
DateTime.Now.AddMinutes(20), System.Web.Caching.Cache.NoSlidingExpiration);
}
v_sys_menu_privilegeInfoList allpagelist = HttpContext.Current.Cache["allpages"] as v_sys_menu_privilegeInfoList;
var getonepage = allpagelist.FirstOrDefault(q => q.pagename.IndexOf("~" + localpath) > -1);
if (getonepage != null && (getonepage.pagename ?? "").Trim().Length > 0)
{
var uo = CriteriaFactory.Create<bo_view>() as ICriteria<bo_view>;
int operationid = 0;
if (int.TryParse(context.User.Identity.Name, out operationid))
{
uo = uo.Where(q => q.bokey == cmds[0]
&& q.pagename == "~" + localpath);
//
var uopartio = bo_view.Fetch(uo);
if (uopartio != null)
{
//uiview = Template.Parse((uopartio.viewflow ?? "").Trim()).Render(Hash.FromAnonymousObject(new
//{
// Model = new
// {
// orderbill = new
// {
// IsNew = (bool)MethodCaller.CallPropertyGetter(coditionobject, "IsNew"),
// confirmflag = (bool)MethodCaller.CallPropertyGetter(coditionobject, "confirmflag"),
// },
// currentuser = context.User.Identity as useriddentity
// }
//}));
//uiview = Razor.Parse((uopartio.viewcontent ?? "").Trim(), new { billui = fs, currentuser = context.User.Identity as useriddentity });
}
}
}
//uiview =
}
//if (fs is System.Collections.IList)
//{
if (Csla.Reflection.MethodCaller.IsMethodImplemented(fs, "ToDics"))
{
fs = Csla.Reflection.MethodCaller.CallMethod(fs, "ToDics");
}
else
{
fs = WH.Business.Utilities.DojoPageUtility.uidics.ToDics(fs);
}
//}
//else
//{
// Dictionary<string, object> dic = new Dictionary<string, object>();
// Csla.Data.DataMapper.Map(fs, dic);
// fs = dic;
//}
}
var returndata = new
{
ErrorNo = 0,
ErrorInfo = "",
Data = fs == null ? (object)"" : fs,
View = uiview
};
var timeConverter = new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" };
outtxt = JsonConvert.SerializeObject(returndata, Formatting.Indented, timeConverter);
}
catch (Csla.Reflection.CallMethodException ex)
{
string errorinfo = "";
//if (ex.InnerException is ArgumentException)
//{
// errorinfo = ModelValidator.ShowUIError(ex.InnerException as ArgumentException);
//}
if ((errorinfo ?? "").Trim().Length == 0)
{
errorinfo = ex.InnerException != null ? ex.InnerException.Message : ex.Message;
}
var ap = new
{
ErrorInfo = errorinfo,
ErrorNo = 1,
Data = "",
};
var timeConverter = new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" };
outtxt = JsonConvert.SerializeObject(ap, Formatting.Indented, timeConverter);
}
catch (Exception ex)
{
var ap = new
{
ErrorInfo = ex.Message,
ErrorNo = 1,
Data = "",
};
var timeConverter = new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" };
outtxt = JsonConvert.SerializeObject(ap, Formatting.Indented, timeConverter);
}
context.Response.Write(outtxt ?? "");
}
//private static void createnewobj(object coditionobject,JObject js)
//{
// //if (coditionobject != null && !((bool)Csla.Reflection.MethodCaller.CallPropertyGetter(coditionobject, "IsNew")))//非新对象
// //{
// //}
// var createtypes =coditionobject.GetType().GetProperties();// createtype.GetProperties();
// //fecholdobj(coditionobject, js);
// foreach (var item in createtypes)
// {
// object m = null;
// if (item.Name == "operater")
// {
// //value = Csla.Utilities.CoerceValue(pType, vType, oldValue, value);
// }
// else if (item.Name == "operdate")
// {
// m = DateTime.Now;
// }
// if (m != null)
// {
// Csla.Reflection.MethodCaller.CallPropertySetter(coditionobject, item.Name, m);
// continue;
// }
// JToken outvalue = null;
// if (js.TryGetValue(item.Name, out outvalue))
// {
// Type vType = Csla.Utilities.GetPropertyType(item.PropertyType);
// if (js[item.Name].Type == JTokenType.Array)
// {
// var sonobj = Csla.Reflection.MethodCaller.CallPropertyGetter(coditionobject, item.Name);
// //IList childs = sonobj as IList;
// //for (int childindex = 0; childindex < childs.Count; childindex++)
// //{
// // childs[childindex]
// // //var
// //}
// var sonType = Csla.Utilities.GetChildItemType(vType);
// var sj = JArray.Parse(js[item.Name].ToString());
// //var jarray = js[item.Name].ToArray();// JArray.Parse(js[item.Name].ToString());
// foreach (JObject sonitem in sj)
// {
// var sonitemobj = Csla.Reflection.MethodCaller.CallMethod(sonobj, "AddNew");
// createnewobj(sonitemobj, sonitem);
// //Csla.Reflection.MethodCaller.CallFactoryMethod(vType, "CreateChild");
// //Csla.Reflection.MethodCaller.CallMethod(sonobj, "Add", sonitemobj);
// //Csla.Reflection.MethodCaller.CallMethod(sonobj,"Add",)
// }
// //createnewobj(vType, sonobj, js[item.Name].ToArray());
// //处理子对象
// }
// else
// {
// m = js[item.Name].ToObject(vType);
// //object m = js[item.Name].ToObject(vType);
// //value = Csla.Utilities.CoerceValue(pType, vType, oldValue, value);
// Csla.Reflection.MethodCaller.CallPropertySetter(coditionobject, item.Name, m);
// }
// }
// }
//}
private static object createnewobj(object coditionobject, object tempobj, JObject js)
{
object oldobj = null;
if (tempobj != null && !((bool)Csla.Reflection.MethodCaller.CallPropertyGetter(tempobj, "IsNew")))
{
oldobj = tempobj;
}
else
{
oldobj = coditionobject;
}
//if (coditionobject != null && !((bool)Csla.Reflection.MethodCaller.CallPropertyGetter(coditionobject, "IsNew")))//非新对象
//{
//}
var createtypes = coditionobject.GetType().GetProperties();// createtype.GetProperties();
//fecholdobj(coditionobject, js);
foreach (var item in createtypes)
{
object m = null;
if (item.Name == "operater")
{
useriddentity museriddentity = HttpContext.Current.User.Identity as useriddentity;
if (museriddentity == null)
{
throw new Exception("未登录");
}
m = int.Parse(museriddentity.Name);
//value = Csla.Utilities.CoerceValue(pType, vType, oldValue, value);
}
else if (item.Name == "operdate")
{
Type vType = Csla.Utilities.GetPropertyType(item.PropertyType);
if (vType == typeof(DateTime))
m = DateTime.Now;
else
m = new SmartDate(DateTime.Now);
}
//if (item.Name == "user_password"
// && (tempobj != null
// && !((bool)Csla.Reflection.MethodCaller.CallPropertyGetter(tempobj, "IsNew")))
// && coditionobject)
//{
//}
if (m != null)
{
Csla.Reflection.MethodCaller.CallPropertySetter(oldobj, item.Name, m);
continue;
}
JToken outvalue = null;
if (js.TryGetValue(item.Name, out outvalue))
{
Type vType = Csla.Utilities.GetPropertyType(item.PropertyType);
if (js[item.Name].Type == JTokenType.Array)
{
var sonobj = Csla.Reflection.MethodCaller.CallPropertyGetter(oldobj, item.Name);//数组的问题
var sonobjlist = sonobj as IList;
var sonType = Csla.Utilities.GetChildItemType(vType);
var sj = JArray.Parse(js[item.Name].ToString());
//var jarray = js[item.Name].ToArray();// JArray.Parse(js[item.Name].ToString());
bool isobjnew = true;
foreach (JObject sonitem in sj)
{
//当前值是否已有
foreach (var onesonitemobj in sonobjlist)
{
if (onesonitemobj != null
&& ((bool)Csla.Reflection.MethodCaller.CallPropertyGetter(onesonitemobj, "IsNew")))
{
continue;
}
try
{
if (isamekeyobj(onesonitemobj, sonitem))
{
createnewobj(onesonitemobj, onesonitemobj, sonitem);
isobjnew = false;
break;
}
}
catch (Exception ex)
{
var ss = onesonitemobj;
var yy = sonitem;
throw ex;
}
}
if (isobjnew)
{
var sonitemobj = Csla.Reflection.MethodCaller.CallMethod(sonobjlist, "AddNew");
createnewobj(sonitemobj, null, sonitem);
}
if (!isobjnew)
{
isobjnew = true;
}
}
foreach (var onesonitemobj in sonobjlist)
{
if (onesonitemobj != null
&& !((bool)Csla.Reflection.MethodCaller.CallPropertyGetter(onesonitemobj, "IsDirty")))
{
Csla.Reflection.MethodCaller.CallMethod(onesonitemobj, "MarkDeleted");
}
}
}
else
{
if (item.GetCustomAttributes(typeof(System.ComponentModel.DataObjectFieldAttribute), false).Length > 0)
{
continue;
}
if (!item.CanWrite)
{
continue;
}
if (vType == typeof(SmartDate))
{
m = new SmartDate((DateTime)js[item.Name].ToObject(typeof(DateTime)));
}
else
{
m = js[item.Name].ToObject(vType);
}
Csla.Reflection.MethodCaller.CallPropertySetter(oldobj, item.Name, m);
}
}
}
return oldobj;
}
private static object fecholditemobj(Type coditiontye, JObject js)
{
return "";
//var keys = coditiontye.GetProperties().Where(q =>
// q.GetCustomAttributes(typeof(System.ComponentModel.DataObjectFieldAttribute), false).Length > 0);//获取key
//NameValueCollection condtions = new NameValueCollection();
//string values = "";
////fs = Csla.Reflection.MethodCaller.CallFactoryMethod(createtype, cmds[1], new object[] { condtions });
//foreach (var item in keys)
//{
// JToken outvalue = null;
// if (js.TryGetValue(item.Name, out outvalue))
// {
// Type vType = Csla.Utilities.GetPropertyType(item.PropertyType);
// var keyvalue = js[item.Name].ToObject(vType);
// if (keyvalue == null)
// {
// return null;
// }
// else
// {
// values += ("," + keyvalue.ToString());
// }
// }
//}
}
private static object fecholdobj(Type coditiontye, JObject js)
{
var keys = coditiontye.GetProperties().Where(q =>
q.GetCustomAttributes(typeof(System.ComponentModel.DataObjectFieldAttribute), false).Length > 0);//获取key
NameValueCollection condtions = new NameValueCollection();
string values = "";
//fs = Csla.Reflection.MethodCaller.CallFactoryMethod(createtype, cmds[1], new object[] { condtions });
foreach (var item in keys)
{
JToken outvalue = null;
if (js.TryGetValue(item.Name, out outvalue))
{
Type vType = Csla.Utilities.GetPropertyType(item.PropertyType);
var keyvalue = js[item.Name].ToObject(vType);
if (keyvalue == null)
{
return null;
}
else
{
values += ("," + keyvalue.ToString());
}
}
}
condtions.Add("key", values.Remove(0, 1));
return Csla.Reflection.MethodCaller.CallFactoryMethod(coditiontye, "FetchOne", new object[] { condtions });
}
//private static bool isamekeyobj(object oldobj, object newobj)
//{
// var coditiontype = oldobj.GetType();
// var keys = coditiontype.GetProperties().Where(q =>
// q.GetCustomAttributes(typeof(System.ComponentModel.DataObjectFieldAttribute), false).Length > 0);//获取key
// foreach (var item in keys)
// {
// var obj1 = Csla.Reflection.MethodCaller.CallPropertyGetter(oldobj, item.Name);
// var obj2 = Csla.Reflection.MethodCaller.CallPropertyGetter(newobj, item.Name);
// if (!obj1.Equals(obj2))
// return false;
// }
// return true;
//}
//private static void setobj(object newobj, object oldobj)
//{
// var createtypes = newobj.GetType().GetProperties();// createtype.GetProperties();
// foreach (var item in createtypes)
// {
// var obj = Csla.Reflection.MethodCaller.CallPropertyGetter(newobj, item.Name);
// if (obj is IList)
// {
// //正反对比,获取要删除的数据
// var oldlist = Csla.Reflection.MethodCaller.CallPropertyGetter(oldobj, item.Name) as IList;
// var newlist = Csla.Reflection.MethodCaller.CallPropertyGetter(newobj, item.Name) as IList;
// //delete alls
// bool isdelete = true;
// foreach (var olditem in oldlist)
// {
// foreach (var newitem in newlist)
// {
// if (isamekeyobj(olditem, newitem))
// {
// isdelete = false;//赋值
// setobj(newitem, oldobj);
// break;
// }
// if (isdelete)
// {
// isdelete = false;
// Csla.Reflection.MethodCaller.CallMethod(oldobj, "Remove", olditem);
// }
// }
// }
// foreach (var newitem in newlist)
// {
// foreach (var olditem in oldlist)
// {
// if (isamekeyobj(olditem, newitem))
// {
// continue;
// }
// }
// }
// }
// }
//}
private static bool isamekeyobj(object oldobj, object newobj)
{
var coditiontype = oldobj.GetType();
var keys = coditiontype.GetProperties().Where(q =>
q.GetCustomAttributes(typeof(System.ComponentModel.DataObjectFieldAttribute), false).Length > 0);//获取key
foreach (var item in keys)
{
var obj1 = Csla.Reflection.MethodCaller.CallPropertyGetter(oldobj, item.Name);
var obj2 = Csla.Reflection.MethodCaller.CallPropertyGetter(newobj, item.Name);
if (!obj1.Equals(obj2))
return false;
}
return true;
}
private static bool isamekeyobj(object oldobj, JObject js)
{
var coditiontype = oldobj.GetType();
var keys = coditiontype.GetProperties().Where(q =>
q.GetCustomAttributes(typeof(System.ComponentModel.DataObjectFieldAttribute), false).Length > 0);//获取key
foreach (var item in keys)
{
var obj1 = Csla.Reflection.MethodCaller.CallPropertyGetter(oldobj, item.Name);
Type vType = Csla.Utilities.GetPropertyType(item.PropertyType);
var obj2 = js[item.Name].ToObject(vType);
//var obj2 = Csla.Reflection.MethodCaller.CallPropertyGetter(newobj, item.Name);
if (!obj1.Equals(obj2))
return false;
}
return true;
}
private static void setobj(object newobj, object oldobj)
{
var createtypes = newobj.GetType().GetProperties();// createtype.GetProperties();
foreach (var item in createtypes)
{
var obj = Csla.Reflection.MethodCaller.CallPropertyGetter(newobj, item.Name);
if (obj is IList)
{
//正反对比,获取要删除的数据
var oldlist = Csla.Reflection.MethodCaller.CallPropertyGetter(oldobj, item.Name) as IList;
var newlist = Csla.Reflection.MethodCaller.CallPropertyGetter(newobj, item.Name) as IList;
//delete alls
bool isdelete = true;
foreach (var olditem in oldlist)
{
foreach (var newitem in newlist)
{
if (isamekeyobj(olditem, newitem))
{
isdelete = false;//赋值
setobj(newitem, oldobj);
break;
}
if (isdelete)
{
isdelete = false;
Csla.Reflection.MethodCaller.CallMethod(oldobj, "Remove", olditem);
}
}
}
foreach (var newitem in newlist)
{
foreach (var olditem in oldlist)
{
if (isamekeyobj(olditem, newitem))
{
continue;
}
}
}
}
}
//var coditiontype = oldobj.GetType();
//var keys = coditiontype.GetProperties().Where(q =>
// q.GetCustomAttributes(typeof(System.ComponentModel.DataObjectFieldAttribute), false).Length > 0);//获取key
//NameValueCollection condtions = new NameValueCollection();
//string values = "";
////fs = Csla.Reflection.MethodCaller.CallFactoryMethod(createtype, cmds[1], new object[] { condtions });
//foreach (var item in keys)
//{
// var obj1 = Csla.Reflection.MethodCaller.CallPropertyGetter(oldobj, item.Name);
// var obj2 = Csla.Reflection.MethodCaller.CallPropertyGetter(newobj, item.Name);
// if (!obj1.Equals(obj2))
// return false;
// //JToken outvalue = null;
// //if (js.TryGetValue(item.Name, out outvalue))
// //{
// // Type vType = Csla.Utilities.GetPropertyType(item.PropertyType);
// // var keyvalue = js[item.Name].ToObject(vType);
// // if (keyvalue == null)
// // {
// // return null;
// // }
// // else
// // {
// // values += ("," + keyvalue.ToString());
// // }
// //}
//}
//condtions.Add("key", values.Remove(0, 1));
//return Csla.Reflection.MethodCaller.CallFactoryMethod(coditiontye, "FetchOne", new object[] { condtions });
//if (oldobj == null)
//{
// return;
//}
//((Csla.Core.BusinessBase)newobj).ValidationComplete
//NameValueCollection condtions = new NameValueCollection();
//condtions.Add("key","");
// var oldobject = Csla.Reflection.MethodCaller.CallFactoryMethod(createtype, cmds[1], new object[] { condtions });
// var createtypes =coditionobject.GetType();
// var getproperties = createtypes.GetProperties();// createtype.GetProperties();
// foreach (var item in getproperties)
// {
// }
}
public bool IsReusable
{
get
{
return false;
}
}
private static void checkuserrole(HttpContext context)
{
if (context.User == null)
{
throw new Exception("登录已超时");
}
//useriddentity museriddentity = context.User.Identity as useriddentity;
//if (museriddentity.Name == "admin" ||
// museriddentity.rolename == "系统管理员")
//{
// return;
//}
//rolepageset mroleinfo = new rolepageset();
//Criteria<rolepageset> criteria = new Criteria<rolepageset>();
//var wherecriteria = criteria.Where(q => q.rolename == museriddentity.rolename);
//var oneroleinfo = mroleinfo.Fetch(wherecriteria);
//if (oneroleinfo.Count == 0)
//{
// throw new Exception("对不起,你没有相应的访问权限");
//}
// string method = Request["METHAD"];
//2. string ClassNamespace = method.Substring(0, method.LastIndexOf("."));// "Bss.Web.Login";
//3. object objType = CreateObject(Request["DLL"], ClassNamespace);
//4. Type ht = objType.GetType();
//5. MethodInfo methodInfo = ht.GetMethod(method.Substring(method.LastIndexOf(".") + 1));
//6. int length = Request.Form.AllKeys.Length;
//7. ParameterInfo[] paramsInfo = methodInfo.GetParameters();//得到指定方法的参数列表
//8.
//9. object[] obj = new object[length];
//10. for (int i = 0; i < length; i++)
//11. {
//12. Type tType = paramsInfo[i].ParameterType;
//13. //如果它是值类型,或者String
//14. if (tType.Equals(typeof(string)) || (!tType.IsInterface && !tType.IsClass))
//15. {
//16. //改变参数类型
//17. obj[i] = Convert.ChangeType(Request.Form[i], tType);
//18. }
//19. else if (tType.IsClass)//如果是类,将它的json字符串转换成对象
//20. {
//21. obj[i] = Newtonsoft.Json.JsonConvert.DeserializeObject(Request.Form[i], tType);
//22. }
//23. }
//24. //执行方法
//25. object returnValue = methodInfo.Invoke(objType, obj);
//var opertation = context.Request["operation"] ?? "";
//if (opertation.Trim().Length == 0)
//{
// throw new Exception("对不起,不支持空操作");
//}
//string[] cmds = opertation.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries);
//controlsetinfo mcontrolsetinfo = new controlsetinfo();
//Criteria<controlsetinfo> controlsetinfocriteria = new Criteria<controlsetinfo>();
//var wherecontrolsetinfo = controlsetinfocriteria.Where(q => q.rolename == museriddentity.rolename
// && q.controlobjectkey == cmds[0]
// && q.controlname == cmds[1]);
//var onecontrolsetinfo = mcontrolsetinfo.Fetch(wherecontrolsetinfo);
//if (onecontrolsetinfo.Count == 0)
//{
// throw new Exception("对不起,你没有相应的访问权限");
//}
//if (("," + oneroleinfo[0].rolecontrol + ",").IndexOf(',' + cmds[1].Trim() + ',') < 0)
//{
// throw new Exception("对不起,你没有相应的操作权限");
//}
}
}
查询条件的解决方案
public class uidoserver : IHttpHandler
{
public static List<string> businessnames = new List<string>();
static uidoserver()
{
string businessnameconfig = (ConfigurationManager.AppSettings["businessnames"] ?? "").Trim();
string[] businessnameconfigs = businessnameconfig.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
foreach (var item in businessnameconfigs)
{
businessnames.Add(item);
}
}
public void ProcessRequest(HttpContext context)
{
string operation = context.Request["operation"] ?? "";//操作
bool isencode = (context.Request["encode"] ?? "0") == "1";//是否需要解码
bool isview = (context.Request["view"] ?? "").Trim().Length > 0;//返回界面
string outtxt = "";
try
{
string[] cmds = operation.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries);
//获取执行类型
Type createtype = createExeType(cmds[0]);
//bool istypeexecl = false;
int paramsnum = 0;
if (cmds.Length > 2)
{
string strparamsnum = cmds[2];
int.TryParse(strparamsnum, out paramsnum);
}
//获取执行方法
System.Reflection.MethodInfo execlMethod = getExeMethod(cmds[1], createtype, paramsnum);
//权限判断
checkuserrole(context);
//为实例对象或者参数赋值
object coditionobject = null;
object objparams = null;
if (!execlMethod.IsStatic && paramsnum == 0)//实例方法,针对表单之类的bill.save(),fuck.save()之类的
{
coditionobject = createExecObj(createtype);//执行对象
string dojojsondata = isencode ?
HttpUtility.UrlDecode((context.Request.Params["dojojsondata"] ?? "").Trim())
: (context.Request.Params["dojojsondata"] ?? "").Trim();
if (dojojsondata.Length > 0)
{
JObject js = JObject.Parse(dojojsondata);
var oldobj = fecholdobj(coditionobject.GetType(), js);
coditionobject = createnewobj(coditionobject, oldobj, js);
}
else
{
FormToClass(coditionobject, context.Request.Form);//对象值
FormToClass(coditionobject, context.Request.QueryString);//对象值
}
}
else if (paramsnum > 0)//实例方法
{
//获取方法参数
NameValueCollection condtions = new NameValueCollection();
condtions.Add(context.Request.Form);
condtions.Add(context.Request.QueryString);
condtions.Add(context.Request.Headers);
ParameterInfo[] parameterInfos = execlMethod.GetParameters();
var isnamevalue = parameterInfos.FirstOrDefault(q => q.ParameterType == typeof(NameValueCollection));
if (isnamevalue != null)
{
objparams = condtions;
}
else
{
objparams = FormToParams(parameterInfos, condtions);
}
}
object fs = "";
if (cmds[1] == "Builder")//
{
fs = MethodCaller.CreateInstance(createtype);
}
else if (cmds[1] == "CreateObject")
{
fs = coditionobject;
}
else if (Csla.Utilities.GetPropertyType(createtype).GetInterface("IBusinessObject") != null
&&
cmds[1] == "FindPage")//分页
{
fs = MethodCaller.CallFactoryMethod(createtype, execlMethod.Name, new object[] { objparams });
var allcount = MethodCaller.CallPropertyGetter(fs, "TotalRowCount");
var allcount2 = MethodCaller.CallPropertyGetter(fs, "Count");
//分页处理
int skip = 0;
int take = 0;
string orderbyasc = "";
string orderbydesc = "";
WH.Business.Utilities.DojoPageUtility.pageorderpasers(objparams as NameValueCollection, out take, out skip,
out orderbyasc, out orderbydesc);
if ((int)allcount2 > (long)allcount)
{
allcount = allcount2;
}
if ((int)allcount2 > 0)
{
allcount2 = (int)allcount2 - 1;
}
context.Response.Headers.Add("Content-Range", "items " + skip.ToString() + "-" + allcount2 + "/" + allcount.ToString());
}
else if (Csla.Utilities.GetPropertyType(createtype).GetInterface("IBusinessObject") != null
&& (cmds[1] == "Save" || cmds[1] == "Confirme"))
{
string brokenRules = "";
var isvalid = MethodCaller.CallPropertyGetter(coditionobject, "IsValid");
if (!((bool)isvalid))
{
var getbrokenrules = MethodCaller.CallMethod(coditionobject, "GetBrokenRules") as Csla.Rules.BrokenRulesCollection;
foreach (var brokenRule in getbrokenrules)
{
var lambdaBrokenRule = brokenRule;
var friendlyName =
Csla.Core.FieldManager.PropertyInfoManager.GetRegisteredProperties(coditionobject.GetType()).Find(
c => c.Name == lambdaBrokenRule.Property).FriendlyName;
brokenRules += string.Format("{0}: {1}{2}", friendlyName, brokenRule, Environment.NewLine);
}
throw new Exception(brokenRules);
}
}
else
{
if (objparams != null)
{
if (objparams is NameValueCollection)
{
if (execlMethod.IsStatic)
{
fs = MethodCaller.CallFactoryMethod(createtype, execlMethod.Name, new object[] { objparams });
}
else
{
fs = MethodCaller.CallMethod(coditionobject, execlMethod.Name, new object[] { objparams });
}
}
else
{
if (execlMethod.IsStatic)
{
fs = MethodCaller.CallFactoryMethod(createtype, execlMethod.Name, (objparams as List<object>).ToArray());
}
else
{
fs = MethodCaller.CallMethod(coditionobject, execlMethod.Name, (objparams as List<object>).ToArray());
}
}
}
else
{
if (execlMethod.IsStatic)
{
fs = MethodCaller.CallFactoryMethod(createtype, execlMethod.Name);
}
else
{
fs = MethodCaller.CallMethod(coditionobject, execlMethod.Name);
}
}
}
string uiview = "";
if (isview)
{
//string localpath = context.Request.UrlReferrer.LocalPath.Substring(context.Request.UrlReferrer.LocalPath.LastIndexOf('/'));
//if (HttpContext.Current.Cache["allpages"] == null)
//{
// ICriteria<page> criteria = CriteriaFactory.Create<page>();
// pageList mpagelist = pageList.Fetch(criteria);
// if (mpagelist.Count > 0)
// {
// }
// HttpContext.Current.Cache.Insert("allpages", mpagelist, null,
// DateTime.Now.AddMinutes(20), System.Web.Caching.Cache.NoSlidingExpiration);
//}
//pageList allpagelist = HttpContext.Current.Cache["allpages"] as pageList;
//var getonepage = allpagelist.FirstOrDefault(q => q.pageurl.IndexOf(localpath, StringComparison.OrdinalIgnoreCase) > -1);
//if (getonepage != null && (getonepage.pagecontrol ?? "").Trim().Length > 0)
//{
// var uo = CriteriaFactory.Create<user_operation>() as ICriteria<user_operation>;
// int operationid = 0;
// if (int.TryParse(context.User.Identity.Name, out operationid))
// {
// uo = uo.Where(q => q.user_id == operationid
// && q.user_name == getonepage.pagename);
// //
// var uopartio = user_operation.Fetch(uo);
// //if (!uopartio.IsNew) uopartio = user_operation.New();
// //uiview = "sss";
// //context.Server.Execute(TemplatePath + "?" + context.Request.ServerVariables["Query_String"]);
// //第一种
// //context.Server.Execute(TemplatePath + "?" + context.Request.ServerVariables["Query_String"]);
// //第二种,下面这段生成静态文件的时候有用,如果不生成静态文件就用上面这种
// //System.IO.TextWriter tw = new System.IO.StringWriter();
// //context.Server.Execute(TemplatePath + "?" + context.Request.ServerVariables["Query_String"], tw);
// //context.Response.Write(tw.ToString());
// uiview = Template.Parse((getonepage.pagecontrol ?? "").Trim()).Render(Hash.FromAnonymousObject(new
// {
// Model = new
// {
// orderbill = new
// {
// IsNew = (bool)MethodCaller.CallPropertyGetter(coditionobject, "IsNew"),
// confirmflag = (bool)MethodCaller.CallPropertyGetter(coditionobject, "confirmflag"),
// },
// currentuser = context.User.Identity as useriddentity,
// uopartios = new
// {
// openflag = uopartio.openflag,
// addlag = uopartio.addlag,
// modifyflag = uopartio.modifyflag,
// delflag = uopartio.delflag,
// confirmflag = uopartio.confirmflag,
// exportflag = uopartio.exportflag,
// printflag = uopartio.printflag
// }
// }
// }));
// //uiview = Razor.Parse((getonepage.pagecontrol ?? "").Trim(), new { orderbill = fs, currentuser = context.User.Identity as useriddentity, uopartios = uopartio });
// }
//}
//uiview =
}
var returndata = new
{
ErrorNo = 0,
ErrorInfo = "",
Data = fs == null ? (object)"" : fs,
View = uiview
};
var timeConverter = new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" };
outtxt = JsonConvert.SerializeObject(returndata, Formatting.Indented, timeConverter);
}
catch (CallMethodException ex)
{
string errorinfo = "";
//if (ex.InnerException is ArgumentException)
//{
// errorinfo = ModelValidator.ShowUIError(ex.InnerException as ArgumentException);
//}
if ((errorinfo ?? "").Trim().Length == 0)
{
errorinfo = ex.InnerException != null ? ex.InnerException.Message : ex.Message;
}
var ap = new
{
ErrorInfo = errorinfo,
ErrorNo = 1,
Data = "",
};
var timeConverter = new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" };
outtxt = JsonConvert.SerializeObject(ap, Formatting.Indented, timeConverter);
}
catch (Exception ex)
{
var ap = new
{
ErrorInfo = ex.Message,
ErrorNo = 1,
Data = "",
};
var timeConverter = new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" };
outtxt = JsonConvert.SerializeObject(ap, Formatting.Indented, timeConverter);
}
context.Response.Write(outtxt ?? "");
}
private static object createExecObj(Type createtype)
{
return MethodCaller.CreateInstance(createtype);
}
private const BindingFlags factoryFlags =
BindingFlags.Static |
BindingFlags.Public |
BindingFlags.FlattenHierarchy;
/// <summary>
/// 获取执行方法
/// </summary>
/// <param name="cmds"></param>
/// <param name="createtype"></param>
/// <returns></returns>
private static System.Reflection.MethodInfo getExeMethod(string cmdname, Type createtype, int paramsnum)
{
System.Reflection.MethodInfo execlMethod = MethodCaller.FindMethod(createtype, cmdname, paramsnum);
if (execlMethod == null)
{
System.Reflection.MethodInfo[] methods = createtype.GetMethods(factoryFlags);
foreach (System.Reflection.MethodInfo oneMethod in methods)
if (oneMethod.Name == cmdname && oneMethod.GetParameters().Length == paramsnum)
{
execlMethod = oneMethod;
break;
}
}
if (execlMethod == null)
{
throw new Exception("此命令不存在");
}
return execlMethod;
}
/// <summary>
/// 创建执行主体类型
/// </summary>
/// <param name="cmdobj"></param>
/// <returns></returns>
private static Type createExeType(string cmdobj)
{
Type createtype = null;
foreach (var createtypename in businessnames)
{
createtype = Type.GetType(string.Format(createtypename, cmdobj));
if (createtype != null)
{
break;
}
}
if (createtype == null)
{
throw new Exception("此命令对象不存在");
}
return createtype;
}
private static object createnewobj(object coditionobject, object tempobj, JObject js)
{
object oldobj = null;
if (tempobj != null )
{
oldobj = tempobj;
}
else
{
oldobj = coditionobject;
}
var createtypes = coditionobject.GetType().GetProperties();
foreach (var item in createtypes)
{
JToken outvalue = null;
if (js.TryGetValue(item.Name, out outvalue))
{
Type vType = Csla.Utilities.GetPropertyType(item.PropertyType);
if (js[item.Name].Type == JTokenType.Array)
{
var sonobj = MethodCaller.CallPropertyGetter(oldobj, item.Name);//数组的问题
var sonobjlist = sonobj as IList;
var sonType = Csla.Utilities.GetChildItemType(vType);
var sj = JArray.Parse(js[item.Name].ToString());
//var jarray = js[item.Name].ToArray();// JArray.Parse(js[item.Name].ToString());
bool isobjnew = true;
List<object> sameobjs = new List<object>();
foreach (JObject sonitem in sj)
{
//当前值是否已有
foreach (var onesonitemobj in sonobjlist)
{
try
{
if (isamekeyobj(onesonitemobj, sonitem))
{
createnewobj(onesonitemobj, null, sonitem);
isobjnew = false;
sameobjs.Add(onesonitemobj);
break;
}
}
catch (Exception ex)
{
throw ex;
}
}
if (isobjnew)
{
var itemtype = Csla.Utilities.GetChildItemType(sonobjlist.GetType());
var sonitemobj = MethodCaller.CreateInstance(itemtype);
MethodCaller.CallMethod(sonobjlist, "Add", sonitemobj);
createnewobj(sonitemobj, null, sonitem);
sameobjs.Add(sonitemobj);
}
if (!isobjnew)
{
isobjnew = true;
}
}
List<object> nosameobjs = new List<object>();
foreach (var onesonitemobj in sonobjlist)
{
if (sameobjs.IndexOf(onesonitemobj) == -1)
{
nosameobjs.Add(onesonitemobj);
}
}
sameobjs.Clear();
//移除不要的
foreach (var removeitem in nosameobjs)
{
sonobjlist.Remove(removeitem);
}
}
else
{
object m;
if (!item.CanWrite)
{
continue;
}
if (vType == typeof(SmartDate))
{
m = new SmartDate((DateTime)js[item.Name].ToObject(typeof(DateTime)));
}
else
{
m = js[item.Name].ToObject(vType);
}
MethodCaller.CallPropertySetter(oldobj, item.Name, m);
}
}
}
return oldobj;
}
private static object fecholdobj(Type coditiontye, JObject js)
{
var keys = coditiontye.GetProperties().Where(q =>
q.GetCustomAttributes(typeof(System.ComponentModel.DataObjectFieldAttribute), false).Length > 0);//获取key
if (keys.Count() == 0) return null;
NameValueCollection condtions = new NameValueCollection();
string values = "";
//fs = MethodCaller.CallFactoryMethod(createtype, cmds[1], new object[] { condtions });
foreach (var item in keys)
{
JToken outvalue = null;
if (js.TryGetValue(item.Name, out outvalue))
{
Type vType = Csla.Utilities.GetPropertyType(item.PropertyType);
var keyvalue = js[item.Name].ToObject(vType);
if (keyvalue == null)
{
return null;
}
else
{
values += ("," + keyvalue.ToString());
}
}
}
condtions.Add("key", values.Remove(0, 1));
return MethodCaller.CallFactoryMethod(coditiontye, "FetchOne", new object[] { condtions });
}
private static bool isamekeyobj(object oldobj, object newobj)
{
var coditiontype = oldobj.GetType();
var keys = coditiontype.GetProperties().Where(q =>
q.GetCustomAttributes(typeof(System.ComponentModel.DataObjectFieldAttribute), false).Length > 0);//获取key
foreach (var item in keys)
{
var obj1 = MethodCaller.CallPropertyGetter(oldobj, item.Name);
var obj2 = MethodCaller.CallPropertyGetter(newobj, item.Name);
if (!obj1.Equals(obj2))
return false;
}
return true;
}
private static bool isamekeyobj(object oldobj, JObject js)
{
var coditiontype = oldobj.GetType();
var keys = coditiontype.GetProperties().Where(q =>
q.GetCustomAttributes(typeof(System.ComponentModel.DataObjectFieldAttribute), false).Length > 0);//获取key
foreach (var item in keys)
{
var obj1 = MethodCaller.CallPropertyGetter(oldobj, item.Name);
Type vType = Csla.Utilities.GetPropertyType(item.PropertyType);
var obj2 = js[item.Name].ToObject(vType);
if (!obj1.Equals(obj2))
return false;
}
return true;
}
private static void setobj(object newobj, object oldobj)
{
var createtypes = newobj.GetType().GetProperties();// createtype.GetProperties();
foreach (var item in createtypes)
{
var obj = MethodCaller.CallPropertyGetter(newobj, item.Name);
if (obj is IList)
{
//正反对比,获取要删除的数据
var oldlist = MethodCaller.CallPropertyGetter(oldobj, item.Name) as IList;
var newlist = MethodCaller.CallPropertyGetter(newobj, item.Name) as IList;
//delete alls
bool isdelete = true;
foreach (var olditem in oldlist)
{
foreach (var newitem in newlist)
{
if (isamekeyobj(olditem, newitem))
{
isdelete = false;//赋值
setobj(newitem, oldobj);
break;
}
if (isdelete)
{
isdelete = false;
MethodCaller.CallMethod(oldobj, "Remove", olditem);
}
}
}
foreach (var newitem in newlist)
{
foreach (var olditem in oldlist)
{
if (isamekeyobj(olditem, newitem))
{
continue;
}
}
}
}
}
}
public bool IsReusable
{
get
{
return false;
}
}
/// <summary>
/// 检查权限
/// </summary>
/// <param name="context"></param>
private static void checkuserrole(HttpContext context)
{
if (context.User == null)
{
throw new Exception("登录已超时");
}
}
public void FormToClass(object nowobj, NameValueCollection collection)
{
var properties = nowobj.GetType().GetProperties();
foreach (var item in properties)
{
if (item.CanWrite && collection.AllKeys.Contains(item.Name))
{
try
{
MethodCaller.CallPropertySetter(nowobj, item.Name,
Convert.ChangeType(collection[item.Name], item.PropertyType));
}
catch (Exception ex)
{
throw new ArgumentException(ex.Message, item.Name);
}
}
}
}
public List<object> FormToParams(ParameterInfo[] properties, NameValueCollection collection)
{
List<object> nowobj = new List<object>();
//var properties = nowobj.GetType().GetProperties();
foreach (var item in properties)
{
if (collection.AllKeys.Contains(item.Name))
{
try
{
//MethodCaller.CallPropertySetter(nowobj, item.Name,
nowobj.Add(Convert.ChangeType(collection[item.Name], item.ParameterType));//);
}
catch (Exception ex)
{
throw new ArgumentException(ex.Message, item.Name);
}
}
}
return nowobj;
}
}