File: ServiceChannelProxy.cs

package info (click to toggle)
mono 4.6.2.7%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 778,148 kB
  • ctags: 914,052
  • sloc: cs: 5,779,509; xml: 2,773,713; ansic: 432,645; sh: 14,749; makefile: 12,361; perl: 2,488; python: 1,434; cpp: 849; asm: 531; sql: 95; sed: 16; php: 1
file content (807 lines) | stat: -rw-r--r-- 29,846 bytes parent folder | download
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
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation.  All rights reserved.
//------------------------------------------------------------

namespace System.ServiceModel.Channels
{
    using System.Collections;
    using System.Collections.Specialized;
    using System.Globalization;
    using System.Reflection;
    using System.Runtime;
    using System.Runtime.Diagnostics;
    using System.Runtime.Remoting;
    using System.Runtime.Remoting.Messaging;
    using System.Runtime.Remoting.Proxies;
    using System.Security;
    using System.ServiceModel.Description;
    using System.ServiceModel.Diagnostics;
    using System.ServiceModel.Dispatcher;
    using System.Threading.Tasks;

    [Fx.Tag.SecurityNote(Critical = "Accesses a variety of LinkDemanded classes/methods (especially RealProxy)." +
        "Caller should protect access to the ServiceChannelProxy instance after construction.")]
#pragma warning disable 618 // have not moved to the v4 security model yet
    [SecurityCritical(SecurityCriticalScope.Everything)]
#pragma warning restore 618
    sealed class ServiceChannelProxy : RealProxy, IRemotingTypeInfo
    {
        const String activityIdSlotName = "E2ETrace.ActivityID";
        Type proxiedType;
        Type interfaceType;
        ServiceChannel serviceChannel;
        MbrObject objectWrapper;
        ImmutableClientRuntime proxyRuntime;
        MethodDataCache methodDataCache;

        internal ServiceChannelProxy(Type interfaceType, Type proxiedType, MessageDirection direction, ServiceChannel serviceChannel)
            : base(proxiedType)
        {
            if (!MessageDirectionHelper.IsDefined(direction))
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("direction"));

            this.interfaceType = interfaceType;
            this.proxiedType = proxiedType;
            this.serviceChannel = serviceChannel;
            this.proxyRuntime = serviceChannel.ClientRuntime.GetRuntime();
            this.methodDataCache = new MethodDataCache();

            this.objectWrapper = new MbrObject(this, proxiedType);
        }

        //Workaround is to set the activityid in remoting call's LogicalCallContext
        static LogicalCallContext SetActivityIdInLogicalCallContext(LogicalCallContext logicalCallContext)
        {
            if (TraceUtility.ActivityTracing)
            {
                logicalCallContext.SetData(activityIdSlotName, DiagnosticTraceBase.ActivityId);
            }

            return logicalCallContext;
        }

        IMethodReturnMessage CreateReturnMessage(object ret, object[] returnArgs, IMethodCallMessage methodCall)
        {
            if (returnArgs != null)
            {
                return CreateReturnMessage(ret, returnArgs, returnArgs.Length, SetActivityIdInLogicalCallContext(methodCall.LogicalCallContext), methodCall);
            }
            else
            {
                return new SingleReturnMessage(ret, methodCall);
            }
        }

        IMethodReturnMessage CreateReturnMessage(object ret, object[] outArgs, int outArgsCount, LogicalCallContext callCtx, IMethodCallMessage mcm)
        {
            return new ReturnMessage(ret, outArgs, outArgsCount, callCtx, mcm);
        }

        IMethodReturnMessage CreateReturnMessage(Exception e, IMethodCallMessage mcm)
        {
            return new ReturnMessage(e, mcm);
        }

