(转)lambda表达式的解析(八) 泛型调用及typeof操作

这篇是这个系列的最后一篇了,对于BlockExpression不再深入展开了,只对之前的泛型调用支持及typeof操作补漏。这次是进一步完善了grammar,现在能正常解析泛型调用及typeof操作,这使得我们能对这2种表达式进行正确的转换了。大家可以从http://tinynetevent.googlecode.com/files/ExpressionParser0809.zip下载到最新的代码。

由于对grammar作了一点修正,所以之前PareeTreeNode的扩展方法GetClrType也需要跟着改了:

  1. public static Type[] GetGenericArguments(this ParseTreeNode node)
  2. {
  3. var typeArgs = node.GetDescendant("type_argument_list");
  4. if (typeArgs == null)
  5. return null;
  6. return (from typeChild in node.GetChild("type_ref_list").ChildNodes
  7. select typeChild.GetClrType()).ToArray();
  8. }
  9. private static Type GetClrType(this ParseTreeNode node)
  10. {
  11. if (node == null)
  12. return null;
  13. if (node.GetName() != "qual_name_with_targs")
  14. node = node.GetDescendant("qual_name_with_targs");
  15. var isNullable = node.GetChild("qmark_opt").FindTokenAndGetText() == "?";
  16. var typeName = node.FindTokenAndGetText();
  17. var type = ExpressionParser.GetType(typeName);
  18. var typeArguments = node.GetGenericArguments();
  19. if (typeArguments != null)
  20. type.MakeGenericType(typeArguments);
  21. if (isNullable)
  22. return typeof(Nullable<>).MakeGenericType(type);
  23. return type;
  24. }
  25. public static Type GetNodeClrType(this ParseTreeNode node)
  26. {
  27. return node.GetDescendant("qual_name_with_targs").GetClrType();
  28. }

泛型调用时肯定是带完整的泛型参数的,比如func<int, int>(a),所以只需要能正确得到泛型参数列表就能通过反射生成正确的带实参泛型方法,成员访问表达式里关于方法调用表达式修改后的代码片段:

  1. case "member_invoke":
  2. var methodName = member.FirstChild.GetValue();
  3. var method = type.GetMethod(methodName);
  4. var targs = member.GetGenericArguments();
  5. if (targs != null)
  6. {
  7. method = method.MakeGenericMethod(targs);
  8. }

至于typeof操作则略有不同,首先typeof是在静态编译时就确定的值,所以可以看做一个ConstantExpression。然后typeof操作可以获得带泛型参数的泛型类型,也就是可以typeof(Func<,>),所以解析的时候针对泛型需要两种方式都支持。typeof视为一元运算符,解析的入口为之前介绍的一元运算符处理方法:

  1. private Expression ProcessUnaryExpression(ParseTreeNode expNode)
  2. {
  3. string op;
  4. var first = expNode.FirstChild;
  5. var second = expNode.LastChild;
  6. switch (first.GetName())
  7. {
  8. ....
  9. case "typeof_expression":
  10. return ProcessTypeofExpression(first);
  11. ....
  12. }

typeof处理方法:

  1. private Expression ProcessTypeofExpression(ParseTreeNode expNode)
  2. {
  3. var node = expNode.GetChild("typeof_parenthesized_expression").GetDescendant("type_ref_typeof");
  4. var glist = node.GetDescendant("type_argument_list_opt");
  5. Type type = node.GetNodeClrType();
  6. if (glist != null)
  7. {
  8. type = Type.GetType(String.Format("{0}`{1}", type.FullName, glist.ChildNodes.Count));
  9. }
  10. var rank = node.GetChild("rank_specifiers_opt");
  11. var ranks = rank.GetDescendant("rank_specifiers");
  12. if (ranks != null)
  13. {
  14. for (int i = ranks.ChildNodes.Count - 1; i >= 0; i--)
  15. {
  16. var r = ranks.ChildNodes[i];
  17. var rs = r.ChildNodes.Count;
  18. type = rs > 1 ? type.MakeArrayType(rs) : type.MakeArrayType();
  19. }
  20. }
  21. return Expression.Constant(type);
  22. }

对于typeof(Func<,>)这种表达式,Func<,>类型就是Func`2,也就是类型名跟一个`符号再跟上参数个数。

而对于typeof(int[][,][,,])这种形式就稍微有点疑惑了,看上去int[][,][,,]的类型应该是Int32[][,][,,],但实际上却是Int32[,,][,][],正好反一反,所以在转换的时候也只能从最后一个开始往前做MakeArrayType了,而且MakeArrayType()与MakeArrayType(1)居然也不一样,因此还需要判断是否是第一个。对C#这个特点有兴趣的可以看看http://topic.csdn.net/u/20110805/16/d1a4226c-68d4-44be-ba65-b56a1614c942.html

基本上到这里关于lambda表达式转换的基本知识都讲完了,基于这些的基础更进一步就是如何把LINQ转换成表达式,也就是说用手工代码把LINQ的查询语法糖解析成表达式,当然提供下载的代码里已经实现了这个,下次就是要讲讲其工作原理。

原文地址:https://www.cnblogs.com/liuhaili/p/2210996.html