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
|
// ---------------------------------------------------------------------------
// Copyright (C) 2005 Microsoft Corporation All Rights Reserved
// ---------------------------------------------------------------------------
#pragma warning disable 1634, 1691
#define CODE_ANALYSIS
using System.CodeDom;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Reflection;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Compiler;
namespace System.Workflow.Activities.Rules
{
#region RuleExpressionResult class hierarchy
public abstract class RuleExpressionResult
{
public abstract object Value { get; set; }
}
public class RuleLiteralResult : RuleExpressionResult
{
private object literal;
public RuleLiteralResult(object literal)
{
this.literal = literal;
}
public override object Value
{
get
{
return literal;
}
set
{
throw new InvalidOperationException(Messages.CannotWriteToExpression);
}
}
}
internal class RuleFieldResult : RuleExpressionResult
{
private object targetObject;
private FieldInfo fieldInfo;
public RuleFieldResult(object targetObject, FieldInfo fieldInfo)
{
if (fieldInfo == null)
throw new ArgumentNullException("fieldInfo");
this.targetObject = targetObject;
this.fieldInfo = fieldInfo;
}
public override object Value
{
get
{
#pragma warning disable 56503
if (!fieldInfo.IsStatic && targetObject == null)
{
// Accessing a non-static field from null target.
string message = string.Format(CultureInfo.CurrentCulture, Messages.TargetEvaluatedNullField, fieldInfo.Name);
RuleEvaluationException exception = new RuleEvaluationException(message);
exception.Data[RuleUserDataKeys.ErrorObject] = fieldInfo;
throw exception;
}
return fieldInfo.GetValue(targetObject);
#pragma warning restore 56503
}
set
{
if (!fieldInfo.IsStatic && targetObject == null)
{
// Accessing a non-static field from null target.
string message = string.Format(CultureInfo.CurrentCulture, Messages.TargetEvaluatedNullField, fieldInfo.Name);
RuleEvaluationException exception = new RuleEvaluationException(message);
exception.Data[RuleUserDataKeys.ErrorObject] = fieldInfo;
throw exception;
}
fieldInfo.SetValue(targetObject, value);
}
}
}
internal class RulePropertyResult : RuleExpressionResult
{
private PropertyInfo propertyInfo;
private object targetObject;
private object[] indexerArguments;
public RulePropertyResult(PropertyInfo propertyInfo, object targetObject, object[] indexerArguments)
{
if (propertyInfo == null)
throw new ArgumentNullException("propertyInfo");
this.targetObject = targetObject;
this.propertyInfo = propertyInfo;
this.indexerArguments = indexerArguments;
}
public override object Value
{
get
{
#pragma warning disable 56503
if (!propertyInfo.GetGetMethod(true).IsStatic && targetObject == null)
{
string message = string.Format(CultureInfo.CurrentCulture, Messages.TargetEvaluatedNullProperty, propertyInfo.Name);
RuleEvaluationException exception = new RuleEvaluationException(message);
exception.Data[RuleUserDataKeys.ErrorObject] = propertyInfo;
throw exception;
}
try
{
return propertyInfo.GetValue(targetObject, indexerArguments);
}
catch (TargetInvocationException e)
{
// if there is no inner exception, leave it untouched
if (e.InnerException == null)
throw;
string message = string.Format(CultureInfo.CurrentCulture, Messages.Error_PropertyGet,
RuleDecompiler.DecompileType(propertyInfo.ReflectedType), propertyInfo.Name, e.InnerException.Message);
throw new TargetInvocationException(message, e.InnerException);
}
#pragma warning restore 56503
}
set
{
if (!propertyInfo.GetSetMethod(true).IsStatic && targetObject == null)
{
string message = string.Format(CultureInfo.CurrentCulture, Messages.TargetEvaluatedNullProperty, propertyInfo.Name);
RuleEvaluationException exception = new RuleEvaluationException(message);
exception.Data[RuleUserDataKeys.ErrorObject] = propertyInfo;
throw exception;
}
try
{
propertyInfo.SetValue(targetObject, value, indexerArguments);
}
catch (TargetInvocationException e)
{
// if there is no inner exception, leave it untouched
if (e.InnerException == null)
throw;
string message = string.Format(CultureInfo.CurrentCulture, Messages.Error_PropertySet,
RuleDecompiler.DecompileType(propertyInfo.ReflectedType), propertyInfo.Name, e.InnerException.Message);
throw new TargetInvocationException(message, e.InnerException);
}
}
}
}
internal class RuleArrayElementResult : RuleExpressionResult
{
private Array targetArray;
private long[] indexerArguments;
public RuleArrayElementResult(Array targetArray, long[] indexerArguments)
{
if (targetArray == null)
throw new ArgumentNullException("targetArray");
if (indexerArguments == null)
throw new ArgumentNullException("indexerArguments");
this.targetArray = targetArray;
this.indexerArguments = indexerArguments;
}
public override object Value
{
get
{
return targetArray.GetValue(indexerArguments);
}
set
{
targetArray.SetValue(value, indexerArguments);
}
}
}
#endregion
#region RuleExecution Class
public class RuleExecution
{
private bool halted; // "Halt" was executed?
private Activity activity;
private object thisObject;
private RuleValidation validation;
private ActivityExecutionContext activityExecutionContext;
private RuleLiteralResult thisLiteralResult;
[SuppressMessage("Microsoft.Naming", "CA1720:AvoidTypeNamesInParameters", MessageId = "1#")]
public RuleExecution(RuleValidation validation, object thisObject)
{
if (validation == null)
throw new ArgumentNullException("validation");
if (thisObject == null)
throw new ArgumentNullException("thisObject");
if (validation.ThisType != thisObject.GetType())
throw new InvalidOperationException(
string.Format(CultureInfo.CurrentCulture, Messages.ValidationMismatch,
RuleDecompiler.DecompileType(validation.ThisType),
RuleDecompiler.DecompileType(thisObject.GetType())));
this.validation = validation;
this.activity = thisObject as Activity;
this.thisObject = thisObject;
this.thisLiteralResult = new RuleLiteralResult(thisObject);
}
[SuppressMessage("Microsoft.Naming", "CA1720:AvoidTypeNamesInParameters", MessageId = "1#")]
public RuleExecution(RuleValidation validation, object thisObject, ActivityExecutionContext activityExecutionContext)
: this(validation, thisObject)
{
this.activityExecutionContext = activityExecutionContext;
}
public object ThisObject
{
get { return thisObject; }
}
public Activity Activity
{
get
{
#pragma warning disable 56503
if (activity == null)
throw new InvalidOperationException(Messages.NoActivity);
return activity;
#pragma warning restore 56503
}
}
public RuleValidation Validation
{
get { return validation; }
set
{
if (value == null)
throw new ArgumentNullException("value");
validation = value;
}
}
public bool Halted
{
get { return halted; }
set { halted = value; }
}
public ActivityExecutionContext ActivityExecutionContext
{
get { return this.activityExecutionContext; }
}
internal RuleLiteralResult ThisLiteralResult
{
get { return this.thisLiteralResult; }
}
}
#endregion
#region RuleState internal class
internal class RuleState : IComparable
{
internal Rule Rule;
private ICollection<int> thenActionsActiveRules;
private ICollection<int> elseActionsActiveRules;
internal RuleState(Rule rule)
{
this.Rule = rule;
}
internal ICollection<int> ThenActionsActiveRules
{
get { return thenActionsActiveRules; }
set { thenActionsActiveRules = value; }
}
internal ICollection<int> ElseActionsActiveRules
{
get { return elseActionsActiveRules; }
set { elseActionsActiveRules = value; }
}
int IComparable.CompareTo(object obj)
{
RuleState other = obj as RuleState;
int compare = other.Rule.Priority.CompareTo(Rule.Priority);
if (compare == 0)
// if the priorities are the same, compare names (in ascending order)
compare = -other.Rule.Name.CompareTo(Rule.Name);
return compare;
}
}
#endregion
#region Tracking Argument
/// <summary>
/// Contains the name and condition result of a rule that has caused one or more actions to execute.
/// </summary>
[Serializable]
[Obsolete("The System.Workflow.* types are deprecated. Instead, please use the new types from System.Activities.*")]
public class RuleActionTrackingEvent
{
private string ruleName;
private bool conditionResult;
internal RuleActionTrackingEvent(string ruleName, bool conditionResult)
{
this.ruleName = ruleName;
this.conditionResult = conditionResult;
}
/// <summary>
/// The name of the rule that has caused one or more actions to execute.
/// </summary>
public string RuleName
{
get { return ruleName; }
}
/// <summary>
/// The rule's condition result: false means the "else" actions are executed; true means the "then" actions are executed.
/// </summary>
public bool ConditionResult
{
get { return conditionResult; }
}
}
#endregion
internal class Executor
{
#region Rule Set Executor
internal static IList<RuleState> Preprocess(RuleChainingBehavior behavior, ICollection<Rule> rules, RuleValidation validation, Tracer tracer)
{
// start by taking the active rules and make them into a list sorted by priority
List<RuleState> orderedRules = new List<RuleState>(rules.Count);
foreach (Rule r in rules)
{
if (r.Active)
orderedRules.Add(new RuleState(r));
}
orderedRules.Sort();
// Analyze the rules to match side-effects with dependencies.
// Note that the RuleSet needs to have been validated prior to this.
AnalyzeRules(behavior, orderedRules, validation, tracer);
// return the sorted list of rules
return orderedRules;
}
internal static void ExecuteRuleSet(IList<RuleState> orderedRules, RuleExecution ruleExecution, Tracer tracer, string trackingKey)
{
// keep track of rule execution
long[] executionCount = new long[orderedRules.Count];
bool[] satisfied = new bool[orderedRules.Count];
// clear the halted flag
ruleExecution.Halted = false;
ActivityExecutionContext activityExecutionContext = ruleExecution.ActivityExecutionContext;
// loop until we hit the end of the list
int current = 0;
while (current < orderedRules.Count)
{
RuleState currentRuleState = orderedRules[current];
// does this rule need to be evaluated?
if (!satisfied[current])
{
// yes, so evaluate it and determine the list of actions needed
if (tracer != null)
tracer.StartRule(currentRuleState.Rule.Name);
satisfied[current] = true;
bool result = currentRuleState.Rule.Condition.Evaluate(ruleExecution);
if (tracer != null)
tracer.RuleResult(currentRuleState.Rule.Name, result);
if (activityExecutionContext != null && currentRuleState.Rule.Name != null)
activityExecutionContext.TrackData(trackingKey, new RuleActionTrackingEvent(currentRuleState.Rule.Name, result));
ICollection<RuleAction> actions = (result) ?
currentRuleState.Rule.thenActions :
currentRuleState.Rule.elseActions;
ICollection<int> activeRules = result ?
currentRuleState.ThenActionsActiveRules :
currentRuleState.ElseActionsActiveRules;
// are there any actions to be performed?
if ((actions != null) && (actions.Count > 0))
{
++executionCount[current];
string ruleName = currentRuleState.Rule.Name;
if (tracer != null)
tracer.StartActions(ruleName, result);
// evaluate the actions
foreach (RuleAction action in actions)
{
action.Execute(ruleExecution);
// was Halt executed?
if (ruleExecution.Halted)
break;
}
// was Halt executed?
if (ruleExecution.Halted)
break;
// any fields updated?
if (activeRules != null)
{
foreach (int updatedRuleIndex in activeRules)
{
RuleState rs = orderedRules[updatedRuleIndex];
if (satisfied[updatedRuleIndex])
{
// evaluate at least once, or repeatedly if appropriate
if ((executionCount[updatedRuleIndex] == 0) || (rs.Rule.ReevaluationBehavior == RuleReevaluationBehavior.Always))
{
if (tracer != null)
tracer.TraceUpdate(ruleName, rs.Rule.Name);
satisfied[updatedRuleIndex] = false;
if (updatedRuleIndex < current)
current = updatedRuleIndex;
}
}
}
}
continue;
}
}
++current;
}
// no more rules to execute, so we are done
}
class RuleSymbolInfo
{
internal ICollection<string> conditionDependencies;
internal ICollection<string> thenSideEffects;
internal ICollection<string> elseSideEffects;
}
private static void AnalyzeRules(RuleChainingBehavior behavior, List<RuleState> ruleStates, RuleValidation validation, Tracer tracer)
{
int i;
int numRules = ruleStates.Count;
// if no chaining is required, then nothing to do
if (behavior == RuleChainingBehavior.None)
return;
// Analyze all the rules and collect all the dependencies & side-effects
RuleSymbolInfo[] ruleSymbols = new RuleSymbolInfo[numRules];
for (i = 0; i < numRules; ++i)
ruleSymbols[i] = AnalyzeRule(behavior, ruleStates[i].Rule, validation, tracer);
for (i = 0; i < numRules; ++i)
{
RuleState currentRuleState = ruleStates[i];
if (ruleSymbols[i].thenSideEffects != null)
{
currentRuleState.ThenActionsActiveRules = AnalyzeSideEffects(ruleSymbols[i].thenSideEffects, ruleSymbols);
if ((currentRuleState.ThenActionsActiveRules != null) && (tracer != null))
tracer.TraceThenTriggers(currentRuleState.Rule.Name, currentRuleState.ThenActionsActiveRules, ruleStates);
}
if (ruleSymbols[i].elseSideEffects != null)
{
currentRuleState.ElseActionsActiveRules = AnalyzeSideEffects(ruleSymbols[i].elseSideEffects, ruleSymbols);
if ((currentRuleState.ElseActionsActiveRules != null) && (tracer != null))
tracer.TraceElseTriggers(currentRuleState.Rule.Name, currentRuleState.ElseActionsActiveRules, ruleStates);
}
}
}
private static ICollection<int> AnalyzeSideEffects(ICollection<string> sideEffects, RuleSymbolInfo[] ruleSymbols)
{
Dictionary<int, object> affectedRules = new Dictionary<int, object>();
for (int i = 0; i < ruleSymbols.Length; ++i)
{
ICollection<string> dependencies = ruleSymbols[i].conditionDependencies;
if (dependencies == null)
{
continue;
}
foreach (string sideEffect in sideEffects)
{
bool match = false;
if (sideEffect.EndsWith("*", StringComparison.Ordinal))
{
foreach (string dependency in dependencies)
{
if (dependency.EndsWith("*", StringComparison.Ordinal))
{
// Strip the trailing "/*" from the dependency
string stripDependency = dependency.Substring(0, dependency.Length - 2);
// Strip the trailing "*" from the side-effect
string stripSideEffect = sideEffect.Substring(0, sideEffect.Length - 1);
string shortString;
string longString;
if (stripDependency.Length < stripSideEffect.Length)
{
shortString = stripDependency;
longString = stripSideEffect;
}
else
{
shortString = stripSideEffect;
longString = stripDependency;
}
// There's a match if the shorter string is a prefix of the longer string.
if (longString.StartsWith(shortString, StringComparison.Ordinal))
{
match = true;
break;
}
}
else
{
string stripSideEffect = sideEffect.Substring(0, sideEffect.Length - 1);
string stripDependency = dependency;
if (stripDependency.EndsWith("/", StringComparison.Ordinal))
stripDependency = stripDependency.Substring(0, stripDependency.Length - 1);
if (stripDependency.StartsWith(stripSideEffect, StringComparison.Ordinal))
{
match = true;
break;
}
}
}
}
else
{
// The side-effect did not end with a wildcard
foreach (string dependency in dependencies)
{
if (dependency.EndsWith("*", StringComparison.Ordinal))
{
// Strip the trailing "/*"
string stripDependency = dependency.Substring(0, dependency.Length - 2);
string shortString;
string longString;
if (stripDependency.Length < sideEffect.Length)
{
shortString = stripDependency;
longString = sideEffect;
}
else
{
shortString = sideEffect;
longString = stripDependency;
}
// There's a match if the shorter string is a prefix of the longer string.
if (longString.StartsWith(shortString, StringComparison.Ordinal))
{
match = true;
break;
}
}
else
{
// The side-effect must be a prefix of the dependency (or an exact match).
if (dependency.StartsWith(sideEffect, StringComparison.Ordinal))
{
match = true;
break;
}
}
}
}
if (match)
{
affectedRules[i] = null;
break;
}
}
}
return affectedRules.Keys;
}
private static RuleSymbolInfo AnalyzeRule(RuleChainingBehavior behavior, Rule rule, RuleValidation validator, Tracer tracer)
{
RuleSymbolInfo rsi = new RuleSymbolInfo();
if (rule.Condition != null)
{
rsi.conditionDependencies = rule.Condition.GetDependencies(validator);
if ((rsi.conditionDependencies != null) && (tracer != null))
tracer.TraceConditionSymbols(rule.Name, rsi.conditionDependencies);
}
if (rule.thenActions != null)
{
rsi.thenSideEffects = GetActionSideEffects(behavior, rule.thenActions, validator);
if ((rsi.thenSideEffects != null) && (tracer != null))
tracer.TraceThenSymbols(rule.Name, rsi.thenSideEffects);
}
if (rule.elseActions != null)
{
rsi.elseSideEffects = GetActionSideEffects(behavior, rule.elseActions, validator);
if ((rsi.elseSideEffects != null) && (tracer != null))
tracer.TraceElseSymbols(rule.Name, rsi.elseSideEffects);
}
return rsi;
}
private static ICollection<string> GetActionSideEffects(RuleChainingBehavior behavior, IList<RuleAction> actions, RuleValidation validation)
{
// Man, I wish there were a Set<T> class...
Dictionary<string, object> symbols = new Dictionary<string, object>();
foreach (RuleAction action in actions)
{
if ((behavior == RuleChainingBehavior.Full) ||
((behavior == RuleChainingBehavior.UpdateOnly) && (action is RuleUpdateAction)))
{
ICollection<string> sideEffects = action.GetSideEffects(validation);
if (sideEffects != null)
{
foreach (string symbol in sideEffects)
symbols[symbol] = null;
}
}
}
return symbols.Keys;
}
#endregion
#region Condition Executors
internal static bool EvaluateBool(CodeExpression expression, RuleExecution context)
{
object result = RuleExpressionWalker.Evaluate(context, expression).Value;
if (result is bool)
return (bool)result;
Type expectedType = context.Validation.ExpressionInfo(expression).ExpressionType;
if (expectedType == null)
{
// oops ... not a boolean, so error
InvalidOperationException exception = new InvalidOperationException(Messages.ConditionMustBeBoolean);
exception.Data[RuleUserDataKeys.ErrorObject] = expression;
throw exception;
}
return (bool)AdjustType(expectedType, result, typeof(bool));
}
internal static object AdjustType(Type operandType, object operandValue, Type toType)
{
// if no conversion required, we are done
if (operandType == toType)
return operandValue;
object converted;
if (AdjustValueStandard(operandType, operandValue, toType, out converted))
return converted;
// not a standard conversion, see if it's an implicit user defined conversions
ValidationError error;
MethodInfo conversion = RuleValidation.FindImplicitConversion(operandType, toType, out error);
if (conversion == null)
{
if (error != null)
throw new RuleEvaluationException(error.ErrorText);
throw new RuleEvaluationException(
string.Format(CultureInfo.CurrentCulture,
Messages.CastIncompatibleTypes,
RuleDecompiler.DecompileType(operandType),
RuleDecompiler.DecompileType(toType)));
}
// now we have a method, need to do the conversion S -> Sx -> Tx -> T
Type sx = conversion.GetParameters()[0].ParameterType;
Type tx = conversion.ReturnType;
object intermediateResult1;
if (AdjustValueStandard(operandType, operandValue, sx, out intermediateResult1))
{
// we are happy with the first conversion, so call the user's static method
object intermediateResult2 = conversion.Invoke(null, new object[] { intermediateResult1 });
object intermediateResult3;
if (AdjustValueStandard(tx, intermediateResult2, toType, out intermediateResult3))
return intermediateResult3;
}
throw new RuleEvaluationException(
string.Format(CultureInfo.CurrentCulture,
Messages.CastIncompatibleTypes,
RuleDecompiler.DecompileType(operandType),
RuleDecompiler.DecompileType(toType)));
}
internal static object AdjustTypeWithCast(Type operandType, object operandValue, Type toType)
{
// if no conversion required, we are done
if (operandType == toType)
return operandValue;
object converted;
if (AdjustValueStandard(operandType, operandValue, toType, out converted))
return converted;
// handle enumerations (done above?)
// now it's time for implicit and explicit user defined conversions
ValidationError error;
MethodInfo conversion = RuleValidation.FindExplicitConversion(operandType, toType, out error);
if (conversion == null)
{
if (error != null)
throw new RuleEvaluationException(error.ErrorText);
throw new RuleEvaluationException(
string.Format(CultureInfo.CurrentCulture,
Messages.CastIncompatibleTypes,
RuleDecompiler.DecompileType(operandType),
RuleDecompiler.DecompileType(toType)));
}
// now we have a method, need to do the conversion S -> Sx -> Tx -> T
Type sx = conversion.GetParameters()[0].ParameterType;
Type tx = conversion.ReturnType;
object intermediateResult1;
if (AdjustValueStandard(operandType, operandValue, sx, out intermediateResult1))
{
// we are happy with the first conversion, so call the user's static method
object intermediateResult2 = conversion.Invoke(null, new object[] { intermediateResult1 });
object intermediateResult3;
if (AdjustValueStandard(tx, intermediateResult2, toType, out intermediateResult3))
return intermediateResult3;
}
throw new RuleEvaluationException(
string.Format(CultureInfo.CurrentCulture,
Messages.CastIncompatibleTypes,
RuleDecompiler.DecompileType(operandType),
RuleDecompiler.DecompileType(toType)));
}
[SuppressMessage("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity")]
private static bool AdjustValueStandard(Type operandType, object operandValue, Type toType, out object converted)
{
// assume it's the same for now
converted = operandValue;
// check for null
if (operandValue == null)
{
// are we converting to a value type?
if (toType.IsValueType)
{
// is the conversion to nullable?
if (!ConditionHelper.IsNullableValueType(toType))
{
// value type and null, so no conversion possible
string message = string.Format(CultureInfo.CurrentCulture, Messages.CannotCastNullToValueType, RuleDecompiler.DecompileType(toType));
throw new InvalidCastException(message);
}
// here we have a Nullable<T>
// however, we may need to call the implicit conversion operator if the types are not compatible
converted = Activator.CreateInstance(toType);
ValidationError error;
return RuleValidation.StandardImplicitConversion(operandType, toType, null, out error);
}
// not a value type, so null is valid
return true;
}
// check simple cases
Type currentType = operandValue.GetType();
if (currentType == toType)
return true;
// now the fun begins
// this should handle most class conversions
if (toType.IsAssignableFrom(currentType))
return true;
// handle the numerics (both implicit and explicit), along with nullable
// note that if the value was null, it's already handled, so value cannot be nullable
if ((currentType.IsValueType) && (toType.IsValueType))
{
if (currentType.IsEnum)
{
// strip off the enum representation
currentType = Enum.GetUnderlyingType(currentType);
ArithmeticLiteral literal = ArithmeticLiteral.MakeLiteral(currentType, operandValue);
operandValue = literal.Value;
}
bool resultNullable = ConditionHelper.IsNullableValueType(toType);
Type resultType = (resultNullable) ? Nullable.GetUnderlyingType(toType) : toType;
if (resultType.IsEnum)
{
// Enum.ToObject may throw if currentType is not type SByte,
// Int16, Int32, Int64, Byte, UInt16, UInt32, or UInt64.
// So we adjust currentValue to the underlying type (which may throw if out of range)
Type underlyingType = Enum.GetUnderlyingType(resultType);
object adjusted;
if (AdjustValueStandard(currentType, operandValue, underlyingType, out adjusted))
{
converted = Enum.ToObject(resultType, adjusted);
if (resultNullable)
converted = Activator.CreateInstance(toType, converted);
return true;
}
}
else if ((resultType.IsPrimitive) || (resultType == typeof(decimal)))
{
// resultType must be a primitive to continue (not a struct)
// (enums and generics handled above)
if (currentType == typeof(char))
{
char c = (char)operandValue;
if (resultType == typeof(float))
{
converted = (float)c;
}
else if (resultType == typeof(double))
{
converted = (double)c;
}
else if (resultType == typeof(decimal))
{
converted = (decimal)c;
}
else
{
converted = ((IConvertible)c).ToType(resultType, CultureInfo.CurrentCulture);
}
if (resultNullable)
converted = Activator.CreateInstance(toType, converted);
return true;
}
else if (currentType == typeof(float))
{
float f = (float)operandValue;
if (resultType == typeof(char))
{
converted = (char)f;
}
else
{
converted = ((IConvertible)f).ToType(resultType, CultureInfo.CurrentCulture);
}
if (resultNullable)
converted = Activator.CreateInstance(toType, converted);
return true;
}
else if (currentType == typeof(double))
{
double d = (double)operandValue;
if (resultType == typeof(char))
{
converted = (char)d;
}
else
{
converted = ((IConvertible)d).ToType(resultType, CultureInfo.CurrentCulture);
}
if (resultNullable)
converted = Activator.CreateInstance(toType, converted);
return true;
}
else if (currentType == typeof(decimal))
{
decimal d = (decimal)operandValue;
if (resultType == typeof(char))
{
converted = (char)d;
}
else
{
converted = ((IConvertible)d).ToType(resultType, CultureInfo.CurrentCulture);
}
if (resultNullable)
converted = Activator.CreateInstance(toType, converted);
return true;
}
else
{
IConvertible convert = operandValue as IConvertible;
if (convert != null)
{
try
{
converted = convert.ToType(resultType, CultureInfo.CurrentCulture);
if (resultNullable)
converted = Activator.CreateInstance(toType, converted);
return true;
}
catch (InvalidCastException)
{
// not IConvertable, so can't do it
return false;
}
}
}
}
}
// no luck with standard conversions, so no conversion done
return false;
}
#endregion
}
}
|