1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004
|
/* ****************************************************************************
*
* Copyright (c) Microsoft Corporation.
*
* This source code is subject to terms and conditions of the Apache License, Version 2.0. A
* copy of the license can be found in the License.html file at the root of this distribution. If
* you cannot locate the Apache License, Version 2.0, please send an email to
* dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
* by the terms of the Apache License, Version 2.0.
*
* You must not remove this notice, or any other, from this software.
*
*
* ***************************************************************************/
using System;
using System.Diagnostics;
using System.Dynamic.Utils;
using System.Reflection;
using System.Runtime.CompilerServices;
#if SILVERLIGHT
using System.Core;
#endif
#if CLR2
namespace Microsoft.Scripting.Ast {
#else
namespace System.Linq.Expressions {
#endif
/// <summary>
/// Represents an expression that has a unary operator.
/// </summary>
#if !SILVERLIGHT
[DebuggerTypeProxy(typeof(Expression.UnaryExpressionProxy))]
#endif
public sealed class UnaryExpression : Expression {
private readonly Expression _operand;
private readonly MethodInfo _method;
private readonly ExpressionType _nodeType;
private readonly Type _type;
internal UnaryExpression(ExpressionType nodeType, Expression expression, Type type, MethodInfo method) {
_operand = expression;
_method = method;
_nodeType = nodeType;
_type = type;
}
/// <summary>
/// Gets the static type of the expression that this <see cref="Expression" /> represents. (Inherited from <see cref="Expression"/>.)
/// </summary>
/// <returns>The <see cref="Type"/> that represents the static type of the expression.</returns>
public sealed override Type Type {
get { return _type; }
}
/// <summary>
/// Returns the node type of this <see cref="Expression" />. (Inherited from <see cref="Expression" />.)
/// </summary>
/// <returns>The <see cref="ExpressionType"/> that represents this expression.</returns>
public sealed override ExpressionType NodeType {
get { return _nodeType; }
}
/// <summary>
/// Gets the operand of the unary operation.
/// </summary>
/// <returns> An <see cref="ExpressionType"/> that represents the operand of the unary operation.</returns>
public Expression Operand {
get { return _operand; }
}
/// <summary>
/// Gets the implementing method for the unary operation.
/// </summary>
/// <returns>The <see cref="MethodInfo"/> that represents the implementing method.</returns>
public MethodInfo Method {
get { return _method; }
}
/// <summary>
/// Gets a value that indicates whether the expression tree node represents a lifted call to an operator.
/// </summary>
/// <returns>true if the node represents a lifted call; otherwise, false.</returns>
public bool IsLifted {
get {
if (NodeType == ExpressionType.TypeAs || NodeType == ExpressionType.Quote || NodeType == ExpressionType.Throw) {
return false;
}
bool operandIsNullable = TypeUtils.IsNullableType(_operand.Type);
bool resultIsNullable = TypeUtils.IsNullableType(this.Type);
if (_method != null) {
return (operandIsNullable && !TypeUtils.AreEquivalent(_method.GetParametersCached()[0].ParameterType, _operand.Type)) ||
(resultIsNullable && !TypeUtils.AreEquivalent(_method.ReturnType, this.Type));
}
return operandIsNullable || resultIsNullable;
}
}
/// <summary>
/// Gets a value that indicates whether the expression tree node represents a lifted call to an operator whose return type is lifted to a nullable type.
/// </summary>
/// <returns>true if the operator's return type is lifted to a nullable type; otherwise, false.</returns>
public bool IsLiftedToNull {
get {
return IsLifted && TypeUtils.IsNullableType(this.Type);
}
}
/// <summary>
/// Dispatches to the specific visit method for this node type.
/// </summary>
protected internal override Expression Accept(ExpressionVisitor visitor) {
return visitor.VisitUnary(this);
}
/// <summary>
/// Gets a value that indicates whether the expression tree node can be reduced.
/// </summary>
public override bool CanReduce {
get {
switch (_nodeType) {
case ExpressionType.PreIncrementAssign:
case ExpressionType.PreDecrementAssign:
case ExpressionType.PostIncrementAssign:
case ExpressionType.PostDecrementAssign:
return true;
}
return false;
}
}
/// <summary>
/// Reduces the expression node to a simpler expression.
/// If CanReduce returns true, this should return a valid expression.
/// This method is allowed to return another node which itself
/// must be reduced.
/// </summary>
/// <returns>The reduced expression.</returns>
public override Expression Reduce() {
if (CanReduce) {
switch (_operand.NodeType) {
case ExpressionType.Index:
return ReduceIndex();
case ExpressionType.MemberAccess:
return ReduceMember();
default:
return ReduceVariable();
}
}
return this;
}
private bool IsPrefix {
get { return _nodeType == ExpressionType.PreIncrementAssign || _nodeType == ExpressionType.PreDecrementAssign; }
}
private UnaryExpression FunctionalOp(Expression operand) {
ExpressionType functional;
if (_nodeType == ExpressionType.PreIncrementAssign || _nodeType == ExpressionType.PostIncrementAssign) {
functional = ExpressionType.Increment;
} else {
functional = ExpressionType.Decrement;
}
return new UnaryExpression(functional, operand, operand.Type, _method);
}
private Expression ReduceVariable() {
if (IsPrefix) {
// (op) var
// ... is reduced into ...
// var = op(var)
return Assign(_operand, FunctionalOp(_operand));
}
// var (op)
// ... is reduced into ...
// temp = var
// var = op(var)
// temp
var temp = Parameter(_operand.Type, null);
return Block(
new[] { temp },
Assign(temp, _operand),
Assign(_operand, FunctionalOp(temp)),
temp
);
}
private Expression ReduceMember() {
var member = (MemberExpression)_operand;
if (member.Expression == null) {
//static member, reduce the same as variable
return ReduceVariable();
} else {
var temp1 = Parameter(member.Expression.Type, null);
var initTemp1 = Assign(temp1, member.Expression);
member = MakeMemberAccess(temp1, member.Member);
if (IsPrefix) {
// (op) value.member
// ... is reduced into ...
// temp1 = value
// temp1.member = op(temp1.member)
return Block(
new[] { temp1 },
initTemp1,
Assign(member, FunctionalOp(member))
);
}
// value.member (op)
// ... is reduced into ...
// temp1 = value
// temp2 = temp1.member
// temp1.member = op(temp2)
// temp2
var temp2 = Parameter(member.Type, null);
return Block(
new[] { temp1, temp2 },
initTemp1,
Assign(temp2, member),
Assign(member, FunctionalOp(temp2)),
temp2
);
}
}
private Expression ReduceIndex() {
// left[a0, a1, ... aN] (op)
//
// ... is reduced into ...
//
// tempObj = left
// tempArg0 = a0
// ...
// tempArgN = aN
// tempValue = tempObj[tempArg0, ... tempArgN]
// tempObj[tempArg0, ... tempArgN] = op(tempValue)
// tempValue
bool prefix = IsPrefix;
var index = (IndexExpression)_operand;
int count = index.Arguments.Count;
var block = new Expression[count + (prefix ? 2 : 4)];
var temps = new ParameterExpression[count + (prefix ? 1 : 2)];
var args = new ParameterExpression[count];
int i = 0;
temps[i] = Parameter(index.Object.Type, null);
block[i] = Assign(temps[i], index.Object);
i++;
while (i <= count) {
var arg = index.Arguments[i - 1];
args[i - 1] = temps[i] = Parameter(arg.Type, null);
block[i] = Assign(temps[i], arg);
i++;
}
index = MakeIndex(temps[0], index.Indexer, new TrueReadOnlyCollection<Expression>(args));
if (!prefix) {
var lastTemp = temps[i] = Parameter(index.Type, null);
block[i] = Assign(temps[i], index);
i++;
Debug.Assert(i == temps.Length);
block[i++] = Assign(index, FunctionalOp(lastTemp));
block[i++] = lastTemp;
} else {
Debug.Assert(i == temps.Length);
block[i++] = Assign(index, FunctionalOp(index));
}
Debug.Assert(i == block.Length);
return Block(new TrueReadOnlyCollection<ParameterExpression>(temps), new TrueReadOnlyCollection<Expression>(block));
}
/// <summary>
/// Creates a new expression that is like this one, but using the
/// supplied children. If all of the children are the same, it will
/// return this expression.
/// </summary>
/// <param name="operand">The <see cref="Operand" /> property of the result.</param>
/// <returns>This expression if no children changed, or an expression with the updated children.</returns>
public UnaryExpression Update(Expression operand) {
if (operand == Operand) {
return this;
}
return Expression.MakeUnary(NodeType, operand, Type, Method);
}
}
public partial class Expression {
/// <summary>
/// Creates a <see cref="UnaryExpression"></see>, given an operand, by calling the appropriate factory method.
/// </summary>
/// <param name="unaryType">The <see cref="ExpressionType"></see> that specifies the type of unary operation.</param>
/// <param name="operand">An <see cref="Expression"></see> that represents the operand.</param>
/// <param name="type">The <see cref="Type"></see> that specifies the type to be converted to (pass null if not applicable).</param>
/// <returns>The <see cref="UnaryExpression"></see> that results from calling the appropriate factory method.</returns>
/// <exception cref="ArgumentException">Thrown when <paramref name="unaryType"/> does not correspond to a unary expression.</exception>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="operand"/> is null.</exception>
public static UnaryExpression MakeUnary(ExpressionType unaryType, Expression operand, Type type) {
return MakeUnary(unaryType, operand, type, null);
}
/// <summary>
/// Creates a <see cref="UnaryExpression"></see>, given an operand and implementing method, by calling the appropriate factory method.
/// </summary>
/// <param name="unaryType">The <see cref="ExpressionType"></see> that specifies the type of unary operation.</param>
/// <param name="operand">An <see cref="Expression"></see> that represents the operand.</param>
/// <param name="type">The <see cref="Type"></see> that specifies the type to be converted to (pass null if not applicable).</param>
/// <param name="method">The <see cref="MethodInfo"></see> that represents the implementing method.</param>
/// <returns>The <see cref="UnaryExpression"></see> that results from calling the appropriate factory method.</returns>
/// <exception cref="ArgumentException">Thrown when <paramref name="unaryType"/> does not correspond to a unary expression.</exception>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="operand"/> is null.</exception>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity")]
public static UnaryExpression MakeUnary(ExpressionType unaryType, Expression operand, Type type, MethodInfo method) {
switch (unaryType) {
case ExpressionType.Negate:
return Negate(operand, method);
case ExpressionType.NegateChecked:
return NegateChecked(operand, method);
case ExpressionType.Not:
return Not(operand, method);
case ExpressionType.IsFalse:
return IsFalse(operand, method);
case ExpressionType.IsTrue:
return IsTrue(operand, method);
case ExpressionType.OnesComplement:
return OnesComplement(operand, method);
case ExpressionType.ArrayLength:
return ArrayLength(operand);
case ExpressionType.Convert:
return Convert(operand, type, method);
case ExpressionType.ConvertChecked:
return ConvertChecked(operand, type, method);
case ExpressionType.Throw:
return Throw(operand, type);
case ExpressionType.TypeAs:
return TypeAs(operand, type);
case ExpressionType.Quote:
return Quote(operand);
case ExpressionType.UnaryPlus:
return UnaryPlus(operand, method);
case ExpressionType.Unbox:
return Unbox(operand, type);
case ExpressionType.Increment:
return Increment(operand, method);
case ExpressionType.Decrement:
return Decrement(operand, method);
case ExpressionType.PreIncrementAssign:
return PreIncrementAssign(operand, method);
case ExpressionType.PostIncrementAssign:
return PostIncrementAssign(operand, method);
case ExpressionType.PreDecrementAssign:
return PreDecrementAssign(operand, method);
case ExpressionType.PostDecrementAssign:
return PostDecrementAssign(operand, method);
default:
throw Error.UnhandledUnary(unaryType);
}
}
private static UnaryExpression GetUserDefinedUnaryOperatorOrThrow(ExpressionType unaryType, string name, Expression operand) {
UnaryExpression u = GetUserDefinedUnaryOperator(unaryType, name, operand);
if (u != null) {
ValidateParamswithOperandsOrThrow(u.Method.GetParametersCached()[0].ParameterType, operand.Type, unaryType, name);
return u;
}
throw Error.UnaryOperatorNotDefined(unaryType, operand.Type);
}
private static UnaryExpression GetUserDefinedUnaryOperator(ExpressionType unaryType, string name, Expression operand) {
Type operandType = operand.Type;
Type[] types = new Type[] { operandType };
Type nnOperandType = TypeUtils.GetNonNullableType(operandType);
BindingFlags flags = BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic;
MethodInfo method = nnOperandType.GetMethodValidated(name, flags, null, types, null);
if (method != null) {
return new UnaryExpression(unaryType, operand, method.ReturnType, method);
}
// try lifted call
if (TypeUtils.IsNullableType(operandType)) {
types[0] = nnOperandType;
method = nnOperandType.GetMethodValidated(name, flags, null, types, null);
if (method != null && method.ReturnType.IsValueType && !TypeUtils.IsNullableType(method.ReturnType)) {
return new UnaryExpression(unaryType, operand, TypeUtils.GetNullableType(method.ReturnType), method);
}
}
return null;
}
private static UnaryExpression GetMethodBasedUnaryOperator(ExpressionType unaryType, Expression operand, MethodInfo method) {
System.Diagnostics.Debug.Assert(method != null);
ValidateOperator(method);
ParameterInfo[] pms = method.GetParametersCached();
if (pms.Length != 1)
throw Error.IncorrectNumberOfMethodCallArguments(method);
if (ParameterIsAssignable(pms[0], operand.Type)) {
ValidateParamswithOperandsOrThrow(pms[0].ParameterType, operand.Type, unaryType, method.Name);
return new UnaryExpression(unaryType, operand, method.ReturnType, method);
}
// check for lifted call
if (TypeUtils.IsNullableType(operand.Type) &&
ParameterIsAssignable(pms[0], TypeUtils.GetNonNullableType(operand.Type)) &&
method.ReturnType.IsValueType && !TypeUtils.IsNullableType(method.ReturnType)) {
return new UnaryExpression(unaryType, operand, TypeUtils.GetNullableType(method.ReturnType), method);
}
throw Error.OperandTypesDoNotMatchParameters(unaryType, method.Name);
}
private static UnaryExpression GetUserDefinedCoercionOrThrow(ExpressionType coercionType, Expression expression, Type convertToType) {
UnaryExpression u = GetUserDefinedCoercion(coercionType, expression, convertToType);
if (u != null) {
return u;
}
throw Error.CoercionOperatorNotDefined(expression.Type, convertToType);
}
private static UnaryExpression GetUserDefinedCoercion(ExpressionType coercionType, Expression expression, Type convertToType) {
MethodInfo method = TypeUtils.GetUserDefinedCoercionMethod(expression.Type, convertToType, false);
if (method != null) {
return new UnaryExpression(coercionType, expression, convertToType, method);
} else {
return null;
}
}
private static UnaryExpression GetMethodBasedCoercionOperator(ExpressionType unaryType, Expression operand, Type convertToType, MethodInfo method) {
System.Diagnostics.Debug.Assert(method != null);
ValidateOperator(method);
ParameterInfo[] pms = method.GetParametersCached();
if (pms.Length != 1) {
throw Error.IncorrectNumberOfMethodCallArguments(method);
}
if (ParameterIsAssignable(pms[0], operand.Type) && TypeUtils.AreEquivalent(method.ReturnType, convertToType)) {
return new UnaryExpression(unaryType, operand, method.ReturnType, method);
}
// check for lifted call
if ((TypeUtils.IsNullableType(operand.Type) || TypeUtils.IsNullableType(convertToType)) &&
ParameterIsAssignable(pms[0], TypeUtils.GetNonNullableType(operand.Type)) &&
TypeUtils.AreEquivalent(method.ReturnType, TypeUtils.GetNonNullableType(convertToType))) {
return new UnaryExpression(unaryType, operand, convertToType, method);
}
throw Error.OperandTypesDoNotMatchParameters(unaryType, method.Name);
}
/// <summary>
/// Creates a <see cref="UnaryExpression"></see> that represents an arithmetic negation operation.
/// </summary>
/// <param name="expression">An <see cref="Expression"></see> to set the <see cref="P:UnaryExpression.Operand"></see> property equal to.</param>
/// <returns>A <see cref="UnaryExpression"></see> that has the <see cref="P:Expression.NodeType"></see> property equal to <see cref="P:ExpressionType.Negate"></see> and the <see cref="P:UnaryExpression.Operand"></see> properties set to the specified value.</returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="expression"/> is null.</exception>
/// <exception cref="InvalidOperationException">Thrown when the unary minus operator is not defined for <see cref="P:Expression.Type"></see></exception>
public static UnaryExpression Negate(Expression expression) {
return Negate(expression, null);
}
/// <summary>
/// Creates a <see cref="UnaryExpression"></see> that represents an arithmetic negation operation.
/// </summary>
/// <param name="expression">An <see cref="Expression"></see> to set the <see cref="P:UnaryExpression.Operand"></see> property equal to.</param>
/// <param name="method">A <see cref="MethodInfo"></see> to set the <see cref="P:UnaryExpression.Method"></see> property equal to.</param>
/// <returns>A <see cref="UnaryExpression"></see> that has the <see cref="P:Expression.NodeType"></see> property equal to <see cref="P:ExpressionType.Negate"></see> and the <see cref="P:UnaryExpression.Operand"></see> and <see cref="P:UnaryExpression.Method"></see> properties set to the specified value.</returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="expression"/> is null.</exception>
/// <exception cref="ArgumentException">Thrown when <paramref name="method"/> is not null and the method it represents returns void, is not static (Shared in Visual Basic), or does not take exactly one argument.</exception>
/// <exception cref="InvalidOperationException">Thown when <paramref name="method"/> is null and the unary minus operator is not defined for expression.Type or expression.Type (or its corresponding non-nullable type if it is a nullable value type) is not assignable to the argument type of the method represented by method.</exception>
public static UnaryExpression Negate(Expression expression, MethodInfo method) {
RequiresCanRead(expression, "expression");
if (method == null) {
if (TypeUtils.IsArithmetic(expression.Type) && !TypeUtils.IsUnsignedInt(expression.Type)) {
return new UnaryExpression(ExpressionType.Negate, expression, expression.Type, null);
}
return GetUserDefinedUnaryOperatorOrThrow(ExpressionType.Negate, "op_UnaryNegation", expression);
}
return GetMethodBasedUnaryOperator(ExpressionType.Negate, expression, method);
}
/// <summary>
/// Creates a <see cref="UnaryExpression"></see> that represents a unary plus operation.
/// </summary>
/// <param name="expression">An <see cref="Expression"></see> to set the <see cref="UnaryExpression.Operand"></see> property equal to.</param>
/// <returns>A <see cref="UnaryExpression"></see> that has the <see cref="Expression.NodeType"></see> property equal to <see cref="ExpressionType.UnaryPlus"></see> and the <see cref="UnaryExpression.Operand"></see> property set to the specified value.</returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="expression"/> is null.</exception>
/// <exception cref="InvalidOperationException">Thown when the unary minus operator is not defined for expression.Type.</exception>
public static UnaryExpression UnaryPlus(Expression expression) {
return UnaryPlus(expression, null);
}
/// <summary>
/// Creates a <see cref="UnaryExpression"></see> that represents a unary plus operation.
/// </summary>
/// <param name="expression">An <see cref="Expression"></see> to set the <see cref="UnaryExpression.Operand"></see> property equal to.</param>
/// <param name="method">A <see cref="MethodInfo"></see> to set the <see cref="UnaryExpression.Method"></see> property equal to.</param>
/// <returns>A <see cref="UnaryExpression"></see> that has the <see cref="Expression.NodeType"></see> property equal to <see cref="ExpressionType.UnaryPlus"></see> and the <see cref="UnaryExpression.Operand"></see> and <see cref="UnaryExpression.Method"></see>property set to the specified value.</returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="expression"/> is null.</exception>
/// <exception cref="ArgumentException">Thrown when <paramref name="method"/> is not null and the method it represents returns void, is not static (Shared in Visual Basic), or does not take exactly one argument.</exception>
/// <exception cref="InvalidOperationException">Thown when <paramref name="method"/> is null and the unary minus operator is not defined for expression.Type or expression.Type (or its corresponding non-nullable type if it is a nullable value type) is not assignable to the argument type of the method represented by method.</exception>
public static UnaryExpression UnaryPlus(Expression expression, MethodInfo method) {
RequiresCanRead(expression, "expression");
if (method == null) {
if (TypeUtils.IsArithmetic(expression.Type)) {
return new UnaryExpression(ExpressionType.UnaryPlus, expression, expression.Type, null);
}
return GetUserDefinedUnaryOperatorOrThrow(ExpressionType.UnaryPlus, "op_UnaryPlus", expression);
}
return GetMethodBasedUnaryOperator(ExpressionType.UnaryPlus, expression, method);
}
/// <summary>Creates a <see cref="T:System.Linq.Expressions.UnaryExpression" /> that represents an arithmetic negation operation that has overflow checking.</summary>
/// <returns>A <see cref="T:System.Linq.Expressions.UnaryExpression" /> that has the <see cref="P:System.Linq.Expressions.Expression.NodeType" /> property equal to <see cref="F:System.Linq.Expressions.ExpressionType.NegateChecked" /> and the <see cref="P:System.Linq.Expressions.UnaryExpression.Operand" /> property set to the specified value.</returns>
/// <param name="expression">An <see cref="T:System.Linq.Expressions.Expression" /> to set the <see cref="P:System.Linq.Expressions.UnaryExpression.Operand" /> property equal to.</param>
/// <exception cref="T:System.ArgumentNullException">
/// Thrown when <paramref name="expression" /> is null.</exception>
/// <exception cref="T:System.InvalidOperationException">Thrown when the unary minus operator is not defined for <paramref name="expression" />.Type.</exception>
public static UnaryExpression NegateChecked(Expression expression) {
return NegateChecked(expression, null);
}
///<summary>Creates a <see cref="T:System.Linq.Expressions.UnaryExpression" /> that represents an arithmetic negation operation that has overflow checking. The implementing method can be specified.</summary>
///<returns>A <see cref="T:System.Linq.Expressions.UnaryExpression" /> that has the <see cref="P:System.Linq.Expressions.Expression.NodeType" /> property equal to <see cref="F:System.Linq.Expressions.ExpressionType.NegateChecked" /> and the <see cref="P:System.Linq.Expressions.UnaryExpression.Operand" /> and <see cref="P:System.Linq.Expressions.UnaryExpression.Method" /> properties set to the specified values.</returns>
///<param name="expression">An <see cref="T:System.Linq.Expressions.Expression" /> to set the <see cref="P:System.Linq.Expressions.UnaryExpression.Operand" /> property equal to.</param>
///<param name="method">A <see cref="T:System.Reflection.MethodInfo" /> to set the <see cref="P:System.Linq.Expressions.UnaryExpression.Method" /> property equal to.</param>
///<exception cref="T:System.ArgumentNullException">
///<paramref name="expression" /> is null.</exception>
///<exception cref="T:System.ArgumentException">
///<paramref name="method" /> is not null and the method it represents returns void, is not static (Shared in Visual Basic), or does not take exactly one argument.</exception>
///<exception cref="T:System.InvalidOperationException">
///<paramref name="method" /> is null and the unary minus operator is not defined for <paramref name="expression" />.Type.-or-<paramref name="expression" />.Type (or its corresponding non-nullable type if it is a nullable value type) is not assignable to the argument type of the method represented by <paramref name="method" />.</exception>
public static UnaryExpression NegateChecked(Expression expression, MethodInfo method) {
RequiresCanRead(expression, "expression");
if (method == null) {
if (TypeUtils.IsArithmetic(expression.Type) && !TypeUtils.IsUnsignedInt(expression.Type)) {
return new UnaryExpression(ExpressionType.NegateChecked, expression, expression.Type, null);
}
return GetUserDefinedUnaryOperatorOrThrow(ExpressionType.NegateChecked, "op_UnaryNegation", expression);
}
return GetMethodBasedUnaryOperator(ExpressionType.NegateChecked, expression, method);
}
///<summary>Creates a <see cref="T:System.Linq.Expressions.UnaryExpression" /> that represents a bitwise complement operation.</summary>
///<returns>A <see cref="T:System.Linq.Expressions.UnaryExpression" /> that has the <see cref="P:System.Linq.Expressions.Expression.NodeType" /> property equal to <see cref="F:System.Linq.Expressions.ExpressionType.Not" /> and the <see cref="P:System.Linq.Expressions.UnaryExpression.Operand" /> property set to the specified value.</returns>
///<param name="expression">An <see cref="T:System.Linq.Expressions.Expression" /> to set the <see cref="P:System.Linq.Expressions.UnaryExpression.Operand" /> property equal to.</param>
///<exception cref="T:System.ArgumentNullException">
///<paramref name="expression" /> is null.</exception>
///<exception cref="T:System.InvalidOperationException">The unary not operator is not defined for <paramref name="expression" />.Type.</exception>
public static UnaryExpression Not(Expression expression) {
return Not(expression, null);
}
///<summary>Creates a <see cref="T:System.Linq.Expressions.UnaryExpression" /> that represents a bitwise complement operation. The implementing method can be specified.</summary>
///<returns>A <see cref="T:System.Linq.Expressions.UnaryExpression" /> that has the <see cref="P:System.Linq.Expressions.Expression.NodeType" /> property equal to <see cref="F:System.Linq.Expressions.ExpressionType.Not" /> and the <see cref="P:System.Linq.Expressions.UnaryExpression.Operand" /> and <see cref="P:System.Linq.Expressions.UnaryExpression.Method" /> properties set to the specified values.</returns>
///<param name="expression">An <see cref="T:System.Linq.Expressions.Expression" /> to set the <see cref="P:System.Linq.Expressions.UnaryExpression.Operand" /> property equal to.</param>
///<param name="method">A <see cref="T:System.Reflection.MethodInfo" /> to set the <see cref="P:System.Linq.Expressions.UnaryExpression.Method" /> property equal to.</param>
///<exception cref="T:System.ArgumentNullException">
///<paramref name="expression" /> is null.</exception>
///<exception cref="T:System.ArgumentException">
///<paramref name="method" /> is not null and the method it represents returns void, is not static (Shared in Visual Basic), or does not take exactly one argument.</exception>
///<exception cref="T:System.InvalidOperationException">
///<paramref name="method" /> is null and the unary not operator is not defined for <paramref name="expression" />.Type.-or-<paramref name="expression" />.Type (or its corresponding non-nullable type if it is a nullable value type) is not assignable to the argument type of the method represented by <paramref name="method" />.</exception>
public static UnaryExpression Not(Expression expression, MethodInfo method) {
RequiresCanRead(expression, "expression");
if (method == null) {
if (TypeUtils.IsIntegerOrBool(expression.Type)) {
return new UnaryExpression(ExpressionType.Not, expression, expression.Type, null);
}
UnaryExpression u = GetUserDefinedUnaryOperator(ExpressionType.Not, "op_LogicalNot", expression);
if (u != null) {
return u;
}
return GetUserDefinedUnaryOperatorOrThrow(ExpressionType.Not, "op_OnesComplement", expression);
}
return GetMethodBasedUnaryOperator(ExpressionType.Not, expression, method);
}
/// <summary>
/// Returns whether the expression evaluates to false.
/// </summary>
/// <param name="expression">An <see cref="T:System.Linq.Expressions.Expression" /> to evaluate.</param>
/// <returns>An instance of <see cref="UnaryExpression"/>.</returns>
public static UnaryExpression IsFalse(Expression expression) {
return IsFalse(expression, null);
}
/// <summary>
/// Returns whether the expression evaluates to false.
/// </summary>
///<param name="expression">An <see cref="T:System.Linq.Expressions.Expression" /> to evaluate.</param>
///<param name="method">A <see cref="T:System.Reflection.MethodInfo" /> that represents the implementing method.</param>
/// <returns>An instance of <see cref="UnaryExpression"/>.</returns>
public static UnaryExpression IsFalse(Expression expression, MethodInfo method) {
RequiresCanRead(expression, "expression");
if (method == null) {
if (TypeUtils.IsBool(expression.Type)) {
return new UnaryExpression(ExpressionType.IsFalse, expression, expression.Type, null);
}
return GetUserDefinedUnaryOperatorOrThrow(ExpressionType.IsFalse, "op_False", expression);
}
return GetMethodBasedUnaryOperator(ExpressionType.IsFalse, expression, method);
}
/// <summary>
/// Returns whether the expression evaluates to true.
/// </summary>
/// <param name="expression">An <see cref="T:System.Linq.Expressions.Expression" /> to evaluate.</param>
/// <returns>An instance of <see cref="UnaryExpression"/>.</returns>
public static UnaryExpression IsTrue(Expression expression) {
return IsTrue(expression, null);
}
/// <summary>
/// Returns whether the expression evaluates to true.
/// </summary>
///<param name="expression">An <see cref="T:System.Linq.Expressions.Expression" /> to evaluate.</param>
///<param name="method">A <see cref="T:System.Reflection.MethodInfo" /> that represents the implementing method.</param>
/// <returns>An instance of <see cref="UnaryExpression"/>.</returns>
public static UnaryExpression IsTrue(Expression expression, MethodInfo method) {
RequiresCanRead(expression, "expression");
if (method == null) {
if (TypeUtils.IsBool(expression.Type)) {
return new UnaryExpression(ExpressionType.IsTrue, expression, expression.Type, null);
}
return GetUserDefinedUnaryOperatorOrThrow(ExpressionType.IsTrue, "op_True", expression);
}
return GetMethodBasedUnaryOperator(ExpressionType.IsTrue, expression, method);
}
/// <summary>
/// Returns the expression representing the ones complement.
/// </summary>
///<param name="expression">An <see cref="T:System.Linq.Expressions.Expression" />.</param>
/// <returns>An instance of <see cref="UnaryExpression"/>.</returns>
public static UnaryExpression OnesComplement(Expression expression) {
return OnesComplement(expression, null);
}
/// <summary>
/// Returns the expression representing the ones complement.
/// </summary>
/// <param name="expression">An <see cref="T:System.Linq.Expressions.Expression" />.</param>
/// <param name="method">A <see cref="T:System.Reflection.MethodInfo" /> that represents the implementing method.</param>
/// <returns>An instance of <see cref="UnaryExpression"/>.</returns>
public static UnaryExpression OnesComplement(Expression expression, MethodInfo method) {
RequiresCanRead(expression, "expression");
if (method == null) {
if (TypeUtils.IsInteger(expression.Type)) {
return new UnaryExpression(ExpressionType.OnesComplement, expression, expression.Type, null);
}
return GetUserDefinedUnaryOperatorOrThrow(ExpressionType.OnesComplement, "op_OnesComplement", expression);
}
return GetMethodBasedUnaryOperator(ExpressionType.OnesComplement, expression, method);
}
///<summary>Creates a <see cref="T:System.Linq.Expressions.UnaryExpression" /> that represents an explicit reference or boxing conversion where null is supplied if the conversion fails.</summary>
///<returns>A <see cref="T:System.Linq.Expressions.UnaryExpression" /> that has the <see cref="P:System.Linq.Expressions.Expression.NodeType" /> property equal to <see cref="F:System.Linq.Expressions.ExpressionType.TypeAs" /> and the <see cref="P:System.Linq.Expressions.UnaryExpression.Operand" /> and <see cref="P:System.Linq.Expressions.Expression.Type" /> properties set to the specified values.</returns>
///<param name="expression">An <see cref="T:System.Linq.Expressions.Expression" /> to set the <see cref="P:System.Linq.Expressions.UnaryExpression.Operand" /> property equal to.</param>
///<param name="type">A <see cref="T:System.Type" /> to set the <see cref="P:System.Linq.Expressions.Expression.Type" /> property equal to.</param>
///<exception cref="T:System.ArgumentNullException">
///<paramref name="expression" /> or <paramref name="type" /> is null.</exception>
public static UnaryExpression TypeAs(Expression expression, Type type) {
RequiresCanRead(expression, "expression");
ContractUtils.RequiresNotNull(type, "type");
TypeUtils.ValidateType(type);
if (type.IsValueType && !TypeUtils.IsNullableType(type)) {
throw Error.IncorrectTypeForTypeAs(type);
}
return new UnaryExpression(ExpressionType.TypeAs, expression, type, null);
}
/// <summary>
/// <summary>Creates a <see cref="T:System.Linq.Expressions.UnaryExpression" /> that represents an explicit unboxing.</summary>
/// </summary>
/// <param name="expression">An <see cref="T:System.Linq.Expressions.Expression" /> to unbox.</param>
/// <param name="type">The new <see cref="T:System.Type" /> of the expression.</param>
/// <returns>An instance of <see cref="UnaryExpression"/>.</returns>
public static UnaryExpression Unbox(Expression expression, Type type) {
RequiresCanRead(expression, "expression");
ContractUtils.RequiresNotNull(type, "type");
if (!expression.Type.IsInterface && expression.Type != typeof(object)) {
throw Error.InvalidUnboxType();
}
if (!type.IsValueType) throw Error.InvalidUnboxType();
TypeUtils.ValidateType(type);
return new UnaryExpression(ExpressionType.Unbox, expression, type, null);
}
///<summary>Creates a <see cref="T:System.Linq.Expressions.UnaryExpression" /> that represents a conversion operation.</summary>
///<returns>A <see cref="T:System.Linq.Expressions.UnaryExpression" /> that has the <see cref="P:System.Linq.Expressions.Expression.NodeType" /> property equal to <see cref="F:System.Linq.Expressions.ExpressionType.Convert" /> and the <see cref="P:System.Linq.Expressions.UnaryExpression.Operand" /> and <see cref="P:System.Linq.Expressions.Expression.Type" /> properties set to the specified values.</returns>
///<param name="expression">An <see cref="T:System.Linq.Expressions.Expression" /> to set the <see cref="P:System.Linq.Expressions.UnaryExpression.Operand" /> property equal to.</param>
///<param name="type">A <see cref="T:System.Type" /> to set the <see cref="P:System.Linq.Expressions.Expression.Type" /> property equal to.</param>
///<exception cref="T:System.ArgumentNullException">
///<paramref name="expression" /> or <paramref name="type" /> is null.</exception>
///<exception cref="T:System.InvalidOperationException">No conversion operator is defined between <paramref name="expression" />.Type and <paramref name="type" />.</exception>
public static UnaryExpression Convert(Expression expression, Type type) {
return Convert(expression, type, null);
}
///<summary>Creates a <see cref="T:System.Linq.Expressions.UnaryExpression" /> that represents a conversion operation for which the implementing method is specified.</summary>
///<returns>A <see cref="T:System.Linq.Expressions.UnaryExpression" /> that has the <see cref="P:System.Linq.Expressions.Expression.NodeType" /> property equal to <see cref="F:System.Linq.Expressions.ExpressionType.Convert" /> and the <see cref="P:System.Linq.Expressions.UnaryExpression.Operand" />, <see cref="P:System.Linq.Expressions.Expression.Type" />, and <see cref="P:System.Linq.Expressions.UnaryExpression.Method" /> properties set to the specified values.</returns>
///<param name="expression">An <see cref="T:System.Linq.Expressions.Expression" /> to set the <see cref="P:System.Linq.Expressions.UnaryExpression.Operand" /> property equal to.</param>
///<param name="type">A <see cref="T:System.Type" /> to set the <see cref="P:System.Linq.Expressions.Expression.Type" /> property equal to.</param>
///<param name="method">A <see cref="T:System.Reflection.MethodInfo" /> to set the <see cref="P:System.Linq.Expressions.UnaryExpression.Method" /> property equal to.</param>
///<exception cref="T:System.ArgumentNullException">
///<paramref name="expression" /> or <paramref name="type" /> is null.</exception>
///<exception cref="T:System.ArgumentException">
///<paramref name="method" /> is not null and the method it represents returns void, is not static (Shared in Visual Basic), or does not take exactly one argument.</exception>
///<exception cref="T:System.Reflection.AmbiguousMatchException">More than one method that matches the <paramref name="method" /> description was found.</exception>
///<exception cref="T:System.InvalidOperationException">No conversion operator is defined between <paramref name="expression" />.Type and <paramref name="type" />.-or-<paramref name="expression" />.Type is not assignable to the argument type of the method represented by <paramref name="method" />.-or-The return type of the method represented by <paramref name="method" /> is not assignable to <paramref name="type" />.-or-<paramref name="expression" />.Type or <paramref name="type" /> is a nullable value type and the corresponding non-nullable value type does not equal the argument type or the return type, respectively, of the method represented by <paramref name="method" />.</exception>
public static UnaryExpression Convert(Expression expression, Type type, MethodInfo method) {
RequiresCanRead(expression, "expression");
ContractUtils.RequiresNotNull(type, "type");
TypeUtils.ValidateType(type);
if (method == null) {
if (TypeUtils.HasIdentityPrimitiveOrNullableConversion(expression.Type, type) ||
TypeUtils.HasReferenceConversion(expression.Type, type)) {
return new UnaryExpression(ExpressionType.Convert, expression, type, null);
}
return GetUserDefinedCoercionOrThrow(ExpressionType.Convert, expression, type);
}
return GetMethodBasedCoercionOperator(ExpressionType.Convert, expression, type, method);
}
///<summary>Creates a <see cref="T:System.Linq.Expressions.UnaryExpression" /> that represents a conversion operation that throws an exception if the target type is overflowed.</summary>
///<returns>A <see cref="T:System.Linq.Expressions.UnaryExpression" /> that has the <see cref="P:System.Linq.Expressions.Expression.NodeType" /> property equal to <see cref="F:System.Linq.Expressions.ExpressionType.ConvertChecked" /> and the <see cref="P:System.Linq.Expressions.UnaryExpression.Operand" /> and <see cref="P:System.Linq.Expressions.Expression.Type" /> properties set to the specified values.</returns>
///<param name="expression">An <see cref="T:System.Linq.Expressions.Expression" /> to set the <see cref="P:System.Linq.Expressions.UnaryExpression.Operand" /> property equal to.</param>
///<param name="type">A <see cref="T:System.Type" /> to set the <see cref="P:System.Linq.Expressions.Expression.Type" /> property equal to.</param>
///<exception cref="T:System.ArgumentNullException">
///<paramref name="expression" /> or <paramref name="type" /> is null.</exception>
///<exception cref="T:System.InvalidOperationException">No conversion operator is defined between <paramref name="expression" />.Type and <paramref name="type" />.</exception>
public static UnaryExpression ConvertChecked(Expression expression, Type type) {
return ConvertChecked(expression, type, null);
}
///<summary>Creates a <see cref="T:System.Linq.Expressions.UnaryExpression" /> that represents a conversion operation that throws an exception if the target type is overflowed and for which the implementing method is specified.</summary>
///<returns>A <see cref="T:System.Linq.Expressions.UnaryExpression" /> that has the <see cref="P:System.Linq.Expressions.Expression.NodeType" /> property equal to <see cref="F:System.Linq.Expressions.ExpressionType.ConvertChecked" /> and the <see cref="P:System.Linq.Expressions.UnaryExpression.Operand" />, <see cref="P:System.Linq.Expressions.Expression.Type" />, and <see cref="P:System.Linq.Expressions.UnaryExpression.Method" /> properties set to the specified values.</returns>
///<param name="expression">An <see cref="T:System.Linq.Expressions.Expression" /> to set the <see cref="P:System.Linq.Expressions.UnaryExpression.Operand" /> property equal to.</param>
///<param name="type">A <see cref="T:System.Type" /> to set the <see cref="P:System.Linq.Expressions.Expression.Type" /> property equal to.</param>
///<param name="method">A <see cref="T:System.Reflection.MethodInfo" /> to set the <see cref="P:System.Linq.Expressions.UnaryExpression.Method" /> property equal to.</param>
///<exception cref="T:System.ArgumentNullException">
///<paramref name="expression" /> or <paramref name="type" /> is null.</exception>
///<exception cref="T:System.ArgumentException">
///<paramref name="method" /> is not null and the method it represents returns void, is not static (Shared in Visual Basic), or does not take exactly one argument.</exception>
///<exception cref="T:System.Reflection.AmbiguousMatchException">More than one method that matches the <paramref name="method" /> description was found.</exception>
///<exception cref="T:System.InvalidOperationException">No conversion operator is defined between <paramref name="expression" />.Type and <paramref name="type" />.-or-<paramref name="expression" />.Type is not assignable to the argument type of the method represented by <paramref name="method" />.-or-The return type of the method represented by <paramref name="method" /> is not assignable to <paramref name="type" />.-or-<paramref name="expression" />.Type or <paramref name="type" /> is a nullable value type and the corresponding non-nullable value type does not equal the argument type or the return type, respectively, of the method represented by <paramref name="method" />.</exception>
public static UnaryExpression ConvertChecked(Expression expression, Type type, MethodInfo method) {
RequiresCanRead(expression, "expression");
ContractUtils.RequiresNotNull(type, "type");
TypeUtils.ValidateType(type);
if (method == null) {
if (TypeUtils.HasIdentityPrimitiveOrNullableConversion(expression.Type, type)) {
return new UnaryExpression(ExpressionType.ConvertChecked, expression, type, null);
}
if (TypeUtils.HasReferenceConversion(expression.Type, type)) {
return new UnaryExpression(ExpressionType.Convert, expression, type, null);
}
return GetUserDefinedCoercionOrThrow(ExpressionType.ConvertChecked, expression, type);
}
return GetMethodBasedCoercionOperator(ExpressionType.ConvertChecked, expression, type, method);
}
///<summary>Creates a <see cref="T:System.Linq.Expressions.UnaryExpression" /> that represents getting the length of a one-dimensional array.</summary>
///<returns>A <see cref="T:System.Linq.Expressions.UnaryExpression" /> that has the <see cref="P:System.Linq.Expressions.Expression.NodeType" /> property equal to <see cref="F:System.Linq.Expressions.ExpressionType.ArrayLength" /> and the <see cref="P:System.Linq.Expressions.UnaryExpression.Operand" /> property equal to <paramref name="array" />.</returns>
///<param name="array">An <see cref="T:System.Linq.Expressions.Expression" /> to set the <see cref="P:System.Linq.Expressions.UnaryExpression.Operand" /> property equal to.</param>
///<exception cref="T:System.ArgumentNullException">
///<paramref name="array" /> is null.</exception>
///<exception cref="T:System.ArgumentException">
///<paramref name="array" />.Type does not represent an array type.</exception>
public static UnaryExpression ArrayLength(Expression array) {
ContractUtils.RequiresNotNull(array, "array");
if (!array.Type.IsArray || !typeof(Array).IsAssignableFrom(array.Type)) {
throw Error.ArgumentMustBeArray();
}
if (array.Type.GetArrayRank() != 1) {
throw Error.ArgumentMustBeSingleDimensionalArrayType();
}
return new UnaryExpression(ExpressionType.ArrayLength, array, typeof(int), null);
}
///<summary>Creates a <see cref="T:System.Linq.Expressions.UnaryExpression" /> that represents an expression that has a constant value of type <see cref="T:System.Linq.Expressions.Expression" />.</summary>
///<returns>A <see cref="T:System.Linq.Expressions.UnaryExpression" /> that has the <see cref="P:System.Linq.Expressions.Expression.NodeType" /> property equal to <see cref="F:System.Linq.Expressions.ExpressionType.Quote" /> and the <see cref="P:System.Linq.Expressions.UnaryExpression.Operand" /> property set to the specified value.</returns>
///<param name="expression">An <see cref="T:System.Linq.Expressions.Expression" /> to set the <see cref="P:System.Linq.Expressions.UnaryExpression.Operand" /> property equal to.</param>
///<exception cref="T:System.ArgumentNullException">
///<paramref name="expression" /> is null.</exception>
public static UnaryExpression Quote(Expression expression) {
RequiresCanRead(expression, "expression");
bool validQuote = expression is LambdaExpression;
#if SILVERLIGHT
validQuote |= SilverlightQuirks;
#endif
if (!validQuote) throw Error.QuotedExpressionMustBeLambda();
return new UnaryExpression(ExpressionType.Quote, expression, expression.GetType(), null);
}
/// <summary>
/// Creates a <see cref="T:System.Linq.Expressions.UnaryExpression" /> that represents a rethrowing of an exception.
/// </summary>
/// <returns>A <see cref="T:System.Linq.Expressions.UnaryExpression"/> that represents a rethrowing of an exception.</returns>
public static UnaryExpression Rethrow() {
return Throw(null);
}
/// <summary>
/// Creates a <see cref="T:System.Linq.Expressions.UnaryExpression" /> that represents a rethrowing of an exception with a given type.
/// </summary>
///<param name="type">The new <see cref="T:System.Type" /> of the expression.</param>
/// <returns>A <see cref="T:System.Linq.Expressions.UnaryExpression"/> that represents a rethrowing of an exception.</returns>
public static UnaryExpression Rethrow(Type type) {
return Throw(null, type);
}
/// <summary>
/// Creates a <see cref="T:System.Linq.Expressions.UnaryExpression" /> that represents a throwing of an exception.
/// </summary>
/// <param name="value">An <see cref="T:System.Linq.Expressions.Expression" />.</param>
/// <returns>A <see cref="T:System.Linq.Expressions.UnaryExpression"/> that represents the exception.</returns>
public static UnaryExpression Throw(Expression value) {
return Throw(value, typeof(void));
}
/// <summary>
/// Creates a <see cref="T:System.Linq.Expressions.UnaryExpression" /> that represents a throwing of a value with a given type.
/// </summary>
/// <param name="value">An <see cref="T:System.Linq.Expressions.Expression" />.</param>
/// <param name="type">The new <see cref="T:System.Type" /> of the expression.</param>
/// <returns>A <see cref="T:System.Linq.Expressions.UnaryExpression"/> that represents the exception.</returns>
public static UnaryExpression Throw(Expression value, Type type) {
ContractUtils.RequiresNotNull(type, "type");
TypeUtils.ValidateType(type);
if (value != null) {
RequiresCanRead(value, "value");
if (value.Type.IsValueType) throw Error.ArgumentMustNotHaveValueType();
}
return new UnaryExpression(ExpressionType.Throw, value, type, null);
}
/// <summary>
/// Creates a <see cref="T:System.Linq.Expressions.UnaryExpression" /> that represents the incrementing of the expression by 1.
/// </summary>
/// <param name="expression">An <see cref="T:System.Linq.Expressions.Expression"></see> to increment.</param>
/// <returns>A <see cref="T:System.Linq.Expressions.UnaryExpression"/> that represents the incremented expression.</returns>
public static UnaryExpression Increment(Expression expression) {
return Increment(expression, null);
}
/// <summary>
/// Creates a <see cref="T:System.Linq.Expressions.UnaryExpression" /> that represents the incrementing of the expression by 1.
/// </summary>
/// <param name="expression">An <see cref="T:System.Linq.Expressions.Expression"></see> to increment.</param>
///<param name="method">A <see cref="T:System.Reflection.MethodInfo" /> that represents the implementing method.</param>
/// <returns>A <see cref="T:System.Linq.Expressions.UnaryExpression"/> that represents the incremented expression.</returns>
public static UnaryExpression Increment(Expression expression, MethodInfo method) {
RequiresCanRead(expression, "expression");
if (method == null) {
if (TypeUtils.IsArithmetic(expression.Type)) {
return new UnaryExpression(ExpressionType.Increment, expression, expression.Type, null);
}
return GetUserDefinedUnaryOperatorOrThrow(ExpressionType.Increment, "op_Increment", expression);
}
return GetMethodBasedUnaryOperator(ExpressionType.Increment, expression, method);
}
/// <summary>
/// Creates a <see cref="T:System.Linq.Expressions.UnaryExpression" /> that represents the decrementing of the expression by 1.
/// </summary>
/// <param name="expression">An <see cref="T:System.Linq.Expressions.Expression"></see> to decrement.</param>
/// <returns>A <see cref="T:System.Linq.Expressions.UnaryExpression"/> that represents the decremented expression.</returns>
public static UnaryExpression Decrement(Expression expression) {
return Decrement(expression, null);
}
/// <summary>
/// Creates a <see cref="T:System.Linq.Expressions.UnaryExpression" /> that represents the decrementing of the expression by 1.
/// </summary>
/// <param name="expression">An <see cref="T:System.Linq.Expressions.Expression"></see> to decrement.</param>
///<param name="method">A <see cref="T:System.Reflection.MethodInfo" /> that represents the implementing method.</param>
/// <returns>A <see cref="T:System.Linq.Expressions.UnaryExpression"/> that represents the decremented expression.</returns>
public static UnaryExpression Decrement(Expression expression, MethodInfo method) {
RequiresCanRead(expression, "expression");
if (method == null) {
if (TypeUtils.IsArithmetic(expression.Type)) {
return new UnaryExpression(ExpressionType.Decrement, expression, expression.Type, null);
}
return GetUserDefinedUnaryOperatorOrThrow(ExpressionType.Decrement, "op_Decrement", expression);
}
return GetMethodBasedUnaryOperator(ExpressionType.Decrement, expression, method);
}
/// <summary>
/// Creates a <see cref="UnaryExpression"/> that increments the expression by 1
/// and assigns the result back to the expression.
/// </summary>
/// <param name="expression">An <see cref="T:System.Linq.Expressions.Expression"></see> to apply the operations on.</param>
/// <returns>A <see cref="T:System.Linq.Expressions.UnaryExpression"/> that represents the resultant expression.</returns>
public static UnaryExpression PreIncrementAssign(Expression expression) {
return MakeOpAssignUnary(ExpressionType.PreIncrementAssign, expression, null);
}
/// <summary>
/// Creates a <see cref="UnaryExpression"/> that increments the expression by 1
/// and assigns the result back to the expression.
/// </summary>
/// <param name="expression">An <see cref="T:System.Linq.Expressions.Expression"></see> to apply the operations on.</param>
/// <param name="method">A <see cref="T:System.Reflection.MethodInfo" /> that represents the implementing method.</param>
/// <returns>A <see cref="T:System.Linq.Expressions.UnaryExpression"/> that represents the resultant expression.</returns>
public static UnaryExpression PreIncrementAssign(Expression expression, MethodInfo method) {
return MakeOpAssignUnary(ExpressionType.PreIncrementAssign, expression, method);
}
/// <summary>
/// Creates a <see cref="UnaryExpression"/> that decrements the expression by 1
/// and assigns the result back to the expression.
/// </summary>
/// <param name="expression">An <see cref="T:System.Linq.Expressions.Expression"></see> to apply the operations on.</param>
/// <returns>A <see cref="T:System.Linq.Expressions.UnaryExpression"/> that represents the resultant expression.</returns>
public static UnaryExpression PreDecrementAssign(Expression expression) {
return MakeOpAssignUnary(ExpressionType.PreDecrementAssign, expression, null);
}
/// <summary>
/// Creates a <see cref="UnaryExpression"/> that decrements the expression by 1
/// and assigns the result back to the expression.
/// </summary>
/// <param name="expression">An <see cref="T:System.Linq.Expressions.Expression"></see> to apply the operations on.</param>
/// <param name="method">A <see cref="T:System.Reflection.MethodInfo" /> that represents the implementing method.</param>
/// <returns>A <see cref="T:System.Linq.Expressions.UnaryExpression"/> that represents the resultant expression.</returns>
public static UnaryExpression PreDecrementAssign(Expression expression, MethodInfo method) {
return MakeOpAssignUnary(ExpressionType.PreDecrementAssign, expression, method);
}
/// <summary>
/// Creates a <see cref="UnaryExpression"/> that represents the assignment of the expression
/// followed by a subsequent increment by 1 of the original expression.
/// </summary>
/// <param name="expression">An <see cref="T:System.Linq.Expressions.Expression"></see> to apply the operations on.</param>
/// <returns>A <see cref="T:System.Linq.Expressions.UnaryExpression"/> that represents the resultant expression.</returns>
public static UnaryExpression PostIncrementAssign(Expression expression) {
return MakeOpAssignUnary(ExpressionType.PostIncrementAssign, expression, null);
}
/// <summary>
/// Creates a <see cref="UnaryExpression"/> that represents the assignment of the expression
/// followed by a subsequent increment by 1 of the original expression.
/// </summary>
/// <param name="expression">An <see cref="T:System.Linq.Expressions.Expression"></see> to apply the operations on.</param>
/// <param name="method">A <see cref="T:System.Reflection.MethodInfo" /> that represents the implementing method.</param>
/// <returns>A <see cref="T:System.Linq.Expressions.UnaryExpression"/> that represents the resultant expression.</returns>
public static UnaryExpression PostIncrementAssign(Expression expression, MethodInfo method) {
return MakeOpAssignUnary(ExpressionType.PostIncrementAssign, expression, method);
}
/// <summary>
/// Creates a <see cref="UnaryExpression"/> that represents the assignment of the expression
/// followed by a subsequent decrement by 1 of the original expression.
/// </summary>
/// <param name="expression">An <see cref="T:System.Linq.Expressions.Expression"></see> to apply the operations on.</param>
/// <returns>A <see cref="T:System.Linq.Expressions.UnaryExpression"/> that represents the resultant expression.</returns>
public static UnaryExpression PostDecrementAssign(Expression expression) {
return MakeOpAssignUnary(ExpressionType.PostDecrementAssign, expression, null);
}
/// <summary>
/// Creates a <see cref="UnaryExpression"/> that represents the assignment of the expression
/// followed by a subsequent decrement by 1 of the original expression.
/// </summary>
/// <param name="expression">An <see cref="T:System.Linq.Expressions.Expression"></see> to apply the operations on.</param>
/// <param name="method">A <see cref="T:System.Reflection.MethodInfo" /> that represents the implementing method.</param>
/// <returns>A <see cref="T:System.Linq.Expressions.UnaryExpression"/> that represents the resultant expression.</returns>
public static UnaryExpression PostDecrementAssign(Expression expression, MethodInfo method) {
return MakeOpAssignUnary(ExpressionType.PostDecrementAssign, expression, method);
}
private static UnaryExpression MakeOpAssignUnary(ExpressionType kind, Expression expression, MethodInfo method) {
RequiresCanRead(expression, "expression");
RequiresCanWrite(expression, "expression");
UnaryExpression result;
if (method == null) {
if (TypeUtils.IsArithmetic(expression.Type)) {
return new UnaryExpression(kind, expression, expression.Type, null);
}
string name;
if (kind == ExpressionType.PreIncrementAssign || kind == ExpressionType.PostIncrementAssign) {
name = "op_Increment";
} else {
name = "op_Decrement";
}
result = GetUserDefinedUnaryOperatorOrThrow(kind, name, expression);
} else {
result = GetMethodBasedUnaryOperator(kind, expression, method);
}
// return type must be assignable back to the operand type
if (!TypeUtils.AreReferenceAssignable(expression.Type, result.Type)) {
throw Error.UserDefinedOpMustHaveValidReturnType(kind, method.Name);
}
return result;
}
}
}
|