/// <summary> /// 查找所有程序[特性]类 /// </summary> private static void lookForAttributeInstance() { try { Type[] entryTypes = Assembly.GetEntryAssembly().GetTypes(); //入口程序集 Type[] fwTypes = Assembly.GetExecutingAssembly().GetTypes(); //框架程序集 //Type[] allTypes = entryTypes.Concat(fwTypes).ToArray(); foreach (Type type in entryTypes) { IEnumerable <Attribute> attributes = type.GetCustomAttributes(typeof(BaseInstanceAttribute)); foreach (System.Attribute a in attributes) { //如果是实例(含有BaseInstanceAttribute),创建实例并加入到FrameWorkSet if (a is BaseInstanceAttribute) { BaseInstanceAttribute attr = a as BaseInstanceAttribute; if (FwUtilFunc.StringIsNotEmpty(attr.Name)) { LTCingFWSet.Beans.Add(new FwInstanceBean(attr.Name, type.FullName, Assembly.GetEntryAssembly())); logger.Info(String.Format("FrameWork find instance[{0}] with name[{1}]", type.FullName, attr.Name)); } else if (!LTCingFWSet.Instance.ContainsKey(type.Name)) { LTCingFWSet.Beans.Add(new FwInstanceBean(type.Name, type.FullName, Assembly.GetEntryAssembly())); logger.Info(String.Format("FrameWork find instance[{0}] with name[{1}]", type.FullName, type.Name)); } } } } foreach (Type type in fwTypes) { IEnumerable <Attribute> attributes = type.GetCustomAttributes(typeof(BaseInstanceAttribute)); foreach (System.Attribute a in attributes) { //如果是实例(含有BaseInstanceAttribute),创建实例并加入到FrameWorkSet if (a is BaseInstanceAttribute) { BaseInstanceAttribute attr = a as BaseInstanceAttribute; if (FwUtilFunc.StringIsNotEmpty(attr.Name)) { LTCingFWSet.Beans.Add(new FwInstanceBean(attr.Name, type.FullName, Assembly.GetExecutingAssembly())); logger.Info(String.Format("FrameWork find instance[{0}] with name[{1}]", type.FullName, attr.Name)); } else if (!LTCingFWSet.Instance.ContainsKey(type.Name)) { LTCingFWSet.Beans.Add(new FwInstanceBean(type.Name, type.FullName, Assembly.GetExecutingAssembly())); logger.Info(String.Format("FrameWork find instance[{0}] with name[{1}]", type.FullName, type.Name)); } } } } } catch (Exception e) { throw new LTCingFWException("查找特性实例出错", e); } }
private void GetConnection(string dbAlias) { DB_Leaf node = LTCingFWSet.dbDic[dbAlias]; //String dbtype = node.DbType.Trim().ToLower(); this.DbAlias = dbAlias; if (FwUtilFunc.StringIsNotEmpty(node.ProviderName)) { this.DbFactory = DbProviderFactories.GetFactory(node.ProviderName); this.Connection = DbFactory.CreateConnection(); this.ProviderName = node.ProviderName; if (FwUtilFunc.StringIsNotEmpty(node.ConnectionString)) { this.Connection.ConnectionString = node.ConnectionString; return; } } }
/// <summary> /// 织入代理代码 /// </summary> /// <param name="bean"></param> private static string WavingProxyCode() { //命名空间预设 StringBuilder sb = new StringBuilder(); sb.Append(" using System;\n using LTCingFW;\n using System.Threading;\n"); sb.Append(" using log4net;\n");//log2 sb.Append(" namespace " + FWConfigs.proxyNameSpace + " { \n "); foreach (FwInstanceBean bean in LTCingFWSet.Beans) { #region 代理类 String bean_name = bean.Name; String type_full_name = bean.Type; String[] sps = type_full_name.Split('.'); String LTCingFW_proxy_name = "_proxy_" + sps[sps.Length - 1]; sb.Append(String.Format(" public class {0} : {1} {{ \n", LTCingFW_proxy_name, type_full_name)); //3 sb.Append(String.Format(" private static readonly ILog logger = LogManager.GetLogger(typeof({0}));", LTCingFW_proxy_name)); #region 代理方法 Type type = bean.BelongAssembly.GetType(type_full_name); MethodInfo[] mtds = type.GetMethods(BindingFlags.Public | BindingFlags.Instance); foreach (MethodInfo mi in mtds) { #region 方法筛选 //去除掉队GetType、GetHashCode、Equals、ToString方法的重写 if (mi.Name == "GetType" || mi.Name == "GetHashCode" || mi.Name == "Equals" || mi.Name == "ToString") { continue; } //去除属性方法的重写 if (mi.Name.IndexOf("get_") > -1 || mi.Name.IndexOf("set_") > -1) { continue; } //获取切面 FwAopBean aspect = GetMethodAspect(type_full_name, mi); //获取DBSessionAttribute DBSessionAttribute dbSessionAttr = GetMethodTransactionAttribute(mi); if (dbSessionAttr == null) { dbSessionAttr = GetClassTransactionAttribute(type); } #endregion if (mi.Name == "NoticeDataChangedCallBack") { } if (!mi.IsVirtual) { continue; } #region 设置方法名、参数、返回值还原 StringBuilder generics = new StringBuilder(""); if (mi.IsGenericMethod) { generics.Append("<"); foreach (Type item in mi.GetGenericArguments()) { generics.Append(item.Name); generics.Append(","); } generics.Remove(generics.Length - 1, 1); generics.Append(">"); } string method_head = String.Format("\n public override {0} {1} {2}( ", FwUtilFunc.GetRegularReturnType(mi.ReturnType), mi.Name, generics.ToString()); sb.Append(method_head); #endregion #region 方法内前置内容 ParameterInfo[] paramInfos = mi.GetParameters(); StringBuilder paramStr = new StringBuilder(" "); StringBuilder objStr = new StringBuilder(" "); for (int i = 0; i < paramInfos.Length; i++) { sb.Append(FwUtilFunc.GetRegularReturnType(paramInfos[i].ParameterType)); sb.Append(" para").Append(i).Append(" ,"); if (paramInfos[i].ParameterType.IsByRef) { paramStr.Append(" ref"); } paramStr.Append(" para").Append(i).Append(" ,"); objStr.Append(" para").Append(i).Append(" ,"); } paramStr.Remove(paramStr.Length - 1, 1); sb.Remove(sb.Length - 1, 1); sb.Append(")"); //if (mi.ReturnType.FullName == null) //{ // sb.Append(" where T : OrmBaseModel"); //} sb.Append("{\n"); sb.Append(" object[] paras = new object[]{ ").Append(objStr).Append("};\n"); sb.Append(" bool outerSession = false;\n"); sb.Append(" DBSession session = null;\n"); if (mi.ReturnType.FullName != "System.Void") { sb.Append(" object r = null; \n"); } sb.Append(" try \n {\n"); sb.Append(" if (!LTCingFWSet.ThreadContextDic.ContainsKey(Thread.CurrentThread.ManagedThreadId)) {\n"); sb.Append(" LTCingFWSet.ThreadContextDic.Add(Thread.CurrentThread.ManagedThreadId, new LTCingFW.beans.ThreadContext());\n"); sb.Append(" LTCingFWSet.ThreadContextDic[Thread.CurrentThread.ManagedThreadId].MethodName = \""); sb.Append(type_full_name).Append(".").Append(mi.Name).Append("\";"); sb.Append(" \n}\n"); sb.Append(" else if(LTCingFWSet.ThreadContextDic[Thread.CurrentThread.ManagedThreadId].DBSession != null)\n"); sb.Append(" {\n outerSession = true;\n}\n"); #endregion #region 方法前部分 //进入过程ERROR置为空 sb.Append(" LTCingFWSet.ThreadContextDic[Thread.CurrentThread.ManagedThreadId].Error = null;\n"); //Transaction判定 if (dbSessionAttr != null) { sb.Append(" if(!outerSession){\n"); sb.Append(String.Format("session = DBSession.OpenSession(\"{0}\",{1});\n", dbSessionAttr.DbAlias, dbSessionAttr.OpenTransaction.ToString().ToLower())); sb.Append("LTCingFWSet.ThreadContextDic[Thread.CurrentThread.ManagedThreadId].DBSession = session;\n"); sb.Append(" }\n"); } #region 前AOP方法 if (aspect != null) { if (FwUtilFunc.StringIsNotEmpty(aspect.BeforeMethod)) { //检测切入函数是否为公共和静态 int index = aspect.BeforeMethod.LastIndexOf('.'); String full_type_name = aspect.BeforeMethod.Substring(0, index); String method_name = aspect.BeforeMethod.Substring(index + 1); Type typeCls = Assembly.GetEntryAssembly().GetType(full_type_name); MethodInfo mtdInfo = typeCls.GetMethod(method_name); if (!mtdInfo.IsPublic || !mtdInfo.IsStatic) { throw new Exception("切入函数需为公共静态函数"); } //加入切点函数 sb.Append(aspect.BeforeMethod).Append("(paras);\n"); } } #endregion #endregion #region 原方法,中间部分 if (mi.ReturnType.FullName != "System.Void") { sb.Append(" r = ").Append("base.").Append(mi.Name); if (mi.ReturnType.FullName == null) { sb.Append("<T> "); } sb.Append("(").Append(paramStr).Append(");\n"); } else { sb.Append("base.").Append(mi.Name); sb.Append("(").Append(paramStr).Append(");\n"); } #endregion #region 方法后部分 sb.Append(" }\n catch (Exception ex) { \n "); //sb.Append(" logger.Warn(\"Proxy_InnerException:\"+ex.Message+ex.StackTrace);\n"); sb.Append(" if(session != null && session.Transaction != null) \n{session.RollBack(); session.Close();\n}\n"); //sb.Append(" LTCingFWSet.ErrList.Add(ex); \n");//伴随框架的错误处理线程,一起注掉了 sb.Append(" LTCingFWSet.ThreadContextDic[Thread.CurrentThread.ManagedThreadId].Error = ex;\n"); sb.Append(" throw ex; \n"); sb.Append(" }\n "); sb.Append(" finally \n { \n "); sb.Append(" if(!outerSession)\n{\n"); sb.Append(" if(session != null && !session.IsClosed()) \n{ \n "); sb.Append(" session.Close(); \n}\n"); sb.Append(" if(LTCingFWSet.ThreadContextDic.ContainsKey(Thread.CurrentThread.ManagedThreadId))\n{ \n"); sb.Append(" LTCingFWSet.ThreadContextDic[Thread.CurrentThread.ManagedThreadId].DBSession = null;\n}\n"); sb.Append(" }\n"); #region 后AOP方法 if (aspect != null) { if (FwUtilFunc.StringIsNotEmpty(aspect.AfterMethod)) { //检测切点函数是否为公共和静态 int index = aspect.AfterMethod.LastIndexOf('.'); String full_type_name = aspect.AfterMethod.Substring(0, index); String method_name = aspect.AfterMethod.Substring(index + 1); Type typeCls = Assembly.GetEntryAssembly().GetType(full_type_name); MethodInfo mtdInfo = typeCls.GetMethod(method_name); if (!mtdInfo.IsPublic || !mtdInfo.IsStatic) { throw new Exception("切入函数需为公共静态函数"); } //加入切点函数 sb.Append(aspect.AfterMethod).Append("(paras);\n"); } if (FwUtilFunc.StringIsEmpty(aspect.BeforeMethod) && FwUtilFunc.StringIsEmpty(aspect.AfterMethod)) { throw new Exception("切面需要一个前置切入函数或一个后置切入函数"); } } #endregion sb.Append(" }\n"); //返回值 if (mi.ReturnType.FullName != "System.Void") { sb.Append(" return (").Append(FwUtilFunc.GetRegularReturnType(mi.ReturnType)).Append(") r; \n"); } //结束 sb.Append("}\n"); #endregion } #endregion sb.Append("}\n"); #endregion //记录日志 logger.Info(String.Format("create Proxy for bean[{0}]", bean.Name)); bean.ProxyType = FWConfigs.proxyNameSpace + "." + LTCingFW_proxy_name; } //命名空间结束 sb.Append(" } \n"); return(sb.ToString()); }
/// <summary> /// 针对实例的某个属性验证 /// </summary> /// <param name="model">实例</param> /// <param name="memberName">属性名</param> /// <param name="value">属性值</param> /// <returns></returns> public static ValidResult Valid(object model, string memberName, object val) { string value = val.ToString(); ValidResult result = new ValidResult(); result.Result = true; result.ProPertyName = memberName; result.Value = value; try { //提取验证信息 PropertyInfo info = model.GetType().GetProperty(memberName, BindingFlags.Public | BindingFlags.Instance); ValidateAttribute attr = info.GetCustomAttribute(typeof(ValidateAttribute)) as ValidateAttribute; if (attr == null) { return(result); } //默认常用验证模式 if (attr.Items != 0) { //非空 if ((attr.Items & ValidateEnum.NOT_NULL) == ValidateEnum.NOT_NULL) { if (FwUtilFunc.StringIsEmpty(value)) { result.Result = false; result.ErrorMessage += "必填 "; } } //最大长度 if ((attr.Items & ValidateEnum.MAX_LENGTH) == ValidateEnum.MAX_LENGTH) { if (FwUtilFunc.StringIsNotEmpty(value)) { if (attr.MaxLength != 0) { if (value.Length > attr.MaxLength) { result.Result = false; result.ErrorMessage += "超过最大长度" + attr.MaxLength + " "; } } else { throw new Exception("未设置最大值"); } } } //最小长度 if ((attr.Items & ValidateEnum.MIN_LENGTH) == ValidateEnum.MIN_LENGTH) { if (FwUtilFunc.StringIsNotEmpty(value)) { if (attr.MinLength != 0) { if (value.Length < attr.MinLength) { result.Result = false; result.ErrorMessage += "少于最小长度" + attr.MinLength + " "; } } else { throw new Exception("未设置最小值"); } } } //日期 if ((attr.Items & ValidateEnum.IS_DATE) == ValidateEnum.IS_DATE) { if (FwUtilFunc.StringIsNotEmpty(value)) { try { Convert.ToDateTime(value); } catch (Exception) { result.Result = false; result.ErrorMessage += "为非法日期格式 "; } } } //数字 if ((attr.Items & ValidateEnum.IS_NUMBER) == ValidateEnum.IS_NUMBER) { if (FwUtilFunc.StringIsNotEmpty(value)) { try { Decimal.Parse(value); } catch (Exception) { result.Result = false; result.ErrorMessage += "为非法数字格式 "; } } } } //自定义正则验证模式 if (result.Result && FwUtilFunc.StringIsNotEmpty(attr.Regx)) { try { if (FwUtilFunc.StringIsNotEmpty(value) && !Regex.IsMatch(value, attr.Regx)) { result.Result = false; result.ErrorMessage += "未通过自定义正则验证:" + attr.Regx; logger.Warn(value + "不符合自定义正则表达式" + attr.Regx); } } catch (Exception e) { throw e; } } //自定义函数验证模式,验证函数的格式,必须有个一string参数,以及一个bool返回值,以及static关键字 if (result.Result && FwUtilFunc.StringIsNotEmpty(attr.Function_Path)) { try { if (FwUtilFunc.StringIsNotEmpty(value)) { //提取方法,验证方法信息 int sp_index = attr.Function_Path.LastIndexOf('.'); string classType = attr.Function_Path.Substring(0, sp_index); string methodName = attr.Function_Path.Substring(sp_index + 1); Type type = Assembly.GetEntryAssembly().GetType(classType); MethodInfo mInfo = type.GetMethod(methodName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.IgnoreCase | BindingFlags.Static); if (mInfo == null) { throw new Exception("自定义验证函数未找到,请确定函数路径且必须为static静态!"); } ParameterInfo[] pInfos = mInfo.GetParameters(); if (pInfos.Length != 1 || pInfos[0].ParameterType != typeof(string)) { throw new Exception("自定义验证函数必须为一个字符串类型参数!"); } if (mInfo.ReturnType != typeof(bool)) { throw new Exception("自定义验证函数返回值必须为bool布尔类型!"); } //执行方法,取得结果 bool ret = true; try { ret = (bool)mInfo.Invoke(null, new object[] { value }); } catch (Exception) { ret = false; } //设置返回提示 if (!ret) { result.Result = false; result.ErrorMessage += "未通过自定义验证方法 "; } } } catch (Exception e) { throw e; } } } catch (Exception ex) { result.Result = false; result.ErrorMessage += "验证内部错误"; logger.Warn(ex.Message); } //返回提示 return(result); }