获取强类型检测的属性名

在使用EFCodefirst时,由于EF的局限性,不得不让我们去拼一些查询语句,如下:

string sql = string.Format("select ID,[Name] from User");
。。。
但是这样的代码是没有类型检测的,比如某天项目经理强迫症犯了,将User实体类的“ID”改成UserID,那么程序就会报查询异常错误。那怎么做到强类型检测呢?
方法(推荐):
using System.Linq.Expressions; /************************************************************************* *GUID:9d4b9fe1-733c-406c-a6c5-d7537d3b8285  *CLR:4.0.30319.17929 *Machine:  SHS *Creater:史宏生(qq.498576940) *Time:2014/6/19 10:50:20 *Code Caption: ***************************************************************************/ namespace System {     #region Generic class     /// <summary>     /// Strong type checking     /// </summary>     public class Strong<T> where T : class     {         /// <summary>         /// Get strong property name         /// </summary>         public string Sn(Expression<Func<T, object>> exp)         {             return Strong.Sn(exp);         }     }     //invoke:      //Strong<Goods> g = new Strong<Goods>();     //string id = g.Sn(x => x.Id);      #endregion     #region non-generic class     /// <summary>     /// Strong type checking     /// </summary>     public class Strong     {         /// <summary>         /// Get strong property name         /// </summary>         public static string Sn<T>(Expression<Func<T, object>> exp) where T : class         {             if (exp.Body is UnaryExpression)             {                 return ((MemberExpression)((UnaryExpression)exp.Body).Operand).Member.Name;             }             if (exp.Body is MemberExpression)             {                 return ((MemberExpression)exp.Body).Member.Name;             }             if (exp.Body is ParameterExpression)             {                 return exp.Body.Type.Name;             }             return string.Empty;         }         //invoke:string nm = Strong.Sn<Goods>(x => x.Name);      #endregion     } }
 调用:(现在不管你谁怎么修改实体,我都不用再去打理它了)
           string id=Strong.Sn<User>(x => x.UserID); 

           string name=Strong.Sn<User>(x => x.Name); 

           string sqls = string.Format("select {0},{1} from User",id,name); 

 
方法二:该方法就有点笨拙了,不过也不失为一种坚决方案。
namespace System
{
    public static class SystemExtend
    {
        /// <summary>
        ///获取强类型检测的属性名称
        /// </summary>
        /// <param name="t">对象类型</param>
        /// <param name="propertyName">属性名称</param>
        /// <returns>强类型检测的属性名称</returns>
        public static string StrongProperty(this Type t, string propertyName)
        {
            if (t.IsClass)
            {
                try
                {
                    return t.GetProperty(propertyName).Name;
                }
                catch (Exception)
                {
                    throw new Exception(string.Format("请检查\"{0}\"是否为类\"{1}\"的有效属性"propertyNamet.Name));
                }
            }
            else
            {
                throw new Exception(string.Format("\"{0}\"不是有效的类!"t.Name));
            }
        }
    }
 
   
调用:
 
   
    Type t=typeof(User);
    string sql = string.Format("select 0,1 from User",t.StrongProperty("UserID"),t.StrongProperty("Name"));
 
   
现在修改代码,再也不用怕到投鼠忌器了!!!

 

你可能感兴趣的:(属性)