xronos /Microsoft.Scripting/Hosting/ObjectOperations.cs

Language C# Lines 1025
MD5 Hash 00b4f3e47dd29bea737d584f89e7b607 Estimated Cost $12,446 (why?)
Repository https://bitbucket.org/stefanrusek/xronos View Raw File
   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
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
/* ****************************************************************************
 *
 * Copyright (c) Microsoft Corporation. 
 *
 * This source code is subject to terms and conditions of the Microsoft Public License. A 
 * copy of the license can be found in the License.html file at the root of this distribution. If 
 * you cannot locate the  Microsoft Public License, 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 Microsoft Public License.
 *
 * You must not remove this notice, or any other, from this software.
 *
 *
 * ***************************************************************************/

#if CODEPLEX_40
using System;
#else
using System; using Microsoft;
#endif
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.Remoting;
using System.Security.Permissions;
using Microsoft.Scripting.Runtime;
using Microsoft.Scripting.Utils;
#if CODEPLEX_40
using System.Linq.Expressions;
using System.Dynamic;
#else
using Microsoft.Linq.Expressions;
using Microsoft.Scripting;
#endif

namespace Microsoft.Scripting.Hosting {

    /// <summary>
    /// ObjectOperations provide a large catalogue of object operations such as member access, conversions, 
    /// indexing, and things like addition.  There are several introspection and tool support services available
    /// for more advanced hosts.  
    /// 
    /// You get ObjectOperation instances from ScriptEngine, and they are bound to their engines for the semantics 
    /// of the operations.  There is a default instance of ObjectOperations you can share across all uses of the 
    /// engine.  However, very advanced hosts can create new instances.
    /// </summary>
    public sealed class ObjectOperations
#if !SILVERLIGHT
        : MarshalByRefObject
#endif
        {

        private readonly DynamicOperations _ops;
        private readonly ScriptEngine _engine;

        // friend class: DynamicOperations
        internal ObjectOperations(DynamicOperations ops, ScriptEngine engine) {
            Assert.NotNull(ops);
            Assert.NotNull(engine);
            _ops = ops;
            _engine = engine;
        }
        
        public ScriptEngine Engine {
            get { return _engine; }
        }

#pragma warning disable 618

        #region Local Operations

        /// <summary>
        /// Returns true if the object can be called, false if it cannot.  
        /// 
        /// Even if an object is callable Call may still fail if an incorrect number of arguments or type of arguments are provided.
        /// </summary>
        public bool IsCallable(object obj) {
            return _ops.IsCallable(obj);
        }

        /// <summary>
        /// Invokes the provided object with the given parameters and returns the result.
        /// 
        /// The prefered way of calling objects is to convert the object to a strongly typed delegate 
        /// using the ConvertTo methods and then invoking that delegate.
        /// </summary>
        public object Invoke(object obj, params object[] parameters) {
            return _ops.Invoke(obj, parameters);
        }

        /// <summary>
        /// Invokes a member on the provided object with the given parameters and returns the result.
        /// </summary>
        public object InvokeMember(object obj, string memberName, params object[] parameters) {
            return _ops.InvokeMember(obj, memberName, parameters);
        }

        /// <summary>
        /// Creates a new instance from the provided object using the given parameters, and returns the result.
        /// </summary>
        public object CreateInstance(object obj, params object[] parameters) {
            return _ops.CreateInstance(obj, parameters);
        }

        /// <summary>
        /// Gets the member name from the object obj.  Throws an exception if the member does not exist or is write-only.
        /// </summary>
        public object GetMember(object obj, string name) {
            return _ops.GetMember(obj, name);
        }

        /// <summary>
        /// Gets the member name from the object obj and converts it to the type T.  Throws an exception if the
        /// member does not exist, is write-only, or cannot be converted.
        /// </summary>
        public T GetMember<T>(object obj, string name) {
            return _ops.GetMember<T>(obj, name);
        }

        /// <summary>
        /// Gets the member name from the object obj.  Returns true if the member is successfully retrieved and 
        /// stores the value in the value out param.
        /// </summary>
        public bool TryGetMember(object obj, string name, out object value) {
            return _ops.TryGetMember(obj, name, out value);
        }

        /// <summary>
        /// Returns true if the object has a member named name, false if the member does not exist.
        /// </summary>
        public bool ContainsMember(object obj, string name) {
            return _ops.ContainsMember(obj, name);
        }

        /// <summary>
        /// Removes the member name from the object obj.  Returns true if the member was successfully removed
        /// or false if the member does not exist.
        /// </summary>
        public bool RemoveMember(object obj, string name) {
            return _ops.RemoveMember(obj, name);
        }

        /// <summary>
        /// Sets the member name on object obj to value.
        /// </summary>
        public void SetMember(object obj, string name, object value) {
            _ops.SetMember(obj, name, value);
        }

        /// <summary>
        /// Sets the member name on object obj to value.  This overload can be used to avoid
        /// boxing and casting of strongly typed members.
        /// </summary>
        public void SetMember<T>(object obj, string name, T value) {
            _ops.SetMember<T>(obj, name, value);
        }

        /// <summary>
        /// Gets the member name from the object obj.  Throws an exception if the member does not exist or is write-only.
        /// </summary>
        public object GetMember(object obj, string name, bool ignoreCase) {
            return _ops.GetMember(obj, name, ignoreCase);
        }

        /// <summary>
        /// Gets the member name from the object obj and converts it to the type T.  Throws an exception if the
        /// member does not exist, is write-only, or cannot be converted.
        /// </summary>
        public T GetMember<T>(object obj, string name, bool ignoreCase) {
            return _ops.GetMember<T>(obj, name, ignoreCase);
        }

        /// <summary>
        /// Gets the member name from the object obj.  Returns true if the member is successfully retrieved and 
        /// stores the value in the value out param.
        /// </summary>
        public bool TryGetMember(object obj, string name, bool ignoreCase, out object value) {
            return _ops.TryGetMember(obj, name, ignoreCase, out value);
        }

        /// <summary>
        /// Returns true if the object has a member named name, false if the member does not exist.
        /// </summary>
        public bool ContainsMember(object obj, string name, bool ignoreCase) {
            return _ops.ContainsMember(obj, name, ignoreCase);
        }

        /// <summary>
        /// Removes the member name from the object obj.  Returns true if the member was successfully removed
        /// or false if the member does not exist.
        /// </summary>
        public bool RemoveMember(object obj, string name, bool ignoreCase) {
            return _ops.RemoveMember(obj, name, ignoreCase);
        }

        /// <summary>
        /// Sets the member name on object obj to value.
        /// </summary>
        public void SetMember(object obj, string name, object value, bool ignoreCase) {
            _ops.SetMember(obj, name, value, ignoreCase);
        }

        /// <summary>
        /// Sets the member name on object obj to value.  This overload can be used to avoid
        /// boxing and casting of strongly typed members.
        /// </summary>
        public void SetMember<T>(object obj, string name, T value, bool ignoreCase) {
            _ops.SetMember<T>(obj, name, value, ignoreCase);
        }

        /// <summary>
        /// Convers the object obj to the type T.
        /// </summary>
        public T ConvertTo<T>(object obj) {
            return _ops.ConvertTo<T>(obj);
        }

        /// <summary>
        /// Converts the object obj to the type type.
        /// </summary>
        public object ConvertTo(object obj, Type type) {
            ContractUtils.RequiresNotNull(type, "type");

            return _ops.ConvertTo(obj, type);
        }

        /// <summary>
        /// Converts the object obj to the type T.  Returns true if the value can be converted, false if it cannot.
        /// </summary>
        public bool TryConvertTo<T>(object obj, out T result) {
            return _ops.TryConvertTo<T>(obj, out result);
        }

        /// <summary>
        /// Converts the object obj to the type type.  Returns true if the value can be converted, false if it cannot.
        /// </summary>
        public bool TryConvertTo(object obj, Type type, out object result) {
            return _ops.TryConvertTo(obj, type, out result);
        }

        /// <summary>
        /// Converts the object obj to the type T including explicit conversions which may lose information.
        /// </summary>
        public T ExplicitConvertTo<T>(object obj) {
            return _ops.ExplicitConvertTo<T>(obj);
        }

        /// <summary>
        /// Converts the object obj to the type type including explicit conversions which may lose information.
        /// </summary>
        public object ExplicitConvertTo(object obj, Type type) {
            ContractUtils.RequiresNotNull(type, "type");

            return _ops.ExplicitConvertTo(obj, type);
        }

        /// <summary>
        /// Converts the object obj to the type T including explicit conversions which may lose information.
        /// 
        /// Returns true if the value can be converted, false if it cannot.
        /// </summary>
        public bool TryExplicitConvertTo<T>(object obj, out T result) {
            return _ops.TryExplicitConvertTo<T>(obj, out result);
        }

        /// <summary>
        /// Converts the object obj to the type type including explicit conversions which may lose information.  
        /// 
        /// Returns true if the value can be converted, false if it cannot.
        /// </summary>
        public bool TryExplicitConvertTo(object obj, Type type, out object result) {
            return _ops.TryExplicitConvertTo(obj, type, out result);
        }


        /// <summary>
        /// Performs a generic unary operation on the specified target and returns the result.
        /// </summary>
        public object DoOperation(ExpressionType operation, object target) {
            return _ops.DoOperation<object, object>(operation, target);
        }

        /// <summary>
        /// Performs a generic unary operation on the strongly typed target and returns the value as the specified type
        /// </summary>
        public TResult DoOperation<TTarget, TResult>(ExpressionType operation, TTarget target) {
            return _ops.DoOperation<TTarget, TResult>(operation, target);
        }

        /// <summary>
        /// Performs the generic binary operation on the specified targets and returns the result.
        /// </summary>
        public object DoOperation(ExpressionType operation, object target, object other) {
            return _ops.DoOperation<object, object, object>(operation, target, other);
        }

        /// <summary>
        /// Peforms the generic binary operation on the specified strongly typed targets and returns
        /// the strongly typed result.
        /// </summary>
        public TResult DoOperation<TTarget, TOther, TResult>(ExpressionType operation, TTarget target, TOther other) {
            return _ops.DoOperation<TTarget, TOther, TResult>(operation, target, other);
        }

        /// <summary>
        /// Performs addition on the specified targets and returns the result.  Throws an exception
        /// if the operation cannot be performed.
        /// </summary>
        public object Add(object self, object other) {
            return DoOperation(ExpressionType.Add, self, other);
        }

        /// <summary>
        /// Performs subtraction on the specified targets and returns the result.  Throws an exception
        /// if the operation cannot be performed.
        /// </summary>
        public object Subtract(object self, object other) {
            return DoOperation(ExpressionType.Subtract, self, other);
        }

        /// <summary>
        /// Raises the first object to the power of the second object.  Throws an exception
        /// if the operation cannot be performed.
        /// </summary>
        public object Power(object self, object other) {
            return DoOperation(ExpressionType.Power, self, other);
        }

        /// <summary>
        /// Multiplies the two objects.  Throws an exception
        /// if the operation cannot be performed.
        /// </summary>
        public object Multiply(object self, object other) {
            return DoOperation(ExpressionType.Multiply, self, other);
        }

        /// <summary>
        /// Divides the first object by the second object.  Throws an exception
        /// if the operation cannot be performed.
        /// </summary>
        public object Divide(object self, object other) {
            return DoOperation(ExpressionType.Divide, self, other);
        }

        /// <summary>
        /// Performs modulus of the 1st object by the second object.  Throws an exception
        /// if the operation cannot be performed.
        /// </summary>
        public object Modulo(object self, object other) {
            return DoOperation(ExpressionType.Modulo, self, other);
        }
        /// <summary>
        /// Shifts the left object left by the right object.  Throws an exception if the
        /// operation cannot be performed.
        /// </summary>
        public object LeftShift(object self, object other) {
            return DoOperation(ExpressionType.LeftShift, self, other);
        }

        /// <summary>
        /// Shifts the left object right by the right object.  Throws an exception if the
        /// operation cannot be performed.
        /// </summary>
        public object RightShift(object self, object other) {
            return DoOperation(ExpressionType.RightShift, self, other);
        }

        /// <summary>
        /// Performs a bitwise-and of the two operands.  Throws an exception if the operation 
        /// cannot be performed.
        /// </summary>
        public object BitwiseAnd(object self, object other) {
            return DoOperation(ExpressionType.And, self, other);
        }

        /// <summary>
        /// Performs a bitwise-or of the two operands.  Throws an exception if the operation 
        /// cannot be performed.
        /// </summary>
        public object BitwiseOr(object self, object other) {
            return DoOperation(ExpressionType.Or, self, other);
        }

        /// <summary>
        /// Performs a exclusive-or of the two operands.  Throws an exception if the operation 
        /// cannot be performed.
        /// </summary>
        public object ExclusiveOr(object self, object other) {
            return DoOperation(ExpressionType.ExclusiveOr, self, other);
        }

        /// <summary>
        /// Compares the two objects and returns true if the left object is less than the right object.
        /// Throws an exception if hte comparison cannot be performed.
        /// </summary>
        public bool LessThan(object self, object other) {
            return _ops.DoOperation<object, object, bool>(ExpressionType.LessThan, self, other);
        }

        /// <summary>
        /// Compares the two objects and returns true if the left object is greater than the right object.
        /// Throws an exception if hte comparison cannot be performed.
        /// </summary>
        public bool GreaterThan(object self, object other) {
            return _ops.DoOperation<object, object, bool>(ExpressionType.GreaterThan, self, other);
        }

        /// <summary>
        /// Compares the two objects and returns true if the left object is less than or equal to the right object.
        /// Throws an exception if hte comparison cannot be performed.
        /// </summary>
        public bool LessThanOrEqual(object self, object other) {
            return _ops.DoOperation<object, object, bool>(ExpressionType.LessThanOrEqual, self, other);
        }

        /// <summary>
        /// Compares the two objects and returns true if the left object is greater than or equal to the right object.
        /// Throws an exception if hte comparison cannot be performed.
        /// </summary>
        public bool GreaterThanOrEqual(object self, object other) {
            return _ops.DoOperation<object, object, bool>(ExpressionType.GreaterThanOrEqual, self, other);
        }

        /// <summary>
        /// Compares the two objects and returns true if the left object is equal to the right object.
        /// Throws an exception if the comparison cannot be performed.
        /// </summary>
        public bool Equal(object self, object other) {
            return _ops.DoOperation<object, object, bool>(ExpressionType.Equal, self, other);
        }

        /// <summary>
        /// Compares the two objects and returns true if the left object is not equal to the right object.
        /// Throws an exception if hte comparison cannot be performed.
        /// </summary>
        public bool NotEqual(object self, object other) {
            return _ops.DoOperation<object, object, bool>(ExpressionType.NotEqual, self, other);
        }

        /// <summary>
        /// Returns a string which describes the object as it appears in source code
        /// </summary>
        [Obsolete("Use Format method instead.")]
        public string GetCodeRepresentation(object obj) {
            return obj.ToString();
            //return _ops.DoOperation<object, string>(StandardOperators.CodeRepresentation, obj);
        }

        /// <summary>
        /// Returns a string representation of the object in a language specific object display format.
        /// </summary>
        public string Format(object obj) {
            return _ops.Format(obj);
        }

        /// <summary>
        /// Returns a list of strings which contain the known members of the object.
        /// </summary>
        public IList<string> GetMemberNames(object obj) {
            return _ops.GetMemberNames(obj);
        }

        /// <summary>
        /// Returns a string providing documentation for the specified object.
        /// </summary>
        public string GetDocumentation(object obj) {
            return _ops.GetDocumentation(obj);
        }

        /// <summary>
        /// Returns a list of signatures applicable for calling the specified object in a form displayable to the user.
        /// </summary>
        public IList<string> GetCallSignatures(object obj) {
            return _ops.GetCallSignatures(obj);
        }

        #endregion

        #region Obsolete Operations

        /// <summary>
        /// Calls the provided object with the given parameters and returns the result.
        /// 
        /// The prefered way of calling objects is to convert the object to a strongly typed delegate 
        /// using the ConvertTo methods and then invoking that delegate.
        /// </summary>
        [Obsolete("Use Invoke instead")]
        public object Call(object obj, params object[] parameters) {
            return _ops.Invoke(obj, parameters);
        }

                /// <summary>
        /// Performs a generic unary operation on the specified target and returns the result.
        /// </summary>
        [Obsolete]
        public object DoOperation(Operators op, object target) {
            ExpressionType newOp = GetLinqOp(op);

            return _ops.DoOperation<object, object>(newOp, target);
        }
        
        /// <summary>
        /// Performs a generic unary operation on the strongly typed target and returns the value as the specified type
        /// </summary>
        [Obsolete("Use ExpressionType overload instead")]
        public TResult DoOperation<TTarget, TResult>(Operators op, TTarget target) {
            return _ops.DoOperation<TTarget, TResult>(GetLinqOp(op), target);
        }

        [Obsolete]
        private static ExpressionType GetLinqOp(Operators op) {
            ExpressionType? newOp = null;
            switch (op) {
                case Operators.Positive: newOp = ExpressionType.UnaryPlus; break;
                case Operators.Negate: newOp = ExpressionType.Negate; break;
                case Operators.OnesComplement: newOp = ExpressionType.OnesComplement; break;
                case Operators.IsFalse: newOp = ExpressionType.IsFalse; break;
                case Operators.Decrement: newOp = ExpressionType.Decrement; break;
                case Operators.Increment: newOp = ExpressionType.Increment; break;
                default:
                    throw new InvalidOperationException(String.Format("Unrecognized shared operation: {0}", op));
            }
            return newOp.Value;
        }

        /// <summary>
        /// Performs modulus of the 1st object by the second object.  Throws an exception
        /// if the operation cannot be performed.
        /// </summary>
        [Obsolete("Use Modulo instead")]
        public object Modulus(object self, object other) {
            return Modulo(self, other);
        }

        /// <summary>
        /// Performs the generic binary operation on the specified targets and returns the result.
        /// </summary>
        [Obsolete("Use ExpressionType overload instead")]
        public object DoOperation(Operators op, object target, object other) {
            return _ops.DoOperation<object, object, object>(GetLinqBinaryOp(op), target, other);
        }

        [Obsolete]
        private static ExpressionType GetLinqBinaryOp(Operators op) {
            ExpressionType? newOp = null;
            switch (op) {
                case Operators.Add: newOp = ExpressionType.Add; break;
                case Operators.BitwiseAnd: newOp = ExpressionType.And; break;
                case Operators.Divide: newOp = ExpressionType.Divide; break;
                case Operators.ExclusiveOr: newOp = ExpressionType.ExclusiveOr; break;
                case Operators.Mod: newOp = ExpressionType.Modulo; break;
                case Operators.Multiply: newOp = ExpressionType.Multiply; break;
                case Operators.BitwiseOr: newOp = ExpressionType.Or; break;
                case Operators.Power: newOp = ExpressionType.Power; break;
                case Operators.RightShift: newOp = ExpressionType.RightShift; break;
                case Operators.LeftShift: newOp = ExpressionType.LeftShift; break;
                case Operators.Subtract: newOp = ExpressionType.Subtract; break;

                case Operators.Equals: newOp = ExpressionType.Equal; break;
                case Operators.GreaterThan: newOp = ExpressionType.GreaterThan; break;
                case Operators.GreaterThanOrEqual: newOp = ExpressionType.GreaterThanOrEqual; break;
                case Operators.LessThan: newOp = ExpressionType.LessThan; break;
                case Operators.LessThanOrEqual: newOp = ExpressionType.LessThanOrEqual; break;
                case Operators.NotEquals: newOp = ExpressionType.NotEqual; break;
                default:
                    throw new InvalidOperationException(String.Format("Unrecognized shared operation: {0}", op));
            }
            return newOp.Value;
        }

        /// <summary>
        /// Peforms the generic binary operation on the specified strongly typed targets and returns
        /// the strongly typed result.
        /// </summary>
        [Obsolete]
        public TResult DoOperation<TTarget, TOther, TResult>(Operators op, TTarget target, TOther other) {
            return _ops.DoOperation<TTarget, TOther, TResult>(GetLinqBinaryOp(op), target, other);
        }

#if !SILVERLIGHT
        /// <summary>
        /// Calls the specified remote object with the specified remote parameters.
        /// 
        /// Though delegates are preferable for calls they may not always be usable for remote objects.
        /// </summary>
        [Obsolete("Use Invoke instead")]
        public ObjectHandle Call(ObjectHandle obj, params ObjectHandle[] parameters) {
            return Invoke(obj, parameters);
        }

        /// <summary>
        /// Calls the specified remote object with the local parameters which will be serialized
        /// to the remote app domain.
        /// </summary>
        [Obsolete("Use Invoke instead")]
        public ObjectHandle Call(ObjectHandle obj, params object[] parameters) {
            return Invoke(obj, parameters);
        }

        /// <summary>
        /// Performs the specified unary operator on the remote object.
        /// </summary>
        [Obsolete("Use the ExpressionType overload instead")]
        public object DoOperation(Operators op, ObjectHandle target) {
            return DoOperation(op, GetLocalObject(target));
        }

        /// <summary>
        /// Performs the specified binary operator on the remote object.
        /// </summary>
        [Obsolete("Use the ExpressionType enum instead")]
        public ObjectHandle DoOperation(Operators op, ObjectHandle target, ObjectHandle other) {
            return new ObjectHandle(DoOperation(op, GetLocalObject(target), GetLocalObject(other)));
        }

        /// <summary>
        /// Performs modulus on the 1st remote object by the 2nd.  Throws an exception if the operation cannot be performed.
        /// </summary>
        [Obsolete("Use Modulo instead")]
        public ObjectHandle Modulus(ObjectHandle self, ObjectHandle other) {
            return Modulo(self, other);
        }
#endif
        #endregion

#pragma warning restore 618

        #region Remote APIs

#if !SILVERLIGHT
        // ObjectHandle overloads
        //

        /// <summary>
        /// Returns true if the remote object is callable.
        /// </summary>
        public bool IsCallable([NotNull]ObjectHandle obj) {
            return IsCallable(GetLocalObject(obj));
        }

        /// <summary>
        /// Invokes the specified remote object with the specified remote parameters.
        /// 
        /// Though delegates are preferable for calls they may not always be usable for remote objects.
        /// </summary>
        public ObjectHandle Invoke([NotNull]ObjectHandle obj, params ObjectHandle[] parameters) {
            ContractUtils.RequiresNotNull(parameters, "parameters");

            return new ObjectHandle(Invoke(GetLocalObject(obj), GetLocalObjects(parameters)));
        }

        /// <summary>
        /// Invokes the specified remote object with the local parameters which will be serialized
        /// to the remote app domain.
        /// </summary>
        public ObjectHandle Invoke([NotNull]ObjectHandle obj, params object[] parameters) {
            return new ObjectHandle(Invoke(GetLocalObject(obj), parameters));
        }

        public ObjectHandle Create([NotNull]ObjectHandle obj, [NotNull]params ObjectHandle[] parameters) {
            return new ObjectHandle(CreateInstance(GetLocalObject(obj), GetLocalObjects(parameters)));
        }

        public ObjectHandle Create([NotNull]ObjectHandle obj, params object[] parameters) {
            return new ObjectHandle(CreateInstance(GetLocalObject(obj), parameters));
        }

        /// <summary>
        /// Sets the remote object as a member on the provided remote object.
        /// </summary>
        public void SetMember([NotNull]ObjectHandle obj, string name, [NotNull]ObjectHandle value) {
            SetMember(GetLocalObject(obj), name, GetLocalObject(value));
        }

        /// <summary>
        /// Sets the member name on the remote object obj to value.  This overload can be used to avoid
        /// boxing and casting of strongly typed members.
        /// </summary>
        public void SetMember<T>([NotNull]ObjectHandle obj, string name, T value) {
            SetMember<T>(GetLocalObject(obj), name, value);
        }

        /// <summary>
        /// Gets the member name on the remote object.  Throws an exception if the member is not defined or
        /// is write-only.
        /// </summary>
        public ObjectHandle GetMember([NotNull]ObjectHandle obj, string name) {
            return new ObjectHandle(GetMember(GetLocalObject(obj), name));
        }

        /// <summary>
        /// Gets the member name on the remote object.  Throws an exception if the member is not defined or
        /// is write-only.
        /// </summary>
        public T GetMember<T>([NotNull]ObjectHandle obj, string name) {
            return GetMember<T>(GetLocalObject(obj), name);
        }

        /// <summary>
        /// Gets the member name on the remote object.  Returns false if the member is not defined or
        /// is write-only.
        /// </summary>
        public bool TryGetMember([NotNull]ObjectHandle obj, string name, out ObjectHandle value) {
            object val;
            if (TryGetMember(GetLocalObject(obj), name, out val)) {
                value = new ObjectHandle(val);
                return true;
            }

            value = null;
            return false;
        }

        /// <summary>
        /// Tests to see if the member name is defined on the remote object.  
        /// </summary>
        public bool ContainsMember([NotNull]ObjectHandle obj, string name) {
            return ContainsMember(GetLocalObject(obj), name);
        }

        /// <summary>
        /// Removes the member from the remote object
        /// </summary>
        public bool RemoveMember([NotNull]ObjectHandle obj, string name) {
            return RemoveMember(GetLocalObject(obj), name);
        }

        /// <summary>
        /// Converts the remote object into the specified type returning a handle to
        /// the new remote object.
        /// </summary>
        public ObjectHandle ConvertTo<T>([NotNull]ObjectHandle obj) {
            return new ObjectHandle(ConvertTo<T>(GetLocalObject(obj)));
        }

        /// <summary>
        /// Converts the remote object into the specified type returning a handle to
        /// the new remote object.
        /// </summary>
        public ObjectHandle ConvertTo([NotNull]ObjectHandle obj, Type type) {
            return new ObjectHandle(ConvertTo(GetLocalObject(obj), type));
        }

        /// <summary>
        /// Converts the remote object into the specified type returning a handle to
        /// the new remote object. Returns true if the value can be converted,
        /// false if it cannot.
        /// </summary>
        public bool TryConvertTo<T>([NotNull]ObjectHandle obj, out ObjectHandle result) {
            T resultObj;
            if (TryConvertTo<T>(GetLocalObject(obj), out resultObj)) {
                result = new ObjectHandle(resultObj);
                return true;
            }
            result = null;
            return false;
        }

        /// <summary>
        /// Converts the remote object into the specified type returning a handle to
        /// the new remote object. Returns true if the value can be converted,
        /// false if it cannot.
        /// </summary>
        public bool TryConvertTo([NotNull]ObjectHandle obj, Type type, out ObjectHandle result) {
            object resultObj;
            if (TryConvertTo(GetLocalObject(obj), type, out resultObj)) {
                result = new ObjectHandle(resultObj);
                return true;
            }
            result = null;
            return false;
        }

        /// <summary>
        /// Converts the object obj to the type T including explicit conversions which may lose information.
        /// </summary>
        public ObjectHandle ExplicitConvertTo<T>([NotNull]ObjectHandle obj) {
            return new ObjectHandle(_ops.ExplicitConvertTo<T>(GetLocalObject(obj)));
        }

        /// <summary>
        /// Converts the object obj to the type type including explicit conversions which may lose information.
        /// </summary>
        public ObjectHandle ExplicitConvertTo([NotNull]ObjectHandle obj, Type type) {
            ContractUtils.RequiresNotNull(type, "type");

            return new ObjectHandle(_ops.ExplicitConvertTo(GetLocalObject(obj), type));
        }

        /// <summary>
        /// Converts the object obj to the type T including explicit conversions which may lose information.
        /// 
        /// Returns true if the value can be converted, false if it cannot.
        /// </summary>
        public bool TryExplicitConvertTo<T>([NotNull]ObjectHandle obj, out ObjectHandle result) {
            T outp;
            bool res = _ops.TryExplicitConvertTo<T>(GetLocalObject(obj), out outp);
            if (res) {
                result = new ObjectHandle(obj);
            } else {
                result = null;
            }
            return res;
        }

        /// <summary>
        /// Converts the object obj to the type type including explicit conversions which may lose information.  
        /// 
        /// Returns true if the value can be converted, false if it cannot.
        /// </summary>
        public bool TryExplicitConvertTo([NotNull]ObjectHandle obj, Type type, out ObjectHandle result) {
            object outp;
            bool res = _ops.TryExplicitConvertTo(GetLocalObject(obj), type, out outp);
            if (res) {
                result = new ObjectHandle(obj);
            } else {
                result = null;
            }
            return res;
        }

        /// <summary>
        /// Unwraps the remote object and converts it into the specified type before
        /// returning it.
        /// </summary>
        public T Unwrap<T>([NotNull]ObjectHandle obj) {
            return ConvertTo<T>(GetLocalObject(obj));
        }

        /// <summary>
        /// Performs the specified unary operator on the remote object.
        /// </summary>
        public object DoOperation(ExpressionType op, [NotNull]ObjectHandle target) {
            return DoOperation(op, GetLocalObject(target));
        }

        /// <summary>
        /// Performs the specified binary operator on the remote object.
        /// </summary>
        public ObjectHandle DoOperation(ExpressionType op, ObjectHandle target, ObjectHandle other) {
            return new ObjectHandle(DoOperation(op, GetLocalObject(target), GetLocalObject(other)));
        }

        /// <summary>
        /// Adds the two remote objects.  Throws an exception if the operation cannot be performed.
        /// </summary>
        public ObjectHandle Add([NotNull]ObjectHandle self, [NotNull]ObjectHandle other) {
            return new ObjectHandle(Add(GetLocalObject(self), GetLocalObject(other)));
        }

        /// <summary>
        /// Subtracts the 1st remote object from the second.  Throws an exception if the operation cannot be performed.
        /// </summary>
        public ObjectHandle Subtract([NotNull]ObjectHandle self, [NotNull]ObjectHandle other) {
            return new ObjectHandle(Subtract(GetLocalObject(self), GetLocalObject(other)));
        }

        /// <summary>
        /// Raises the 1st remote object to the power of the 2nd.  Throws an exception if the operation cannot be performed.
        /// </summary>
        public ObjectHandle Power([NotNull]ObjectHandle self, [NotNull]ObjectHandle other) {
            return new ObjectHandle(Power(GetLocalObject(self), GetLocalObject(other)));
        }

        /// <summary>
        /// Multiplies the two remote objects.  Throws an exception if the operation cannot be performed.
        /// </summary>
        public ObjectHandle Multiply([NotNull]ObjectHandle self, [NotNull]ObjectHandle other) {
            return new ObjectHandle(Multiply(GetLocalObject(self), GetLocalObject(other)));
        }

        /// <summary>
        /// Divides the 1st remote object by the 2nd. Throws an exception if the operation cannot be performed.
        /// </summary>
        public ObjectHandle Divide([NotNull]ObjectHandle self, [NotNull]ObjectHandle other) {
            return new ObjectHandle(Divide(GetLocalObject(self), GetLocalObject(other)));
        }

        /// <summary>
        /// Performs modulus on the 1st remote object by the 2nd.  Throws an exception if the operation cannot be performed.
        /// </summary>        
        public ObjectHandle Modulo([NotNull]ObjectHandle self, [NotNull]ObjectHandle other) {
            return new ObjectHandle(Modulo(GetLocalObject(self), GetLocalObject(other)));
        }

        /// <summary>
        /// Shifts the 1st remote object left by the 2nd remote object.  Throws an exception if the operation cannot be performed.
        /// </summary>
        public ObjectHandle LeftShift([NotNull]ObjectHandle self, [NotNull]ObjectHandle other) {
            return new ObjectHandle(LeftShift(GetLocalObject(self), GetLocalObject(other)));
        }

        /// <summary>
        /// Shifts the 1st remote  object right by the 2nd remote object.  Throws an exception if the operation cannot be performed.
        /// </summary>
        public ObjectHandle RightShift([NotNull]ObjectHandle self, [NotNull]ObjectHandle other) {
            return new ObjectHandle(RightShift(GetLocalObject(self), GetLocalObject(other)));
        }

        /// <summary>
        /// Performs bitwise-and on the two remote objects.  Throws an exception if the operation cannot be performed.
        /// </summary>
        public ObjectHandle BitwiseAnd([NotNull]ObjectHandle self, [NotNull]ObjectHandle other) {
            return new ObjectHandle(BitwiseAnd(GetLocalObject(self), GetLocalObject(other)));
        }

        /// <summary>
        /// Performs bitwise-or on the two remote objects.  Throws an exception if the operation cannot be performed.
        /// </summary>
        public ObjectHandle BitwiseOr([NotNull]ObjectHandle self, [NotNull]ObjectHandle other) {
            return new ObjectHandle(BitwiseOr(GetLocalObject(self), GetLocalObject(other)));
        }

        /// <summary>
        /// Performs exclusive-or on the two remote objects.  Throws an exception if the operation cannot be performed.
        /// </summary>
        public ObjectHandle ExclusiveOr([NotNull]ObjectHandle self, [NotNull]ObjectHandle other) {
            return new ObjectHandle(ExclusiveOr(GetLocalObject(self), GetLocalObject(other)));
        }

        /// <summary>
        /// Compares the two remote objects and returns true if the 1st is less than the 2nd.  Throws an exception if the operation cannot be performed.
        /// </summary>
        public bool LessThan([NotNull]ObjectHandle self, [NotNull]ObjectHandle other) {
            return LessThan(GetLocalObject(self), GetLocalObject(other));
        }

        /// <summary>
        /// Compares the two remote objects and returns true if the 1st is greater than the 2nd.  Throws an exception if the operation cannot be performed.
        /// </summary>
        public bool GreaterThan([NotNull]ObjectHandle self, [NotNull]ObjectHandle other) {
            return GreaterThan(GetLocalObject(self), GetLocalObject(other));
        }

        /// <summary>
        /// Compares the two remote objects and returns true if the 1st is less than or equal to the 2nd.  Throws an exception if the operation cannot be performed.
        /// </summary>
        public bool LessThanOrEqual([NotNull]ObjectHandle self, [NotNull]ObjectHandle other) {
            return LessThanOrEqual(GetLocalObject(self), GetLocalObject(other));
        }

        /// <summary>
        /// Compares the two remote objects and returns true if the 1st is greater than or equal to than the 2nd.  Throws an exception if the operation cannot be performed.
        /// </summary>
        public bool GreaterThanOrEqual([NotNull]ObjectHandle self, [NotNull]ObjectHandle other) {
            return GreaterThanOrEqual(GetLocalObject(self), GetLocalObject(other));
        }

        /// <summary>
        /// Compares the two remote objects and returns true if the 1st is equal to the 2nd.  Throws an exception if the operation cannot be performed.
        /// </summary>
        public bool Equal([NotNull]ObjectHandle self, [NotNull]ObjectHandle other) {
            return Equal(GetLocalObject(self), GetLocalObject(other));
        }

        /// <summary>
        /// Compares the two remote objects and returns true if the 1st is not equal to the 2nd.  Throws an exception if the operation cannot be performed.
        /// </summary>
        public bool NotEqual([NotNull]ObjectHandle self, [NotNull]ObjectHandle other) {
            return NotEqual(GetLocalObject(self), GetLocalObject(other));
        }

        /// <summary>
        /// Returns a string representation of the object in a langauge specific object display format.
        /// </summary>
        public string Format([NotNull]ObjectHandle obj) {
            return Format(GetLocalObject(obj));
        }

        /// <summary>
        /// Returns a list of strings which contain the known members of the remote object.
        /// </summary>
        public IList<string> GetMemberNames([NotNull]ObjectHandle obj) {
            return GetMemberNames(GetLocalObject(obj));
        }

        /// <summary>
        /// Returns a string providing documentation for the specified remote object.
        /// </summary>
        public string GetDocumentation([NotNull]ObjectHandle obj) {
            return GetDocumentation(GetLocalObject(obj));
        }

        /// <summary>
        /// Returns a list of signatures applicable for calling the specified object in a form displayable to the user.
        /// </summary>
        public IList<string> GetCallSignatures([NotNull]ObjectHandle obj) {
            return GetCallSignatures(GetLocalObject(obj));
        }

        /// <summary>
        /// Helper to unwrap an object - in the future maybe we should validate the current app domain.
        /// </summary>
        private static object GetLocalObject([NotNull]ObjectHandle obj) {
            ContractUtils.RequiresNotNull(obj, "obj");

            return obj.Unwrap();
        }

        /// <summary>
        /// Helper to unwrap multiple objects
        /// </summary>
        private static object[] GetLocalObjects(ObjectHandle[] ohs) {
            Debug.Assert(ohs != null);

            object[] res = new object[ohs.Length];
            for (int i = 0; i < res.Length; i++) {
                res[i] = GetLocalObject(ohs[i]);
            }

            return res;
        }
#endif

        #endregion

#if !SILVERLIGHT
        // TODO: Figure out what is the right lifetime
        [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.Infrastructure)]
        public override object InitializeLifetimeService() {
            return null;
        }
#endif
    }
}
Back to Top