您好,登錄后才能下訂單哦!
本篇文章為大家展示了Expression中Convert有什么用,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
public class RefClas { public int id; public int age; public RefClas(int id, int age) { this.id = id; this.age = age; } public static MyStudent RefClasToMyStudent(RefClas rc) { MyStudent st = new MyStudent(); st.id = rc.id; return st; } public static explicit operator RefClas(Int32 id) { RefClas rc = new RefClas(id,0); return rc; } public static implicit operator int(RefClas rc) { return rc.id; } } public class MyStudent { public int id1 { get; set; } //屬性 public int id2; //字段 public int id3 { get { return 10; } } //靜態屬性 public int id4 = 20; //靜態字段 public int id; public int new_id; public DateTime time; public static implicit operator MyStudent(int id) { Console.WriteLine("public static implicit operator MyStudent(int id)" + id); MyStudent st = new MyStudent(); st.id = id; return st; } public static implicit operator int(MyStudent st) { Console.WriteLine("public static implicit operator int(MyStudent st)" + st.id); return st.id; } //public static implicit operator Object(MyStudent st)//不允許進行以基類為轉換源或目標的用戶自定義轉換 //public static implicit operator MyStudent() //{ // Console.WriteLine("public static implicit operator Object(MyStudent st)" + st.id); // return st.id; //} } public class StudentHelper { public int id1 { get; set; } //屬性 public int id2; //字段 public static int id3 { get { return 10; } } //靜態屬性 public static int id4 = 20; //靜態字段 }
StudentHelper h = new StudentHelper(); h.id1 = 1; h.id2 = 2; Expression<Func<MyStudent, bool>> la1 = n => n.id1 == h.id1; Expression<Func<MyStudent, bool>> la2 = n => n.id2 == h.id2; Expression<Func<MyStudent, bool>> la3 = n => n.id3 == StudentHelper.id3; Expression<Func<MyStudent, bool>> la4 = n => n.id4 == StudentHelper.id4; Expression<Func<MyStudent, bool>> la5 = n => n.id == n.new_id;//變量n未定義異常 test(la1, "屬性"); test(la2, "字段"); test(la3, "靜態屬性"); test(la4, "靜態字段"); //test(la5, "自身參數");//在Eval中異常 上面表達式中只有n.new_id有不同 RefClas refClass = new RefClas(20,21); Expression<Func<MyStudent, bool>> la6 = n => n.id == refClass; test(la6, "implicit test"); public static void test(Expression<Func<MyStudent, bool>> la, string name) { Console.WriteLine("\n\n*****************" + name + "*********************"); Expression B_exp; // = la.Body as BinaryExpression; B_exp = la.Body as BinaryExpression; Console.WriteLine("Expression類名:" + ((BinaryExpression)B_exp).Right.GetType().Name); if ((((BinaryExpression)B_exp).Right as UnaryExpression) != null) { UnaryExpression cast = ((BinaryExpression)B_exp).Right as UnaryExpression; var deg = Expression.Lambda<Func<int>>(cast).Compile(); object obj = deg(); Console.WriteLine(B_exp + "值為:" + obj); } else { } MemberExpression m_exp = ((BinaryExpression)B_exp).Right as MemberExpression; //為什么能將B_exp.Right轉換成MemberExpression 有的可能,有的應該不能吧? //la6 = n => n.id == refClass;這一句的時候la.Body.Right是UnaryExpression并不是MemberExpression, 所以m_exp=null,后面if(m_exp.Expression==null)就會異常。 //MemberInitExpression m_exp = B_exp.Right as MemberInitExpression; string valueClassName = string.Empty; if (m_exp.Expression == null) { Console.WriteLine("數據為空"); } else { valueClassName = m_exp.Expression.GetType().Name; Console.WriteLine("數據Expression類名:" + valueClassName); } Console.WriteLine("值為:" + Eval(m_exp)); Console.WriteLine("\n\n*********************************************"); } public static object Eval(MemberExpression member) { if(member.NodeType == ExpressionType.MemberAccess) { if(member.Member.DeclaringType == typeof(MyStudent)) { if(member.Type == typeof(MyStudent)) { //if(member.Expression) } } } //當最右邊的n.new_id這個MemberExpression時(實際是FieldExpression)則打印顯示如下: Console.WriteLine("member.NodeType=" + member.NodeType); Console.WriteLine("member.Member.DeclaringType=" + member.Member.DeclaringType); //是表達式右邊的Member的類型 h.id2時則是StudentHelper Console.WriteLine("member.Member.DeclaringType.Name=" + member.Member.DeclaringType.Name); Console.WriteLine("member.Type=" + member.Type); Console.WriteLine("member.Expression=" + member.Expression); //member.NodeType=MemberAccess //member.Member.DeclaringType=鉤子.MyStudent //member.Member.DeclaringType.Name=MyStudent //member.Type=System.Int32 //member.Expression=n //上面member.Type其實是FieldExpression或PropertyExpression從Expression繼承過來并且override的屬性),代表這個表達式所代表的靜態類型 //為了分析方便,將la2 n => n.id2 == h.id2 的顯示也列在下面: //*****************字段********************* //Expression類名:FieldExpression //數據Expression類名:FieldExpression //member.NodeType=MemberAccess //member.Member.DeclaringType=鉤子.StudentHelper //member.Member.DeclaringType.Name=StudentHelper //member.Type=System.Int32 //member.Expression=value(鉤子.Form1+<>c__DisplayClass8).h //值為:2 //為了分析方便,將la3 = n => n.id3 == StudentHelper.id3的顯示也列在下面: //*****************靜態屬性********************* //Expression類名:PropertyExpression //數據為空 //member.NodeType=MemberAccess //member.Member.DeclaringType=鉤子.StudentHelper //member.Member.DeclaringType.Name=StudentHelper //member.Type=System.Int32 //member.Expression= //值為:10 UnaryExpression cast = null; object obj = null; if(member.Member.DeclaringType.Name.Contains("MyStudent")) { cast = Expression.Convert(member, typeof(int)); var deg = Expression.Lambda<Func<int>>(cast).Compile(); //MyStudent mst = new MyStudent();mst.id=100; int rv = deg(); obj = rv; //obj = Expression.Lambda<Func<int>>(cast).Compile().Invoke();//出現該變量n未定義 } else { cast = Expression.Convert(member, typeof(object)); obj = Expression.Lambda<Func<object>>(cast).Compile().Invoke(); } return obj; }
本代碼最重要的地方是要明白:n.id1 == h.id1或 n.id3 == StudentHelper.id3都可以很正常的執行,但Expression<Func<MyStudent, bool>> la5 = n => n.id == n.new_id;卻需要在RefClas中定義一個轉換運算符函數public static implicit operator int(RefClas rc),目標是MyStudent.id的類型。
另外,還可以直接針對la6 Expression調用Compile()生成運行時委托(因為la6是LambdaExpression,有Compile()方法),然后直接調用委托方法:
RefClas refClass = new RefClas(20,21); Expression<Func<MyStudent, bool>> la6 = n => n.id == refClass; var dla6 = la6.Compile(); MyStudent myStudent = new MyStudent(); myStudent.id = 20; //21 Console.WriteLine("dla6=" + dla6(myStudent));
上述內容就是Expression中Convert有什么用,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。