本篇文章为大家展示了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有什么用,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:https://my.oschina.net/u/2963604/blog/5038294