public static CodeContextScopeExpression CodeContextScope(Expression body, Expression newContext) { ContractUtils.RequiresNotNull(body, "body"); ContractUtils.RequiresNotNull(newContext, "newContext"); ContractUtils.Requires(TypeUtils.AreAssignable(typeof(CodeContext), newContext.Type), "newContext"); return(new CodeContextScopeExpression(body, newContext)); }
public static NewArrayExpression NewArrayHelper(Type type, IEnumerable <Expression> initializers) { ContractUtils.RequiresNotNull(type, nameof(type)); ContractUtils.RequiresNotNull(initializers, nameof(initializers)); if (type.Equals(typeof(void))) { throw new ArgumentException("Argument type cannot be System.Void."); } ReadOnlyCollection <Expression> initializerList = initializers.ToReadOnly(); Expression[] clone = null; for (int i = 0; i < initializerList.Count; i++) { Expression initializer = initializerList[i]; ContractUtils.RequiresNotNull(initializer, nameof(initializers)); if (!TypeUtils.AreReferenceAssignable(type, initializer.Type)) { if (clone == null) { clone = new Expression[initializerList.Count]; for (int j = 0; j < i; j++) { clone[j] = initializerList[j]; } } if (type.IsSubclassOf(typeof(Expression)) && TypeUtils.AreAssignable(type, initializer.GetType())) { initializer = Expression.Quote(initializer); } else { initializer = Convert(initializer, type); } } if (clone != null) { clone[i] = initializer; } } if (clone != null) { initializerList = new ReadOnlyCollection <Expression>(clone); } return(Expression.NewArrayInit(type, initializerList)); }
public static YieldExpression MakeYield(LabelTarget target, Expression value, int yieldMarker) { ContractUtils.RequiresNotNull(target, "target"); ContractUtils.Requires(target.Type != typeof(void), "target", "generator label must have a non-void type"); if (value != null) { if (!TypeUtils.AreReferenceAssignable(target.Type, value.Type)) { // C# autoquotes generator return values if (target.Type.IsSubclassOf(typeof(Expression)) && TypeUtils.AreAssignable(target.Type, value.GetType())) { value = Expression.Quote(value); } throw new ArgumentException(string.Format("Expression of type '{0}' cannot be yielded to a generator label of type '{1}'", value.Type, target.Type)); } } return(new YieldExpression(target, value, yieldMarker)); }