/// <summary> /// 构建类方法 /// </summary> /// <param name="p"></param> /// <returns></returns> private static string DoClass(Type p) { MemberInfo classInfo = p; StringBuilder sb = new StringBuilder(); var attrs = System.Attribute.GetCustomAttributes(classInfo, typeof(System.Attribute)).ToList(); if (attrs == null || attrs.Count <= 0) { return(string.Empty); } Attribute.TableAttribute table = null; bool isPartial = false;//是否有自动生成的方法 attrs.ForEach(a => { if (table == null && a.GetType() == typeof(Attribute.TableAttribute)) { table = a as Attribute.TableAttribute; if (!string.IsNullOrWhiteSpace(table.BelongToDBUser) && !_dbUsers.Contains(table.BelongToDBUser)) { _dbUsers.Add(table.BelongToDBUser); } } else if (!isPartial && a is IBaseMaker) { isPartial = true; } }); if (table == null) { return(string.Empty); } if (isPartial) { sb.AppendLine(DoMethod(table, attrs, p)); } return(sb.ToString()); }
private static string DoMethod(Attribute.TableAttribute table, List <System.Attribute> attrs, Type p) { string className = table.TableName;// p.Name.StartsWith("m_") ? p.Name.Substring(2, p.Name.Length - 2) : p.Name;//设置类名 StringBuilder attrBuilder = new StringBuilder(); //todo 实现该类 attrBuilder.Append(" #region 注释:").Append(p.FullName).AppendLine(table.Description); attrBuilder.AppendLine(" /// <summary>"); attrBuilder.AppendLine(" /// 模板生成"); attrBuilder.Append(" /// CreateBy 童岭 ").AppendLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); attrBuilder.Append(" /// ").AppendLine(table.Description); attrBuilder.AppendLine(" /// </summary>"); attrBuilder.Append(" public partial class dl_").AppendLine(className); attrBuilder.AppendLine(" {"); foreach (var attr in attrs) //遍历每一个特性 { if (attr is IBaseMaker) //如果其实现了ICodeBuildMaker 接口 { attrBuilder.AppendLine((attr as IBaseMaker).Do(table, p)); //添加方法 } } return(attrBuilder.AppendLine(" }").AppendLine(" #endregion").ToString()); }
public string DoPublic(TableAttribute tableAttribute, Type p) { try { StringBuilder sb = new StringBuilder(); sb.AppendLine(" /// <summary>"); sb.AppendLine(" /// 添加方法,生成时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); sb.AppendLine(" /// " + this.Description); sb.AppendLine(" /// </summary>"); sb.AppendLine(" /// <param name=\"model\">添加对象</param>"); sb.AppendLine(" /// <returns>大于0为添加成功</returns>"); sb.AppendLine(string.Format(" public int {0}({1} model)", this.MethodName, p.FullName)); sb.AppendLine(" {"); sb.AppendLine(" try"); sb.AppendLine(" {"); var fields = GetAddFields(p); sb.AppendLine(" using (var conn = new System.Data.OracleClient.OracleConnection(OracleHelper.ConnString)){").AppendLine(" conn.Open();"); sb.AppendLine(" var command = conn.CreateCommand();"); sb.Append(this.GetOracleAddParam(" if(model.{1}!=null) fields.Add(\"{0}\");if(model.{1}!=null) command.Parameters.Add(new OracleParameter(\":{0}\", model.{1}));", p)); //sb.AppendLine(string.Format(" command.CommandText = \"insert into {0}({1}) values(:{2})\";", tableAttribute.TableName, string.Join(",", fields), string.Join(",:", fields))); sb.AppendLine(" command.CommandText = string.Format(\"insert into " + tableAttribute.TableName + "({0}) values(:{1})\",string.Join(\",\",fields),string.Join(\",:\",fields));"); sb.AppendLine(" return command.ExecuteNonQuery();"); sb.AppendLine(" }"); sb.AppendLine(" }"); sb.AppendLine(" catch (Exception ex)"); sb.AppendLine(" {"); sb.AppendLine(" Lenovo.Tool.Log4NetHelper.Error(ex);"); sb.AppendLine(" return 0;"); sb.AppendLine(" }"); sb.AppendLine(" }"); return sb.ToString(); } catch (Exception ex) { return ex.Message; } }
public string DoPrivate(TableAttribute tableAttribute, Type p) { }
public string Do(TableAttribute tableAttribute, Type p) { StringBuilder sb=new StringBuilder(); sb.AppendLine(DoPublic(tableAttribute,p)).AppendLine(DoPrivate(tableAttribute,p)); return sb.ToString(); }