Generic reflection common application, create an instance by type, by reflecting achieve CRUD

the Test class public 
    { 


        public void in MyObj <T> () WHERE T: class 
        { 
            // new new T T = T (); 
            T = System.Activator.CreateInstance T <T> (); 
            // get the fields to obtain the type with method with the corresponding name field on it 
        } 
    }

  

The method of creating micro-channel official entity, created by the instance of the generic type 
System.Activator.CreateInstance <T> ();





added:
public static void Add<T>(T model)
        {
            Type t = typeof(T);
            string TableName = GetTableName(t);
            PropertyInfo[] pps = t.GetProperties();
            StringBuilder sb = new StringBuilder("insert into"+ TableName + "(");
            StringBuilder sbColumns = new StringBuilder();
            StringBuilder sbValues = new StringBuilder();
            foreach (PropertyInfo item in pps)
            {
                if (!IsNotMapper(item)&& !IsKey(item))
                {
                    sbColumns.Append(item.Name+",");
                    if (IsNoStr(item.PropertyType))
                    {
                        
                        if (item.GetValue(model)==null|| string.IsNullOrEmpty(item.GetValue(model).ToString()))
                        {
                            sbValues.Append("null,");
                        }
                        else
                        {
                            
                            if (IsBoolean(item.PropertyType))
                            {
                                bool bvalue = (bool)item.GetValue(model);
                                sbValues.Append((bvalue?1:0) + ",");
                            }
                            else
                            {
                                sbValues.Append(item.GetValue(model) + ",");
                            }
                            
                        }
                    }
                    else
                    {
                        sbValues.Append("'"+item.GetValue(model) + "',");
                    }
                    
                }
            }
            string sql = string.Format("insert into " + TableName + "({0}) values({1})",sbColumns.ToString().TrimEnd(','), sbValues.ToString().TrimEnd(','));
            connection.Execute(sql);
        }

  




modify:
public static void Update<T>(T model)
        {
            Type t = typeof(T);
            string TableName = GetTableName(t);
            PropertyInfo[] pps = t.GetProperties();
            StringBuilder sb = new StringBuilder();
            string where = "";
            foreach (PropertyInfo item in pps)
            {
                if (!IsNotMapper(item))
                {
                    string column = item.Name;
                    string value = item.GetValue(model).ToString();
                    if (!IsKey(item))
                    {
                        if (IsNoStr(item.PropertyType))
                        {
                            if (IsBoolean(item.PropertyType))
                            {
                                bool bvalue = (bool)item.GetValue(model);
                                value = bvalue ? "1" : "0";
                            }
                            sb.Append(column + "=" + value + ",");
                        }
                        else
                        {
                            sb.Append(column + "='" + value + "',");
                        }
                    }
                    else
                    {
                        where = column + "=" + value;
                    }
                }
            }

            string sql = string.Format("update {0} set {1} where {2} ",TableName,sb.ToString().TrimEnd(','),where);
        }

  

 

 

 

delete:

public static void Delete<Key, T>(Key id)
        {
            Type tp = typeof(T);
            PropertyInfo[] pps = tp.GetProperties();
            string KeyName = "";
            Key Value = id;
            foreach (PropertyInfo item in pps)
            {
                if (!IsNotMapper(item)&& IsKey(item))
                {
                    KeyName = item.Name;
                }
            }
            if (IsNoStr(typeof(Key)))
            {
                string sql = "delete " + GetTableName(tp) + " where " + KeyName + "=" + Value;
            }
            else
            {
                string sql = "delete " + GetTableName(tp) + " where " + KeyName + "='" + Value + "'";
            }
        }

  

 

Analyzing field or class characteristics comprising:

/// <summary>
        /// 是否包含该特性
        /// </summary>
        /// <param name="item"></param>
        /// <param name="tp"></param>
        /// <returns></returns>
        private static bool IsAttribute(PropertyInfo item, Type tp)
        {
            var attr = item.GetCustomAttributes(tp, true);
            if (attr.Length <= 0)
                return false;
            return true;
        }

  

 

 

Gripping the specified attribute value (e.g., table name, subsections describe other special properties):

/// <summary>
        /// 查询表名
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        private static string GetTableName(Type t)
        {
            string Name = t.Name;
            object[] os = t.GetCustomAttributes(typeof(TablNameAttribute), true);
            if (os.Length > 0)
            {
                TablNameAttribute ta = os[0] as TablNameAttribute;
                Name = ta.TableName;
            }
            return Name;
        }

  

 


Generics are reflected when we do need to frequently used system architecture, if skillfully applied to the project can play the role of constraints and control for business development.












Guess you like

Origin www.cnblogs.com/DavidHuAtIT/p/12150003.html