        MethodData GetMethodData(IMethodCallMessage methodCall)
        {
            MethodBase method = methodCall.MethodBase;

            MethodData methodData;
            if (methodDataCache.TryGetMethodData(method, out methodData))
            {
                return methodData;
            }

            bool canCacheMessageData;

            Type declaringType = method.DeclaringType;
            if (declaringType == typeof(object))
            {
                MethodType methodType;
                if (methodCall.MethodBase == typeof(object).GetMethod("GetType"))
                {
                    methodType = MethodType.GetType;
                }
                else
                {
                    methodType = MethodType.Object;
                }
                canCacheMessageData = true;
                methodData = new MethodData(method, methodType);
            }
            else if (declaringType.IsInstanceOfType(this.serviceChannel))
            {
                canCacheMessageData = true;
                methodData = new MethodData(method, MethodType.Channel);
            }
            else
            {
                ProxyOperationRuntime operation = this.proxyRuntime.GetOperation(method, methodCall.Args, out canCacheMessageData);

                if (operation == null)
                {
                    if (this.serviceChannel.Factory != null)
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR.GetString(SR.SFxMethodNotSupported1, method.Name)));
                    else
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR.GetString(SR.SFxMethodNotSupportedOnCallback1, method.Name)));
                }

                MethodType methodType;

                if (operation.IsTaskCall(methodCall))
                {
                    methodType = MethodType.TaskService;
                }
                else if (operation.IsSyncCall(methodCall))
                {
                    methodType = MethodType.Service;
                }
                else if (operation.IsBeginCall(methodCall))
                {
                    methodType = MethodType.BeginService;
                }
                else
                {
                    methodType = MethodType.EndService;
                }

                methodData = new MethodData(method, methodType, operation);
            }

            if (canCacheMessageData)
            {
                methodDataCache.SetMethodData(methodData);
            }

            return methodData;
        }

        internal ServiceChannel GetServiceChannel()
        {
            return this.serviceChannel;
        }

        public override IMessage Invoke(IMessage message)
        {
            try
            {
                IMethodCallMessage methodCall = message as IMethodCallMessage;

                if (methodCall == null)
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.GetString(SR.SFxExpectedIMethodCallMessage)));

                MethodData methodData = GetMethodData(methodCall);

                switch (methodData.MethodType)
                {
                    case MethodType.Service:
                        return InvokeService(methodCall, methodData.Operation);
                    case MethodType.BeginService:
                        return InvokeBeginService(methodCall, methodData.Operation);
                    case MethodType.EndService:
                        return InvokeEndService(methodCall, methodData.Operation);
                    case MethodType.TaskService:
                        return InvokeTaskService(methodCall, methodData.Operation);
                    case MethodType.Channel:
                        return InvokeChannel(methodCall);
                    case MethodType.GetType:
                        return InvokeGetType(methodCall);
                    case MethodType.Object:
                        return InvokeObject(methodCall);
                    default:
                        Fx.Assert("Invalid proxy method type");
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(String.Format(CultureInfo.InvariantCulture, "Invalid proxy method type")));
                }
            }
