public override void OnMethodInvocationExpression(MethodInvocationExpression node) { if (node.Target.Entity != null && node.Target.Entity == TypeSystemServices.ICallableType.GetMembers().Single(m => m.Name == "Call")) { var mreOriginal = (MemberReferenceExpression)node.Target; var newMethod = TypeSystemServices.Map(typeof(Delegate).GetMethod("DynamicInvoke", BindingFlags.Instance | BindingFlags.Public)); var newTarget = new MemberReferenceExpression(mreOriginal.Target, newMethod.Name); newTarget.Entity = newMethod; node.Replace(node.Target, newTarget); } else if (node.Target.Entity != null && node.Target.Entity.EntityType == EntityType.Method) { var parameters = ((IMethodBase)node.Target.Entity).GetParameters(); var args = node.Arguments.ToArray(); for (int i = 0; i < args.Length && i < parameters.Length; i++) { if (args[i].ExpressionType != null && TypeSystemServices.IsCallable(args[i].ExpressionType) && !TypeSystemServices.IsCallable(parameters[i].Type)) { node.Replace(args[i], InjectCast(args[i])); } } } base.OnMethodInvocationExpression(node); }
public override void OnBinaryExpression(BinaryExpression node) { if (node.IsDeclarationStatement()) { var localDeclaration = (InternalLocal)node.Left.Entity; var declaration = localDeclaration.OriginalDeclaration; if (declaration == null || declaration.Type == null || declaration.Type.Entity != TypeSystemServices.ICallableType) { return; } declaration.Type = FixTypeToDelegateIfNeeded(declaration.Type); node.Replace(node.Right, InjectCast(node.Right)); } else if (node.Operator == BinaryOperatorType.Assign && node.Left?.ExpressionType?.FullName == "Function" && TypeSystemServices.IsCallable(node.Right.ExpressionType)) { node.Replace(node.Right, InjectCast(node.Right)); } }