/
ObjectWriterContext.cs
1163 lines (1010 loc) · 41.9 KB
/
ObjectWriterContext.cs
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Reflection;
using System.Security;
using XAML3 = System.Windows.Markup;
using System.Xaml;
using MS.Internal.Xaml.Runtime;
using System.Xaml.Schema;
using MS.Internal.Xaml.Parser;
using System.Xaml.MS.Impl;
using System.Xaml.Permissions;
using System.IO;
using System.Windows.Markup;
using System.Windows;
namespace MS.Internal.Xaml.Context
{
internal class ObjectWriterContext : XamlContext
{
private XamlContextStack<ObjectWriterFrame> _stack;
private object _rootInstance = null;
ServiceProviderContext _serviceProviderContext;
XamlRuntime _runtime;
int _savedDepth; // The depth of the "saved" part this context is based on.
bool _nameResolutionComplete;
XamlObjectWriterSettings _settings;
List<NameScopeInitializationCompleteSubscriber> _nameScopeInitializationCompleteSubscribers;
public ObjectWriterContext(XamlSavedContext savedContext,
XamlObjectWriterSettings settings, INameScope rootNameScope, XamlRuntime runtime)
: base(savedContext.SchemaContext)
{
_stack = new XamlContextStack<ObjectWriterFrame>(savedContext.Stack, false);
if (settings != null)
{
_settings = settings.StripDelegates();
}
_runtime = runtime;
BaseUri = savedContext.BaseUri;
// If the bottom of the stack is a (no XamlType) Value (reparse) then back-up onto it.
// Otherwise add a blank frame to isolate template use from the saved context.
switch(savedContext.SaveContextType)
{
case SavedContextType.Template:
// Templates always need a root namescope, to isolate them from the rest of the doc
INameScopeDictionary rootNameScopeDictionary = null;
if (rootNameScope == null)
{
#if TARGETTING35SP1
rootNameScopeDictionary = new NameScopeDictionary(new NameScope());
#else
rootNameScopeDictionary = new NameScope();
#endif
}
else
{
rootNameScopeDictionary = rootNameScope as INameScopeDictionary;
if (rootNameScopeDictionary == null)
{
rootNameScopeDictionary = new NameScopeDictionary(rootNameScope);
}
}
// Push an extra frame to ensure that the template NameScope is
// not part of the saved context. Otherwise, the namescope
// will hold things alive as long as the template is alive
_stack.PushScope();
_savedDepth = _stack.Depth;
_stack.CurrentFrame.NameScopeDictionary = rootNameScopeDictionary;
_stack.PushScope();
break;
case SavedContextType.ReparseValue:
case SavedContextType.ReparseMarkupExtension:
Debug.Assert(rootNameScope == null, "Cannot pass a new namescope in to a reparse context");
_savedDepth = _stack.Depth - 1;
break;
}
}
public ObjectWriterContext(XamlSchemaContext schemaContext,
XamlObjectWriterSettings settings, INameScope rootNameScope, XamlRuntime runtime)
: base(schemaContext)
{
_stack = new XamlContextStack<ObjectWriterFrame>(() => new ObjectWriterFrame());
INameScopeDictionary rootNameScopeDictionary = null;
if (rootNameScope == null)
{
#if TARGETTING35SP1
rootNameScopeDictionary = new NameScopeDictionary(new NameScope());
#else
rootNameScopeDictionary = new NameScope();
#endif
}
else
{
rootNameScopeDictionary = rootNameScope as INameScopeDictionary;
if (rootNameScopeDictionary == null)
{
rootNameScopeDictionary = new NameScopeDictionary(rootNameScope);
}
}
_stack.CurrentFrame.NameScopeDictionary = rootNameScopeDictionary;
_stack.PushScope(); // put a blank sentinal frame on the stack.
if (settings != null)
{
_settings = settings.StripDelegates();
}
_runtime = runtime;
_savedDepth = 0;
}
public override Assembly LocalAssembly
{
get
{
Assembly result = base.LocalAssembly;
if (result == null && _settings != null && _settings.AccessLevel != null)
{
result = Assembly.Load(_settings.AccessLevel.AssemblyAccessToAssemblyName);
base.LocalAssembly = result;
}
return result;
}
protected set { base.LocalAssembly = value; }
}
internal ICheckIfInitialized IsInitializedCallback { get; set; }
internal bool NameResolutionComplete
{
get { return _nameResolutionComplete; }
set
{
// Resolution should never become incomplete once it's complete
Debug.Assert(!_nameResolutionComplete || value);
_nameResolutionComplete = value;
}
}
internal XamlRuntime Runtime
{
get
{
return _runtime;
}
}
#region ServiceProvider Interfaces
// This class doesn't implement the IServiceProvider. That is done
// with worker classes ValueConverterContext or MarkupConverterContext.
// The worker class implements IServiceProvider but uses the real
// context for the implementation of the actual services.
internal Type ServiceProvider_Resolve(string qName)
{
// As soon as we have the necessary setting on ObjectWriter, we need to start passing
// the local assembly into the context; currently, this will only return publics.
XamlType xamlType = ServiceProvider_ResolveXamlType(qName);
if (xamlType == null || xamlType.UnderlyingType == null)
{
XamlTypeName name = XamlTypeName.Parse(qName, this._serviceProviderContext);
xamlType = this.GetXamlType(name, true, true);
throw new XamlParseException(SR.Get(SRID.TypeNotFound, xamlType.GetQualifiedName()));
}
return xamlType.UnderlyingType;
}
internal XamlType ServiceProvider_ResolveXamlType(string qName)
{
return ResolveXamlType(qName, true);
}
internal AmbientPropertyValue ServiceProvider_GetFirstAmbientValue(IEnumerable<XamlType> ceilingTypes, XamlMember[] properties)
{
List<AmbientPropertyValue> valueList = FindAmbientValues(ceilingTypes, /*searchLiveStackOnly*/false, /*types*/null, properties, true);
return (valueList.Count == 0) ? null : valueList[0];
}
internal object ServiceProvider_GetFirstAmbientValue(XamlType[] types)
{
List<object> valueList = FindAmbientValues(types, true);
return (valueList.Count == 0) ? null : valueList[0];
}
internal IEnumerable<AmbientPropertyValue> ServiceProvider_GetAllAmbientValues(IEnumerable<XamlType> ceilingTypes, XamlMember[] properties)
{
List<AmbientPropertyValue> valueList = FindAmbientValues(ceilingTypes, /*searchLiveStackOnly*/false, /*types*/null, properties, /*stopAfterFirst*/ false);
return valueList;
}
internal IEnumerable<object> ServiceProvider_GetAllAmbientValues(XamlType[] types)
{
List<object> valueList = FindAmbientValues(types, false);
return valueList;
}
internal IEnumerable<AmbientPropertyValue> ServiceProvider_GetAllAmbientValues(IEnumerable<XamlType> ceilingTypes, bool searchLiveStackOnly, IEnumerable<XamlType> types, XamlMember[] properties)
{
List<AmbientPropertyValue> valueList = FindAmbientValues(ceilingTypes, searchLiveStackOnly, types, properties, false);
return valueList;
}
private static void CheckAmbient(XamlMember xamlMember)
{
if (!xamlMember.IsAmbient)
{
throw new ArgumentException(SR.Get(SRID.NotAmbientProperty, xamlMember.DeclaringType.Name, xamlMember.Name), "xamlMember");
}
}
private static void CheckAmbient(XamlType xamlType)
{
if (!xamlType.IsAmbient)
{
throw new ArgumentException(SR.Get(SRID.NotAmbientType, xamlType.Name), "xamlType");
}
}
internal XamlObjectWriterSettings ServiceProvider_GetSettings()
{
if (_settings == null)
{
_settings = new XamlObjectWriterSettings();
}
return _settings;
}
#endregion
// ----- abstracts overriden from XamlContext.
public override void AddNamespacePrefix(String prefix, string xamlNS)
{
_stack.CurrentFrame.AddNamespace(prefix, xamlNS);
}
public override string FindNamespaceByPrefix(string prefix)
{
string xamlNs;
ObjectWriterFrame frame = _stack.CurrentFrame;
while (frame.Depth > 0)
{
if (frame.TryGetNamespaceByPrefix(prefix, out xamlNs))
{
return xamlNs;
}
frame = (ObjectWriterFrame)frame.Previous;
}
return null;
}
public override IEnumerable<NamespaceDeclaration> GetNamespacePrefixes()
{
ObjectWriterFrame frame = _stack.CurrentFrame;
Dictionary<string, string> keys = new Dictionary<string, string>();
while (frame.Depth > 0)
{
if (frame._namespaces != null)
{
foreach (NamespaceDeclaration namespaceDeclaration in frame.GetNamespacePrefixes())
{
if (!keys.ContainsKey(namespaceDeclaration.Prefix))
{
keys.Add(namespaceDeclaration.Prefix, null);
yield return namespaceDeclaration;
}
}
}
frame = (ObjectWriterFrame)frame.Previous;
}
}
// Method to easily add the lineinfo to a Xaml Exception.
public XamlException WithLineInfo(XamlException ex)
{
ex.SetLineInfo(LineNumber, LinePosition);
return ex;
}
// ----- methods to support the Service Providers
internal ServiceProviderContext ServiceProviderContext
{
get
{
if (null == _serviceProviderContext)
{
_serviceProviderContext = new ServiceProviderContext(this);
}
return _serviceProviderContext;
}
}
internal XamlType GetDestinationType()
{
ObjectWriterFrame frame = _stack.CurrentFrame;
if (frame == null)
{
return null;
}
if (frame.Instance != null && frame.XamlType == null)
{
//
// Text/TypeConverter, we need to go up a frame
frame = frame.Previous as ObjectWriterFrame;
}
if (frame.Member == XamlLanguage.Initialization)
{
return frame.XamlType;
}
return frame.Member.Type;
}
private List<AmbientPropertyValue> FindAmbientValues(IEnumerable<XamlType> ceilingTypesEnumerable,
bool searchLiveStackOnly,
IEnumerable<XamlType> types,
XamlMember[] properties,
bool stopAfterFirst)
{
ArrayHelper.ForAll<XamlMember>(properties, CheckAmbient);
List<XamlType> ceilingTypes = ArrayHelper.ToList<XamlType>(ceilingTypesEnumerable);
List<AmbientPropertyValue> retList = new List<AmbientPropertyValue>();
// Start the search for ambient properties and types starting with the parent frame.
ObjectWriterFrame frame = _stack.PreviousFrame;
ObjectWriterFrame lowerFrame = _stack.CurrentFrame;
while (frame.Depth >= 1)
{
if (searchLiveStackOnly && frame.Depth <= SavedDepth)
{
break;
}
object inst = frame.Instance;
if (types != null)
{
foreach (XamlType type in types)
{
if (frame.XamlType != null && frame.XamlType.CanAssignTo(type))
{
if (inst != null)
{
AmbientPropertyValue apValue = new AmbientPropertyValue(null, inst);
retList.Add(apValue);
}
}
}
}
if (properties != null)
{
foreach (XamlMember prop in properties)
{
bool returnAmbientValue = false;
object value = null;
if (frame.XamlType != null && frame.XamlType.CanAssignTo(prop.DeclaringType))
{
if (inst != null)
{
// If we are searching from inside the target Ambient property,
// (like StaticResource inside a ResourceDictionary)
// and the object is bottom-up, then it won't be assigned to
// the object but will only exist on the parse stack.
// If it is top-down it will be attached to the instance already
// and the normal path will serve.
if (prop == frame.Member && lowerFrame.Instance != null
&& lowerFrame.XamlType != null && !lowerFrame.XamlType.IsUsableDuringInitialization)
{
// One last thing to check: If the object we are inside is a ME
// then we are inside a call to ProvideValue and we don't want to
// return a reference to ourselves to ourselves.
if (!typeof(MarkupExtension).IsAssignableFrom(lowerFrame.Instance.GetType()))
{
returnAmbientValue = true;
value = lowerFrame.Instance;
}
}
else
{ // The Ambient Property is either Fully build or not set.
// FIRST: Ask the object (via IQueryAmbient interface) if it has a value for this property.
// This is usefull to prevent needless creation of empty lazy properties.
var ambientCtrl = inst as IQueryAmbient;
// If there is no ambientControl or if ambientControl says YES, then get the property value.
if (ambientCtrl == null || ambientCtrl.IsAmbientPropertyAvailable(prop.Name))
{
// Demand for XamlLoadPermission will fail if we're trying to
// access an internal property from a partial-trust ME/TC
returnAmbientValue = true;
value = _runtime.GetValue(inst, prop);
}
}
}
if (returnAmbientValue)
{
AmbientPropertyValue apValue = new AmbientPropertyValue(prop, value);
retList.Add(apValue);
}
}
}
}
if (stopAfterFirst && retList.Count > 0)
{
break;
}
if (ceilingTypes != null)
{
if (ceilingTypes.Contains(frame.XamlType))
{
break;
}
}
lowerFrame = frame;
frame = (ObjectWriterFrame)frame.Previous;
Debug.Assert(frame != null);
}
return retList;
}
private List<object> FindAmbientValues(XamlType[] types, bool stopAfterFirst)
{
ArrayHelper.ForAll<XamlType>(types, CheckAmbient);
List<object> retList = new List<object>();
// Start the search for ambient properties with the parent frame.
ObjectWriterFrame frame = _stack.PreviousFrame;
ObjectWriterFrame lowerFrame = _stack.CurrentFrame;
while (frame.Depth >= 1)
{
foreach (XamlType type in types)
{
object inst = frame.Instance;
if (frame.XamlType != null && frame.XamlType.CanAssignTo(type))
{
if (inst != null)
{
retList.Add(inst);
if (stopAfterFirst)
{
return retList;
}
}
}
}
lowerFrame = frame;
frame = (ObjectWriterFrame)frame.Previous;
Debug.Assert(frame != null);
}
return retList;
}
// ----- new public methods.
public void PushScope()
{
_stack.PushScope();
}
// Don't call this - serious workaround
public void LiftScope()
{
_stack.Depth--;
}
// Don't call this - serious workaround
public void UnLiftScope()
{
_stack.Depth++;
}
public void PopScope()
{
_stack.PopScope();
}
/// <summary>
/// Total depth of the stack SavedDepth+LiveDepth
/// </summary>
public int Depth
{
get { return _stack.Depth; }
}
/// <summary>
/// The Depth of the Saved (template) part.
/// </summary>
public int SavedDepth
{
get { return _savedDepth; }
}
/// <summary>
/// The Depth of the Stack above the Saved (template) part
/// </summary>
public int LiveDepth
{
get { return Depth - SavedDepth; }
}
public XamlType CurrentType
{
get { return _stack.CurrentFrame.XamlType; }
set { _stack.CurrentFrame.XamlType = value; }
}
public XamlType ParentType
{
get { return _stack.PreviousFrame.XamlType; }
}
public XamlType GrandParentType
{
get { return (_stack.PreviousPreviousFrame != null) ? _stack.PreviousPreviousFrame.XamlType : null; }
}
public XamlMember CurrentProperty
{
get { return _stack.CurrentFrame.Member; }
set { _stack.CurrentFrame.Member = value; }
}
public XamlMember ParentProperty
{
get { return _stack.PreviousFrame.Member; }
}
public XamlMember GrandParentProperty
{
get { return _stack.PreviousPreviousFrame.Member; }
}
public Object CurrentInstance
{
get { return _stack.CurrentFrame.Instance; }
set { _stack.CurrentFrame.Instance = value; }
}
public Object ParentInstance
{
get { return _stack.PreviousFrame.Instance; }
}
public Object GrandParentInstance
{
get { return (_stack.PreviousPreviousFrame != null) ? _stack.PreviousPreviousFrame.Instance : null; }
}
public Object CurrentCollection
{
get { return _stack.CurrentFrame.Collection; }
set { _stack.CurrentFrame.Collection = value; }
}
public object ParentCollection
{
get { return _stack.PreviousFrame.Collection; }
}
public bool CurrentWasAssignedAtCreation
{
get { return _stack.CurrentFrame.WasAssignedAtCreation; }
set { _stack.CurrentFrame.WasAssignedAtCreation = value; }
}
public bool CurrentIsObjectFromMember
{
get { return _stack.CurrentFrame.IsObjectFromMember; }
set { _stack.CurrentFrame.IsObjectFromMember = value; }
}
public bool ParentIsObjectFromMember
{
get { return _stack.PreviousFrame.IsObjectFromMember; }
}
public bool GrandParentIsObjectFromMember
{
get { return (_stack.PreviousPreviousFrame != null) ? _stack.PreviousPreviousFrame.IsObjectFromMember : false; }
}
public bool CurrentIsPropertyValueSet
{
// get { return _stack.CurrentFrame.IsPropertyValueSet; } Currently Unused (FxCop)
set { _stack.CurrentFrame.IsPropertyValueSet = value; }
}
public bool ParentIsPropertyValueSet
{
get { return _stack.PreviousFrame.IsPropertyValueSet; }
set { _stack.PreviousFrame.IsPropertyValueSet = value; }
}
public bool CurrentIsTypeConvertedObject
{
get { return _stack.CurrentFrame.IsTypeConvertedObject; }
set { _stack.CurrentFrame.IsTypeConvertedObject = value; }
}
public Dictionary<XamlMember, object> CurrentPreconstructionPropertyValues
{
get { return _stack.CurrentFrame.PreconstructionPropertyValues; }
}
public bool CurrentHasPreconstructionPropertyValuesDictionary
{
get { return _stack.CurrentFrame.HasPreconstructionPropertyValuesDictionary; }
}
public Dictionary<XamlMember, object> ParentPreconstructionPropertyValues
{
get { return _stack.PreviousFrame.PreconstructionPropertyValues; }
}
public HashSet<XamlMember> CurrentAssignedProperties
{
get { return _stack.CurrentFrame.AssignedProperties; }
}
public HashSet<XamlMember> ParentAssignedProperties
{
get { return _stack.PreviousFrame.AssignedProperties; }
}
public string CurrentInstanceRegisteredName
{
get { return _stack.CurrentFrame.InstanceRegisteredName; }
set { _stack.CurrentFrame.InstanceRegisteredName = value; }
}
public string ParentInstanceRegisteredName
{
get { return _stack.PreviousFrame.InstanceRegisteredName; }
set { _stack.PreviousFrame.InstanceRegisteredName = value; }
}
public Uri BaseUri { get; set; }
public int LineNumber { get; set; }
public int LinePosition { get; set; }
// Used only for BeginInitHandler, in place of BaseUri.
public Uri SourceBamlUri
{
get { return _settings != null ? _settings.SourceBamlUri : null; }
}
// This specifically stores the start line number for a start object for consistency
public int LineNumber_StartObject { get; set; }
// This specifically stores the start line position for a start object for consistency
public int LinePosition_StartObject { get; set; }
public INameScopeDictionary CurrentNameScope
{
get
{
return LookupNameScopeDictionary((ObjectWriterFrame)_stack.CurrentFrame);
}
}
public INameScopeDictionary ParentNameScope
{
get
{
return LookupNameScopeDictionary((ObjectWriterFrame)_stack.PreviousFrame);
}
}
public INameScopeDictionary GrandParentNameScope
{
get
{
return LookupNameScopeDictionary((ObjectWriterFrame)_stack.PreviousPreviousFrame);
}
}
public INameScopeDictionary RootNameScope
{
get
{
ObjectWriterFrame rootFrame = _stack.GetFrame(SavedDepth + 1);
return LookupNameScopeDictionary(rootFrame);
}
}
/// <summary>
/// From x:Arguments or ME positional syntax.
/// </summary>
public object[] CurrentCtorArgs
{
get { return _stack.CurrentFrame.PositionalCtorArgs; }
set { _stack.CurrentFrame.PositionalCtorArgs = value; }
}
public object CurrentKey
{
get { return _stack.CurrentFrame.Key; }
}
public bool CurrentIsKeySet
{
get { return _stack.CurrentFrame.IsKeySet; }
}
public object ParentKey
{
get { return _stack.PreviousFrame.Key; }
set
{
_stack.PreviousFrame.Key = value;
}
}
public bool CurrentKeyIsUnconverted
{
get { return _stack.CurrentFrame.KeyIsUnconverted; }
set { _stack.CurrentFrame.KeyIsUnconverted = value; }
}
public bool ParentKeyIsUnconverted
{
// Write-only property: Getter was dead code, so removed per FxCop; add back if needed.
set { _stack.PreviousFrame.KeyIsUnconverted = value; }
}
public bool ParentShouldConvertChildKeys
{
get { return _stack.PreviousFrame.ShouldConvertChildKeys; }
set { _stack.PreviousPreviousFrame.ShouldConvertChildKeys = value; }
}
public bool GrandParentShouldConvertChildKeys
{
get { return _stack.PreviousPreviousFrame.ShouldConvertChildKeys; }
set { _stack.PreviousPreviousFrame.ShouldConvertChildKeys = value; }
}
public bool ParentShouldNotConvertChildKeys
{
get { return _stack.PreviousFrame.ShouldNotConvertChildKeys; }
set { _stack.PreviousPreviousFrame.ShouldNotConvertChildKeys = value; }
}
public bool GrandParentShouldNotConvertChildKeys
{
get { return _stack.PreviousPreviousFrame.ShouldNotConvertChildKeys; }
}
public object RootInstance
{
get
{
//evaluate if _rootInstance should just always look at _rootFrame.Instance instead of caching an instance
if (_rootInstance == null)
{
ObjectWriterFrame rootFrame = GetTopFrame();
_rootInstance = rootFrame.Instance;
}
return _rootInstance;
}
}
// Consider replacing GetTopFrame with _rootFrame, _liveRootFrame
private ObjectWriterFrame GetTopFrame()
{
if (_stack.Depth == 0)
{
return null;
}
XamlFrame frame = _stack.CurrentFrame;
while (frame.Depth > 1)
{
frame = frame.Previous;
}
return (ObjectWriterFrame)frame;
}
private INameScopeDictionary LookupNameScopeDictionary(ObjectWriterFrame frame)
{
if (frame.NameScopeDictionary == null)
{
if (frame.XamlType != null && frame.XamlType.IsNameScope)
{
frame.NameScopeDictionary = frame.Instance as INameScopeDictionary ?? new NameScopeDictionary(frame.Instance as INameScope);
}
if (frame.NameScopeDictionary == null)
{
if (frame.Depth == 1)
{
frame.NameScopeDictionary = HuntAroundForARootNameScope(frame);
}
else if (frame.Depth > 1)
{
if (frame.Depth == SavedDepth + 1 &&
_settings != null && !_settings.RegisterNamesOnExternalNamescope)
{
#if TARGETTING35SP1
frame.NameScopeDictionary = new NameScopeDictionary(new NameScope());
#else
frame.NameScopeDictionary = new NameScope();
#endif
}
else
{
var parentFrame = (ObjectWriterFrame)frame.Previous;
frame.NameScopeDictionary = LookupNameScopeDictionary(parentFrame);
}
}
}
}
// We are sure to find a name scope at the root (at least).
Debug.Assert(frame.NameScopeDictionary != null || frame.Depth == 0);
return frame.NameScopeDictionary;
}
public IEnumerable<INameScopeDictionary> StackWalkOfNameScopes
{
get
{
var frame = (ObjectWriterFrame)_stack.CurrentFrame;
INameScopeDictionary previousNameScopeDictionary = null;
INameScopeDictionary nameScopeDictionary = null;
while (frame.Depth > 0)
{
nameScopeDictionary = LookupNameScopeDictionary(frame);
Debug.Assert(nameScopeDictionary != null);
if (frame.NameScopeDictionary != previousNameScopeDictionary)
{
previousNameScopeDictionary = nameScopeDictionary;
yield return nameScopeDictionary;
}
frame = (ObjectWriterFrame)frame.Previous;
}
// return the provided root namescope if it's different from the document root namescope
if (frame.NameScopeDictionary != null && frame.NameScopeDictionary != previousNameScopeDictionary)
{
yield return frame.NameScopeDictionary;
}
}
}
public bool IsOnTheLiveStack(object instance)
{
var frame = (ObjectWriterFrame)_stack.CurrentFrame;
while (frame.Depth > SavedDepth)
{
if (instance == frame.Instance)
{
return true;
}
frame = (ObjectWriterFrame)frame.Previous;
}
return false;
}
private INameScopeDictionary HuntAroundForARootNameScope(ObjectWriterFrame rootFrame)
{
Debug.Assert(rootFrame.Depth == 1);
object inst = rootFrame.Instance;
if (inst == null && rootFrame.XamlType.IsNameScope)
{
throw new InvalidOperationException(SR.Get(SRID.NameScopeOnRootInstance));
}
INameScopeDictionary nameScopeDictionary = null;
nameScopeDictionary = inst as INameScopeDictionary;
if (nameScopeDictionary == null)
{
INameScope nameScope = inst as INameScope;
if (nameScope != null)
{
nameScopeDictionary = new NameScopeDictionary(nameScope);
}
}
// If the root instance isn't a name scope
// then perhaps it designated a property as the name scope.
if (nameScopeDictionary == null)
{
XamlType xamlType = rootFrame.XamlType;
if (xamlType.UnderlyingType != null)
{
// Get the Name Scope Property (from attribute on the class)
XamlMember nameScopeProperty = TypeReflector.LookupNameScopeProperty(xamlType);
if (nameScopeProperty != null)
{
// Read the value of the property. If it is an object we are good.
// if it is null create a stock name scope dictionary object and assign it back.
INameScope nameScope = (INameScope)_runtime.GetValue(inst, nameScopeProperty, false);
if (nameScope == null)
{
#if TARGETTING35SP1
nameScopeDictionary = new NameScopeDictionary(new NameScope());
#else
nameScopeDictionary = new NameScope();
#endif
_runtime.SetValue(inst, nameScopeProperty, nameScopeDictionary);
}
else
{
nameScopeDictionary = nameScope as INameScopeDictionary;
if (nameScopeDictionary == null)
{
nameScopeDictionary = new NameScopeDictionary(nameScope);
}
}
}
}
}
if (nameScopeDictionary == null && _settings != null
&& _settings.RegisterNamesOnExternalNamescope)
{
ObjectWriterFrame frameZero = (ObjectWriterFrame)rootFrame.Previous;
nameScopeDictionary = frameZero.NameScopeDictionary;
}
// Otherwise we still need a namescope at the root of the parse
// for our own usage. For IXamlNameResolver() to use.
if (nameScopeDictionary == null)
{
#if TARGETTING35SP1
nameScopeDictionary = new NameScopeDictionary(new NameScope());
#else
nameScopeDictionary = new NameScope();
#endif
}
rootFrame.NameScopeDictionary = nameScopeDictionary;
return nameScopeDictionary;
}
public XamlSavedContext GetSavedContext(SavedContextType savedContextType)
{
// Ensure that we have a root namescope before cloning the stack
ObjectWriterFrame topFrame = GetTopFrame();
if (topFrame.NameScopeDictionary == null)
{
topFrame.NameScopeDictionary = LookupNameScopeDictionary(topFrame);
}
// Clone the stack
var newStack = new XamlContextStack<ObjectWriterFrame>(_stack, true);
XamlSavedContext savedContext = new XamlSavedContext(savedContextType, this, newStack);
return savedContext;
}
public object ResolveName(string name, out bool isFullyInitialized)
{
isFullyInitialized = false;
object value = null;
foreach (INameScope nameScope in StackWalkOfNameScopes)
{
object obj = nameScope.FindName(name);
if (obj != null)
{
if (IsInitializedCallback != null)
{
isFullyInitialized = IsInitializedCallback.IsFullyInitialized(obj);
}
if (NameResolutionComplete || isFullyInitialized || IsInitializedCallback == null)
{
value = obj;