#pragma warning suppress 56500 // covered by FxCOP
            catch (Exception e)
            {
                if (Fx.IsFatal(e))
                {
                    throw;
                }
                return CreateReturnMessage(e, message as IMethodCallMessage);
            }
        }

        static class TaskCreator
        {
            static readonly Func<ServiceChannel, ProxyOperationRuntime, object[], AsyncCallback, object, IAsyncResult> beginCallDelegate = ServiceChannel.BeginCall;
            static readonly Hashtable createGenericTaskDelegateCache = new Hashtable(); // using Hashtable because it allows for lock-free reads
            static readonly MethodInfo createGenericTaskMI = typeof(TaskCreator).GetMethod("CreateGenericTask", new Type[] { typeof(ServiceChannel), typeof(ProxyOperationRuntime), typeof(object[]) });

            static Func<ServiceChannel, ProxyOperationRuntime, object[], Task> GetOrCreateTaskDelegate(Type taskResultType)
            {
                Func<ServiceChannel, ProxyOperationRuntime, object[], Task> createTaskDelegate = createGenericTaskDelegateCache[taskResultType] as Func<ServiceChannel, ProxyOperationRuntime, object[], Task>;
                if (createTaskDelegate != null)
                {
                    return createTaskDelegate;
                }

                lock (createGenericTaskDelegateCache)
                {
                    createTaskDelegate = createGenericTaskDelegateCache[taskResultType] as Func<ServiceChannel, ProxyOperationRuntime, object[], Task>;
                    if (createTaskDelegate != null)
                    {
                        return createTaskDelegate;
                    }
                    
                    MethodInfo methodInfo = createGenericTaskMI.MakeGenericMethod(taskResultType);
                    createTaskDelegate = Delegate.CreateDelegate(typeof(Func<ServiceChannel, ProxyOperationRuntime, object[], Task>), methodInfo) as Func<ServiceChannel, ProxyOperationRuntime, object[], Task>;
                    createGenericTaskDelegateCache[taskResultType] = createTaskDelegate;
                }

                return createTaskDelegate;
            }

            public static Task CreateTask(ServiceChannel channel, IMethodCallMessage methodCall, ProxyOperationRuntime operation)
            {
                if (operation.TaskTResult == ServiceReflector.VoidType)
                {
                    return TaskCreator.CreateTask(channel, operation, methodCall.InArgs);
                }
                return TaskCreator.CreateGenericTask(channel, operation, methodCall.InArgs);
            }

            static Task CreateGenericTask(ServiceChannel channel, ProxyOperationRuntime operation, object[] inputParameters)
            {
                Func<ServiceChannel, ProxyOperationRuntime, object[], Task> createTaskDelegate = GetOrCreateTaskDelegate(operation.TaskTResult);
                return createTaskDelegate(channel, operation, inputParameters);
            }

            static Task CreateTask(ServiceChannel channel, ProxyOperationRuntime operation, object[] inputParameters)
            {
                Action<IAsyncResult> endCallDelegate = (asyncResult) =>
                {
                    Fx.Assert(asyncResult != null, "'asyncResult' MUST NOT be NULL.");
                    OperationContext originalOperationContext = OperationContext.Current;
                    OperationContext.Current = asyncResult.AsyncState as OperationContext;
                    try
                    {
                        channel.EndCall(operation.Action, ProxyOperationRuntime.EmptyArray, asyncResult);
                    }
                    finally
                    {
                        OperationContext.Current = originalOperationContext;
                    }
                };

                return Task.Factory.FromAsync(beginCallDelegate, endCallDelegate, channel, operation, inputParameters, OperationContext.Current);
            }

            public static Task<T> CreateGenericTask<T>(ServiceChannel channel, ProxyOperationRuntime operation, object[] inputParameters)
            {
                Func<IAsyncResult, T> endCallDelegate = (asyncResult) =>
                {
                    OperationContext originalOperationContext = OperationContext.Current;
                    OperationContext.Current = asyncResult.AsyncState as OperationContext;
                    try
                    {
                        return (T)channel.EndCall(operation.Action, ProxyOperationRuntime.EmptyArray, asyncResult);
                    }
                    finally
                    {
                        OperationContext.Current = originalOperationContext;
                    }
                };

                return Task<T>.Factory.FromAsync<ServiceChannel, ProxyOperationRuntime, object[]>(beginCallDelegate, endCallDelegate, channel, operation, inputParameters, OperationContext.Current);
            }
        }

        IMessage InvokeTaskService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
        {
            Task task = TaskCreator.CreateTask(this.serviceChannel, methodCall, operation);
            return CreateReturnMessage(task, null, methodCall);
        }

        IMethodReturnMessage InvokeChannel(IMethodCallMessage methodCall)
        {
            string activityName = null;
            ActivityType activityType = ActivityType.Unknown;
            if (DiagnosticUtility.ShouldUseActivity)
            {
                if (ServiceModelActivity.Current == null ||
                    ServiceModelActivity.Current.ActivityType != ActivityType.Close)
                {
                    MethodData methodData = this.GetMethodData(methodCall);
                    if (methodData.MethodBase.DeclaringType == typeof(System.ServiceModel.ICommunicationObject)
                        && methodData.MethodBase.Name.Equals("Close", StringComparison.Ordinal))
                    {
                        activityName = SR.GetString(SR.ActivityClose, this.serviceChannel.GetType().FullName);
                        activityType = ActivityType.Close;
                    }
                }
            }

            using (ServiceModelActivity activity = string.IsNullOrEmpty(activityName) ? null : ServiceModelActivity.CreateBoundedActivity())
            {
                if (DiagnosticUtility.ShouldUseActivity)
                {
                    ServiceModelActivity.Start(activity, activityName, activityType);
                }
                return ExecuteMessage(this.serviceChannel, methodCall);
            }
        }

        IMethodReturnMessage InvokeGetType(IMethodCallMessage methodCall)
        {
            return CreateReturnMessage(proxiedType, null, 0, SetActivityIdInLogicalCallContext(methodCall.LogicalCallContext), methodCall);
        }

        IMethodReturnMessage InvokeObject(IMethodCallMessage methodCall)
        {
            return RemotingServices.ExecuteMessage(this.objectWrapper, methodCall);
        }

        IMethodReturnMessage InvokeBeginService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
        {
            AsyncCallback callback;
            object asyncState;
            object[] ins = operation.MapAsyncBeginInputs(methodCall, out callback, out asyncState);
            object ret = this.serviceChannel.BeginCall(operation.Action, operation.IsOneWay, operation, ins, callback, asyncState);
            return CreateReturnMessage(ret, null, methodCall);
        }

        IMethodReturnMessage InvokeEndService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
        {
            IAsyncResult result;
            object[] outs;
            operation.MapAsyncEndInputs(methodCall, out result, out outs);
            object ret = this.serviceChannel.EndCall(operation.Action, outs, result);
            object[] returnArgs = operation.MapAsyncOutputs(methodCall, outs, ref ret);
            return CreateReturnMessage(ret, returnArgs, methodCall);
        }

        IMethodReturnMessage InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
        {
            object[] outs;
            object[] ins = operation.MapSyncInputs(methodCall, out outs);
            object ret = this.serviceChannel.Call(operation.Action, operation.IsOneWay, operation, ins, outs);
            object[] returnArgs = operation.MapSyncOutputs(methodCall, outs, ref ret);
            return CreateReturnMessage(ret, returnArgs, methodCall);
        }

        IMethodReturnMessage ExecuteMessage(object target, IMethodCallMessage methodCall)
        {
            MethodBase targetMethod = methodCall.MethodBase;

            object[] args = methodCall.Args;
            object returnValue = null;
            try
            {
                returnValue = targetMethod.Invoke(target, args);
            }
            catch (TargetInvocationException e)
            {
                return CreateReturnMessage(e.InnerException, methodCall);
            }

            return CreateReturnMessage(returnValue,
                                       args,
                                       args.Length,
                                       null,
                                       methodCall);
        }

        bool IRemotingTypeInfo.CanCastTo(Type toType, object o)
        {
            return toType.IsAssignableFrom(proxiedType) || serviceChannel.CanCastTo(toType);
        }

        string IRemotingTypeInfo.TypeName
        {
            get { return proxiedType.FullName; }
            set { }
        }

        class MethodDataCache
        {
            MethodData[] methodDatas;

            public MethodDataCache()
            {
                this.methodDatas = new MethodData[4];
            }

            object ThisLock
            {
                get { return this; }
            }

            public bool TryGetMethodData(MethodBase method, out MethodData methodData)
            {
                lock (ThisLock)
                {
                    MethodData[] methodDatas = this.methodDatas;
                    int index = FindMethod(methodDatas, method);
                    if (index >= 0)
                    {
                        methodData = methodDatas[index];
                        return true;
                    }
                    else
                    {
                        methodData = new MethodData();
                        return false;
                    }
                }
            }

            static int FindMethod(MethodData[] methodDatas, MethodBase methodToFind)
            {
                for (int i = 0; i < methodDatas.Length; i++)
                {
                    MethodBase method = methodDatas[i].MethodBase;
                    if (method == null)
                    {
                        break;
                    }
                    if (method == methodToFind)
                    {
                        return i;
                    }
                }
                return -1;
            }

            public void SetMethodData(MethodData methodData)
            {
                lock (ThisLock)
                {
                    int index = FindMethod(this.methodDatas, methodData.MethodBase);
                    if (index < 0)
                    {
                        for (int i = 0; i < this.methodDatas.Length; i++)
                        {
                            if (methodDatas[i].MethodBase == null)
                            {
                                methodDatas[i] = methodData;
                                return;
                            }
                        }
                        MethodData[] newMethodDatas = new MethodData[methodDatas.Length * 2];
                        Array.Copy(methodDatas, newMethodDatas, methodDatas.Length);
                        newMethodDatas[methodDatas.Length] = methodData;
                        this.methodDatas = newMethodDatas;
                    }
                }
            }
        }

        enum MethodType
        {
            Service,
            BeginService,
            EndService,
            Channel,
            Object,
            GetType,
            TaskService
        }

        struct MethodData
        {
            MethodBase methodBase;
            MethodType methodType;
            ProxyOperationRuntime operation;

            public MethodData(MethodBase methodBase, MethodType methodType)
                : this(methodBase, methodType, null)
            {
            }

            public MethodData(MethodBase methodBase, MethodType methodType, ProxyOperationRuntime operation)
            {
                this.methodBase = methodBase;
                this.methodType = methodType;
                this.operation = operation;
            }

            public MethodBase MethodBase
            {
                get { return methodBase; }
            }

            public MethodType MethodType
            {
                get { return methodType; }
            }

            public ProxyOperationRuntime Operation
            {
                get { return operation; }
            }
        }

        class MbrObject : MarshalByRefObject
        {
            RealProxy proxy;
            Type targetType;

            internal MbrObject(RealProxy proxy, Type targetType)
            {
                this.proxy = proxy;
                this.targetType = targetType;
            }

            public override bool Equals(object obj)
            {
                return Object.ReferenceEquals(obj, this.proxy.GetTransparentProxy());
            }

            public override string ToString()
            {
                return this.targetType.ToString();
            }

            public override int GetHashCode()
            {
                return this.proxy.GetHashCode();
            }
        }

        class SingleReturnMessage : IMethodReturnMessage
        {
            IMethodCallMessage methodCall;
            object ret;
            PropertyDictionary properties;

            public SingleReturnMessage(object ret, IMethodCallMessage methodCall)
            {
                this.ret = ret;
                this.methodCall = methodCall;
                this.properties = new PropertyDictionary();
            }

            public int ArgCount
            {
                get { return 0; }
            }

            public object[] Args
            {
                get { return EmptyArray.Instance; }
            }

            public Exception Exception
            {
                get { return null; }
            }

            public bool HasVarArgs
            {
                get { return methodCall.HasVarArgs; }
            }

            public LogicalCallContext LogicalCallContext
            {
                get { return SetActivityIdInLogicalCallContext(methodCall.LogicalCallContext); }
            }

            public MethodBase MethodBase
            {
                get { return methodCall.MethodBase; }
            }

            public string MethodName
            {
                get { return methodCall.MethodName; }
            }

            public object MethodSignature
            {
                get { return methodCall.MethodSignature; }
            }

            public object[] OutArgs
            {
                get { return EmptyArray.Instance; }
            }

            public int OutArgCount
            {
                get { return 0; }
            }

            public IDictionary Properties
            {
                get { return properties; }
            }

            public object ReturnValue
            {
                get { return ret; }
            }

            public string TypeName
            {
                get { return methodCall.TypeName; }
            }

            public string Uri
            {
                get { return null; }
            }

            public object GetArg(int index)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("index"));
            }

            public string GetArgName(int index)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("index"));
            }

            public object GetOutArg(int index)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("index"));
            }

            public string GetOutArgName(int index)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("index"));
            }

            class PropertyDictionary : IDictionary
            {
                ListDictionary properties;

                public object this[object key]
                {
                    get { return Properties[key]; }
                    set { Properties[key] = value; }
                }

                public int Count
                {
                    get { return Properties.Count; }
                }

                public bool IsFixedSize
                {
                    get { return false; }
                }

                public bool IsReadOnly
                {
                    get { return false; }
                }

                public bool IsSynchronized
                {
                    get { return false; }
                }

                public ICollection Keys
                {
                    get { return Properties.Keys; }
                }

                ListDictionary Properties
                {
                    get
                    {
                        if (properties == null)
                        {
                            properties = new ListDictionary();
                        }
                        return properties;
                    }
                }

                public ICollection Values
                {
                    get { return Properties.Values; }
                }

                public object SyncRoot
                {
                    get { return null; }
                }

                public void Add(object key, object value)
                {
                    Properties.Add(key, value);
                }

                public void Clear()
                {
                    Properties.Clear();
                }

                public bool Contains(object key)
                {
                    return Properties.Contains(key);
                }

                public void CopyTo(Array array, int index)
                {
                    Properties.CopyTo(array, index);
                }

                public IDictionaryEnumerator GetEnumerator()
                {
                    if (properties == null)
                    {
                        return EmptyEnumerator.Instance;
                    }
                    else
                    {
                        return properties.GetEnumerator();
                    }
                }

                IEnumerator IEnumerable.GetEnumerator()
                {
                    return ((IEnumerable)Properties).GetEnumerator();
                }

                public void Remove(object key)
                {
                    Properties.Remove(key);
                }

                class EmptyEnumerator : IDictionaryEnumerator
                {
                    static EmptyEnumerator instance = new EmptyEnumerator();

                    EmptyEnumerator()
                    {
                    }

                    public static EmptyEnumerator Instance
                    {
                        get { return instance; }
                    }

                    public bool MoveNext()
                    {
                        return false;
                    }

                    public Object Current
                    {
                        get
                        {
#pragma warning suppress 56503 // [....], IEnumerator guidelines, Current throws exception before calling MoveNext
                            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxDictionaryIsEmpty)));
                        }
                    }

                    public void Reset()
                    {
                    }

                    public Object Key
                    {
                        get
                        {
#pragma warning suppress 56503 // [....], IEnumerator guidelines, Current throws exception before calling MoveNext
                            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxDictionaryIsEmpty)));
                        }
                    }

                    public Object Value
                    {
                        get
                        {
#pragma warning suppress 56503 // [....], IEnumerator guidelines, Current throws exception before calling MoveNext
                            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxDictionaryIsEmpty)));
                        }
                    }

                    public DictionaryEntry Entry
                    {
                        get
                        {
#pragma warning suppress 56503 // [....], IEnumerator guidelines, Current throws exception before calling MoveNext
                            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxDictionaryIsEmpty)));
                        }
                    }
                }
            }
        }
    }
}