Exemple #1
0
        /// <summary>
        /// 私有方法,设置泛型实体类的主键字典
        /// </summary>
        /// <typeparam name="T">泛型实体类型</typeparam>
        /// <param name="t">实体</param>
        private static void SetPKCache <T>(T t) where T : new()
        {
            List <string> pklist = new List <string>();

            PropertyInfo[] propcoll = t.GetType().GetProperties();//获取该类型的所有属性
            foreach (var item in propcoll)
            {
                object[] objattribute = item.GetCustomAttributes(typeof(DataSource.ColumnAttribute), false);
                if (objattribute.Length > 0)
                {
                    if ((objattribute[0] as DataSource.ColumnAttribute).IsPrimary)//如果该属性是用主键特性标记为主键的则添加为该类型的主键
                    {
                        pklist.Add(item.Name);
                    }
                }
            }
            if (PKCache.ContainsKey(t.GetType().FullName))
            {
                PKCache[t.GetType().FullName] = pklist;//设置新的主键集合
            }
            else
            {
                PKCache.Add(t.GetType().FullName, pklist);
            }
        }
Exemple #2
0
 /// <summary>
 /// 私有方法,获取泛型实体类的主键集合
 /// </summary>
 /// <typeparam name="T">泛型实体类型</typeparam>
 /// <param name="t">实体</param>
 /// <returns>字典集合</returns>
 private static List <string> GetPKCache <T>(T t) where T : new()
 {
     if (PKCache.Count <= 0)
     {
         SetPKCache <T>(t);
     }
     if (PKCache.ContainsKey(t.GetType().FullName))
     {
         return(PKCache[t.GetType().FullName]);//直接返回
     }
     SetPKCache <T>(t);
     if (PKCache.ContainsKey(t.GetType().FullName))
     {
         return(PKCache[t.GetType().FullName]);//再次返回,如没有则说明该Model没有定义主键特性
     }
     return(null);
 }