-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDesignerSerializationManager.cs
More file actions
992 lines (913 loc) · 45.2 KB
/
DesignerSerializationManager.cs
File metadata and controls
992 lines (913 loc) · 45.2 KB
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
using System;
using System.Collections;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.ComponentModel.Design.Serialization;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Serialization;
using System.Text;
[assembly: CLSCompliant(true)]
[assembly: InternalsVisibleTo("LogicBuilder.ComponentModel.Design.Serialization.Tests, PublicKey=002400000480000094000000060200000024000052534131000400000100010059b59302e7303accd5cc84fd482cae54dea8d8b8de7faaef37abbac4b08e3d91283087f48ae04c4fdd117752a3fcafcda61cd2099e2d5432b9bce70e5fe083b15e43cd652617b06dc1422d347ffe7b2aeb7b466e567c6988f26dccbf9723b4b57b1aeaa0a2dbd00478d7135da9bb04a6138d5f29e54ac7e9ac9ae3b7956cf6c2")]
namespace LogicBuilder.ComponentModel.Design.Serialization
{
/// <summary>Provides an implementation of the <see cref="T:System.ComponentModel.Design.Serialization.IDesignerSerializationManager" /> interface.</summary>
public class DesignerSerializationManager : IDesignerSerializationManager
{
[ExcludeFromCodeCoverage]
private sealed class SerializationSession : IDisposable
{
private readonly DesignerSerializationManager serializationManager;
internal SerializationSession(DesignerSerializationManager serializationManager)
{
this.serializationManager = serializationManager;
}
public void Dispose()
{
this.serializationManager.OnSessionDisposed(EventArgs.Empty);
}
}
[ExcludeFromCodeCoverage]
private sealed class ReferenceComparer : IEqualityComparer
{
bool IEqualityComparer.Equals(object x, object y)
{
return x == y;
}
int IEqualityComparer.GetHashCode(object obj)
{
if (obj != null)
{
return obj.GetHashCode();
}
return 0;
}
}
[ExcludeFromCodeCoverage]
private sealed class WrappedPropertyDescriptor : PropertyDescriptor
{
private readonly object target;
private readonly PropertyDescriptor property;
public override AttributeCollection Attributes
{
get
{
return this.property.Attributes;
}
}
public override Type ComponentType
{
get
{
return this.property.ComponentType;
}
}
public override bool IsReadOnly
{
get
{
return this.property.IsReadOnly;
}
}
public override Type PropertyType
{
get
{
return this.property.PropertyType;
}
}
internal WrappedPropertyDescriptor(PropertyDescriptor property, object target) : base(property.Name, null)
{
this.property = property;
this.target = target;
}
public override bool CanResetValue(object component)
{
return this.property.CanResetValue(this.target);
}
public override object GetValue(object component)
{
return this.property.GetValue(this.target);
}
public override void ResetValue(object component)
{
this.property.ResetValue(this.target);
}
public override void SetValue(object component, object value)
{
this.property.SetValue(this.target, value);
}
public override bool ShouldSerializeValue(object component)
{
return this.property.ShouldSerializeValue(this.target);
}
}
private readonly IServiceProvider serviceProvider;
private ITypeResolutionService typeResolver;
private bool searchedTypeResolver;
private bool recycleInstances;
private bool validateRecycledTypes;
private bool preserveNames;
private IContainer container;
private IDisposable session;
private ResolveNameEventHandler resolveNameEventHandler;
private EventHandler serializationCompleteEventHandler;
private EventHandler sessionCreatedEventHandler;
private EventHandler sessionDisposedEventHandler;
private ArrayList designerSerializationProviders;
private Hashtable defaultProviderTable;
private Hashtable instancesByName;
private Hashtable namesByInstance;
private Hashtable serializers;
private ArrayList errorList;
private ContextStack contextStack;
private PropertyDescriptorCollection properties;
private object propertyProvider;
/// <summary>Occurs when a session is created. </summary>
public event EventHandler SessionCreated
{
add
{
this.sessionCreatedEventHandler = (EventHandler)Delegate.Combine(this.sessionCreatedEventHandler, value);
}
remove
{
this.sessionCreatedEventHandler = (EventHandler)Delegate.Remove(this.sessionCreatedEventHandler, value);
}
}
/// <summary>Occurs when a session is disposed.</summary>
public event EventHandler SessionDisposed
{
add
{
this.sessionDisposedEventHandler = (EventHandler)Delegate.Combine(this.sessionDisposedEventHandler, value);
}
remove
{
this.sessionDisposedEventHandler = (EventHandler)Delegate.Remove(this.sessionDisposedEventHandler, value);
}
}
/// <summary>Occurs when <see cref="M:System.ComponentModel.Design.Serialization.DesignerSerializationManager.System#ComponentModel#Design#Serialization#IDesignerSerializationManager#GetName(System.Object)" /> cannot locate the specified name in the serialization manager's name table. </summary>
/// <exception cref="T:System.InvalidOperationException">The serialization manager does not have an active serialization session.</exception>
event ResolveNameEventHandler IDesignerSerializationManager.ResolveName
{
add
{
this.CheckSession();
this.resolveNameEventHandler = (ResolveNameEventHandler)Delegate.Combine(this.resolveNameEventHandler, value);
}
remove
{
this.resolveNameEventHandler = (ResolveNameEventHandler)Delegate.Remove(this.resolveNameEventHandler, value);
}
}
/// <summary>Occurs when serialization is complete.</summary>
/// <exception cref="T:System.InvalidOperationException">The serialization manager does not have an active serialization session.</exception>
event EventHandler IDesignerSerializationManager.SerializationComplete
{
add
{
this.CheckSession();
this.serializationCompleteEventHandler = (EventHandler)Delegate.Combine(this.serializationCompleteEventHandler, value);
}
remove
{
this.serializationCompleteEventHandler = (EventHandler)Delegate.Remove(this.serializationCompleteEventHandler, value);
}
}
/// <summary>Gets or sets to the container for this serialization manager.</summary>
/// <returns>The <see cref="T:System.ComponentModel.IContainer" /> to which the serialization manager will add components.</returns>
/// <exception cref="T:System.InvalidOperationException">The serialization manager has an active serialization session.</exception>
public IContainer Container
{
get
{
if (this.container == null && this.GetService(typeof(IDesignerHost)) is IDesignerHost designerHost)
{
this.container = designerHost.Container;
}
return this.container;
}
set
{
this.CheckNoSession();
this.container = value;
}
}
/// <summary>Gets the list of errors that occurred during serialization or deserialization.</summary>
/// <returns>The list of errors that occurred during serialization or deserialization.</returns>
/// <exception cref="T:System.InvalidOperationException">This property was accessed outside of a serialization session.</exception>
public IList Errors
{
get
{
this.CheckSession();
this.errorList ??= [];
return this.errorList;
}
}
/// <summary>Gets or sets a value indicating whether the <see cref="M:System.ComponentModel.Design.Serialization.DesignerSerializationManager.CreateInstance(System.Type,System.Collections.ICollection,System.String,System.Boolean)" /> method should check for the presence of the given name in the container.</summary>
/// <returns>true if <see cref="M:System.ComponentModel.Design.Serialization.DesignerSerializationManager.CreateInstance(System.Type,System.Collections.ICollection,System.String,System.Boolean)" /> will pass the given component name; false if <see cref="M:System.ComponentModel.Design.Serialization.DesignerSerializationManager.CreateInstance(System.Type,System.Collections.ICollection,System.String,System.Boolean)" /> will check for the presence of the given name in the container. The default is true.</returns>
/// <exception cref="T:System.InvalidOperationException">This property was changed from within a serialization session.</exception>
public bool PreserveNames
{
get
{
return this.preserveNames;
}
set
{
this.CheckNoSession();
this.preserveNames = value;
}
}
/// <summary>Gets the object that should be used to provide properties to the serialization manager's <see cref="P:System.ComponentModel.Design.Serialization.IDesignerSerializationManager.Properties" /> property.</summary>
/// <returns>The object that should be used to provide properties to the serialization manager's <see cref="P:System.ComponentModel.Design.Serialization.IDesignerSerializationManager.Properties" /> property.</returns>
public object PropertyProvider
{
get
{
return this.propertyProvider;
}
set
{
if (this.propertyProvider != value)
{
this.propertyProvider = value;
this.properties = null;
}
}
}
/// <summary>Gets or sets a flag indicating whether <see cref="M:System.ComponentModel.Design.Serialization.DesignerSerializationManager.CreateInstance(System.Type,System.Collections.ICollection,System.String,System.Boolean)" /> will always create a new instance of a type. </summary>
/// <returns>true if <see cref="M:System.ComponentModel.Design.Serialization.DesignerSerializationManager.CreateInstance(System.Type,System.Collections.ICollection,System.String,System.Boolean)" /> will return the existing instance; false if <see cref="M:System.ComponentModel.Design.Serialization.DesignerSerializationManager.CreateInstance(System.Type,System.Collections.ICollection,System.String,System.Boolean)" /> will create a new instance of a type. The default is false.</returns>
/// <exception cref="T:System.InvalidOperationException">The serialization manager has an active serialization session.</exception>
public bool RecycleInstances
{
get
{
return this.recycleInstances;
}
set
{
this.CheckNoSession();
this.recycleInstances = value;
}
}
/// <summary>Gets or sets a flag indicating whether the <see cref="M:System.ComponentModel.Design.Serialization.DesignerSerializationManager.CreateInstance(System.Type,System.Collections.ICollection,System.String,System.Boolean)" /> method will verify that matching names refer to the same type.</summary>
/// <returns>true if <see cref="M:System.ComponentModel.Design.Serialization.DesignerSerializationManager.CreateInstance(System.Type,System.Collections.ICollection,System.String,System.Boolean)" /> verifies types; otherwise, false if it does not. The default is true.</returns>
/// <exception cref="T:System.InvalidOperationException">The serialization manager has an active serialization session.</exception>
public bool ValidateRecycledTypes
{
get
{
return this.validateRecycledTypes;
}
set
{
this.CheckNoSession();
this.validateRecycledTypes = value;
}
}
/// <summary>Gets the context stack for this serialization session. </summary>
/// <returns>A <see cref="T:System.ComponentModel.Design.Serialization.ContextStack" /> that stores data.</returns>
/// <exception cref="T:System.InvalidOperationException">This property was accessed outside of a serialization session.</exception>
ContextStack IDesignerSerializationManager.Context
{
get
{
if (this.contextStack == null)
{
this.CheckSession();
this.contextStack = new ContextStack();
}
return this.contextStack;
}
}
/// <summary>Implements the <see cref="P:System.ComponentModel.Design.Serialization.IDesignerSerializationManager.Properties" /> property. </summary>
/// <returns>A <see cref="T:System.ComponentModel.PropertyDescriptorCollection" /> containing the properties to be serialized.</returns>
PropertyDescriptorCollection IDesignerSerializationManager.Properties
{
get
{
if (this.properties == null)
{
object obj = this.PropertyProvider;
PropertyDescriptor[] array;
if (obj == null)
{
array = [];
}
else
{
PropertyDescriptorCollection propertyDescriptorCollection = TypeDescriptor.GetProperties(obj);
array = new PropertyDescriptor[propertyDescriptorCollection.Count];
for (int i = 0; i < array.Length; i++)
{
array[i] = WrapProperty(propertyDescriptorCollection[i], obj);
}
}
this.properties = new PropertyDescriptorCollection(array);
}
return this.properties;
}
}
internal ArrayList SerializationProviders
{
get
{
if (this.designerSerializationProviders == null)
{
return [];
}
return this.designerSerializationProviders.Clone() as ArrayList;
}
}
/// <summary>Initializes a new instance of the <see cref="T:System.ComponentModel.Design.Serialization.DesignerSerializationManager" /> class.</summary>
public DesignerSerializationManager()
{
this.preserveNames = true;
this.validateRecycledTypes = true;
}
/// <summary>Initializes a new instance of the <see cref="T:System.ComponentModel.Design.Serialization.DesignerSerializationManager" /> class with the given service provider.</summary>
/// <param name="provider">An <see cref="T:System.IServiceProvider" />.</param>
/// <exception cref="T:System.ArgumentNullException">
/// <paramref name="provider" /> is null.</exception>
public DesignerSerializationManager(IServiceProvider provider)
{
this.serviceProvider = provider ?? throw new ArgumentNullException("provider");
this.preserveNames = true;
this.validateRecycledTypes = true;
}
private void CheckNoSession()
{
if (this.session != null)
{
throw new InvalidOperationException(SR.GetString("SerializationManagerWithinSession"));
}
}
private void CheckSession()
{
if (this.session == null)
{
throw new InvalidOperationException(SR.GetString("SerializationManagerNoSession"));
}
}
/// <summary>Creates an instance of a type.</summary>
/// <returns>A new instance of the type specified by <paramref name="type" />.</returns>
/// <param name="type">The type to create an instance of.</param>
/// <param name="arguments">The parameters of the type’s constructor. This can be null or an empty collection to invoke the default constructor.</param>
/// <param name="name">A name to give the object. If null, the object will not be given a name, unless the object is added to a container and the container gives the object a name.</param>
/// <param name="addToContainer">true to add the object to the container if the object implements <see cref="T:System.ComponentModel.IComponent" />; otherwise, false.</param>
/// <exception cref="T:System.Runtime.Serialization.SerializationException">
/// <paramref name="type" /> does not have a constructor that takes parameters contained in <paramref name="arguments" />.</exception>
protected virtual object CreateInstance(Type type, ICollection arguments, string name, bool addToContainer)
{
object[] array = null;
if (arguments != null && arguments.Count > 0)
{
array = new object[arguments.Count];
arguments.CopyTo(array, 0);
}
object obj = null;
if (this.RecycleInstances && name != null)
{
if (this.instancesByName != null)
{
obj = this.instancesByName[name];
}
if (obj == null && addToContainer && this.Container != null)
{
obj = this.Container.Components[name];
}
if (obj != null && this.ValidateRecycledTypes && obj.GetType() != type)
{
obj = null;
}
}
if (obj == null && addToContainer
&& typeof(IComponent).IsAssignableFrom(type)
&& (array == null || array.Length == 0 || (array.Length == 1 && array[0] == this.Container))
&& this.GetService(typeof(IDesignerHost)) is IDesignerHost designerHost && designerHost.Container == this.Container)
{
bool flag = false;
if (!this.PreserveNames && name != null && this.Container.Components[name] != null)
{
flag = true;
}
obj = (name == null || flag)
? designerHost.CreateComponent(type)
: designerHost.CreateComponent(type, name);
}
if (obj == null)
{
try
{
try
{
obj = TypeDescriptor.CreateInstance(this.serviceProvider, type, null, array);
}
catch (MissingMethodException)
{
if (array == null || array.Length == 0)
throw;
Type[] array2 = new Type[array.Length];
for (int i = 0; i < array.Length; i++)
{
if (array[i] != null)
{
array2[i] = array[i].GetType();
}
}
object[] array3 = new object[array.Length];
ConstructorInfo[] constructors = TypeDescriptor.GetReflectionType(type).GetConstructors(BindingFlags.Instance | BindingFlags.Public | BindingFlags.CreateInstance);
for (int j = 0; j < constructors.Length; j++)
{
ConstructorInfo constructorInfo = constructors[j];
ParameterInfo[] parameters = constructorInfo.GetParameters();
if (parameters != null && parameters.Length == array2.Length)
{
bool flag2 = true;
for (int k = 0; k < array2.Length; k++)
{
if (array2[k] != null && !parameters[k].ParameterType.IsAssignableFrom(array2[k]))
{
if (array[k] is IConvertible convertible)
{
try
{
array3[k] = convertible.ToType(parameters[k].ParameterType, null);
}
catch (Exception e) when (e is FormatException || e is InvalidCastException)
{
// Type conversion failed - this constructor parameter doesn't match.
flag2 = false;
break;
}
}
else
{
flag2 = false;
break;
}
}
else
{
array3[k] = array[k];
}
}
if (flag2)
{
obj = TypeDescriptor.CreateInstance(this.serviceProvider, type, null, array3);
break;
}
}
}
if (obj == null)
{
throw;
}
}
}
catch (MissingMethodException)
{
StringBuilder stringBuilder = new();
object[] array4 = array;
for (int l = 0; array4 != null && l < array4.Length; l++)
{
object obj2 = array4[l];
if (stringBuilder.Length > 0)
{
stringBuilder.Append(", ");
}
if (obj2 != null)
{
stringBuilder.Append(obj2.GetType().Name);
}
else
{
stringBuilder.Append("null");
}
}
throw new SerializationException(SR.GetString("SerializationManagerNoMatchingCtor",
[
type.FullName,
stringBuilder.ToString()
]))
{
HelpLink = "SerializationManagerNoMatchingCtor"
};
}
if (addToContainer && obj is IComponent component && this.Container != null)
{
bool flag3 = false;
if (!this.PreserveNames && name != null && this.Container.Components[name] != null)
{
flag3 = true;
}
if (name == null || flag3)
{
this.Container.Add(component);
}
else
{
this.Container.Add(component, name);
}
}
}
return obj;
}
/// <summary>Creates a new serialization session.</summary>
/// <returns>An <see cref="T:System.IDisposable" /> that represents a new serialization session.</returns>
/// <exception cref="T:System.InvalidOperationException">The serialization manager is already within a session. This version of <see cref="T:System.ComponentModel.Design.Serialization.DesignerSerializationManager" /> does not support simultaneous sessions.</exception>
public IDisposable CreateSession()
{
if (this.session != null)
{
throw new InvalidOperationException(SR.GetString("SerializationManagerAreadyInSession"));
}
this.session = new DesignerSerializationManager.SerializationSession(this);
this.OnSessionCreated(EventArgs.Empty);
return this.session;
}
/// <summary>Gets the serializer for the given object type.</summary>
/// <returns>The serializer for <paramref name="objectType" />, or null, if not found.</returns>
/// <param name="objectType">The type of object for which to retrieve the serializer.</param>
/// <param name="serializerType">The type of serializer to retrieve.</param>
/// <exception cref="T:System.ArgumentNullException">
/// <paramref name="objectType" /> or <paramref name="serializerType" /> is null.</exception>
public object GetSerializer(Type objectType, Type serializerType)
{
if (serializerType == null)
{
throw new ArgumentNullException("serializerType");
}
object obj = null;
if (objectType != null)
{
if (this.serializers != null)
{
obj = this.serializers[objectType];
if (obj != null && !serializerType.IsInstanceOfType(obj))
{
obj = null;
}
}
if (obj == null)
{
AttributeCollection attributes = TypeDescriptor.GetAttributes(objectType);
foreach (Attribute attribute in attributes)
{
if (attribute is DesignerSerializerAttribute designerSerializerAttribute)
{
string serializerBaseTypeName = designerSerializerAttribute.SerializerBaseTypeName;
if (serializerBaseTypeName != null)
{
Type runtimeType = this.GetRuntimeType(serializerBaseTypeName);
if (runtimeType == serializerType && designerSerializerAttribute.SerializerTypeName != null && designerSerializerAttribute.SerializerTypeName.Length > 0)
{
Type runtimeType2 = this.GetRuntimeType(designerSerializerAttribute.SerializerTypeName);
if (runtimeType2 != null)
{
obj = Activator.CreateInstance(runtimeType2, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.CreateInstance, null, null, null);// NOSONAR
break;
}
}
}
}
}
if (obj != null && this.session != null)
{
this.serializers ??= [];
this.serializers[objectType] = obj;
}
}
}
if (this.defaultProviderTable == null || !this.defaultProviderTable.ContainsKey(serializerType))
{
Type type = null;
DefaultSerializationProviderAttribute defaultSerializationProviderAttribute = (DefaultSerializationProviderAttribute)TypeDescriptor.GetAttributes(serializerType)[typeof(DefaultSerializationProviderAttribute)];
if (defaultSerializationProviderAttribute != null)
{
type = this.GetRuntimeType(defaultSerializationProviderAttribute.ProviderTypeName);
if (type != null && typeof(IDesignerSerializationProvider).IsAssignableFrom(type))
{
IDesignerSerializationProvider designerSerializationProvider = (IDesignerSerializationProvider)Activator.CreateInstance(type, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.CreateInstance, null, null, null);// NOSONAR
((IDesignerSerializationManager)this).AddSerializationProvider(designerSerializationProvider);
}
}
this.defaultProviderTable ??= [];
this.defaultProviderTable[serializerType] = type;
}
if (this.designerSerializationProviders != null)
{
bool flag = true;
int num = 0;
while (flag && num < this.designerSerializationProviders.Count)
{
flag = false;
foreach (IDesignerSerializationProvider designerSerializationProvider2 in this.designerSerializationProviders)
{
object serializer = designerSerializationProvider2.GetSerializer(this, obj, objectType, serializerType);
if (serializer != null)
{
flag = (obj != serializer);
obj = serializer;
}
}
num++;
}
}
return obj;
}
/// <summary>Gets the requested service.</summary>
/// <returns>The requested service, or null if the service cannot be resolved.</returns>
/// <param name="serviceType">The type of service to retrieve.</param>
protected virtual object GetService(Type serviceType)
{
if (serviceType == typeof(IContainer))
{
return this.Container;
}
if (this.serviceProvider != null)
{
return this.serviceProvider.GetService(serviceType);
}
return null;
}
/// <summary>Gets the requested type.</summary>
/// <returns>The requested type, or null if the type cannot be resolved.</returns>
/// <param name="typeName">The name of the type to retrieve.</param>
protected virtual Type GetType(string typeName)
{
Type type = this.GetRuntimeType(typeName);
if (type != null && this.GetService(typeof(TypeDescriptionProviderService)) is TypeDescriptionProviderService typeDescriptionProviderService)
{
TypeDescriptionProvider typeDescriptionProvider = typeDescriptionProviderService.GetProvider(type);
if (!typeDescriptionProvider.IsSupportedType(type))
{
type = null;
}
}
return type;
}
/// <summary>Gets the type corresponding to the specified type name.</summary>
/// <returns>The specified type.</returns>
/// <param name="typeName">The name of the type to get.</param>
public Type GetRuntimeType(string typeName)
{
if (this.typeResolver == null && !this.searchedTypeResolver)
{
this.typeResolver = (this.GetService(typeof(ITypeResolutionService)) as ITypeResolutionService);
this.searchedTypeResolver = true;
}
return this.typeResolver == null
? Type.GetType(typeName)
: this.typeResolver.GetType(typeName);
}
/// <summary>Raises the <see cref="E:System.ComponentModel.Design.Serialization.IDesignerSerializationManager.ResolveName" /> event. </summary>
/// <param name="e">A <see cref="T:System.ComponentModel.Design.Serialization.ResolveNameEventArgs" /> that contains the event data. </param>
protected virtual void OnResolveName(ResolveNameEventArgs e)
{
this.resolveNameEventHandler?.Invoke(this, e);
}
/// <summary>Raises the <see cref="E:System.ComponentModel.Design.Serialization.DesignerSerializationManager.SessionCreated" /> event. </summary>
/// <param name="e">An <see cref="T:System.EventArgs" /> that contains the event data. </param>
protected virtual void OnSessionCreated(EventArgs e)
{
this.sessionCreatedEventHandler?.Invoke(this, e);
}
/// <summary>Raises the <see cref="E:System.ComponentModel.Design.Serialization.DesignerSerializationManager.SessionDisposed" /> event. </summary>
/// <param name="e">An <see cref="T:System.EventArgs" /> that contains the event data.</param>
protected virtual void OnSessionDisposed(EventArgs e)
{
try
{
try
{
this.sessionDisposedEventHandler?.Invoke(this, e);
}
finally
{
this.serializationCompleteEventHandler?.Invoke(this, EventArgs.Empty);
}
}
finally
{
this.resolveNameEventHandler = null;
this.serializationCompleteEventHandler = null;
this.instancesByName = null;
this.namesByInstance = null;
this.serializers = null;
this.contextStack = null;
this.errorList = null;
this.session = null;
}
}
private static PropertyDescriptor WrapProperty(PropertyDescriptor property, object owner)
{
if (property == null)
{
throw new ArgumentNullException("property");
}
return new DesignerSerializationManager.WrappedPropertyDescriptor(property, owner);
}
/// <summary>Adds a custom serialization provider to the serialization manager.</summary>
/// <param name="provider">The serialization provider to add.</param>
void IDesignerSerializationManager.AddSerializationProvider(IDesignerSerializationProvider provider)
{
this.designerSerializationProviders ??= [];
if (!this.designerSerializationProviders.Contains(provider))
{
this.designerSerializationProviders.Add(provider);
}
}
/// <summary>Implements the <see cref="M:System.ComponentModel.Design.Serialization.IDesignerSerializationManager.CreateInstance(System.Type,System.Collections.ICollection,System.String,System.Boolean)" /> method.</summary>
/// <returns>The newly created object instance.</returns>
/// <param name="type">The data type to create. </param>
/// <param name="arguments">The arguments to pass to the constructor for this type. </param>
/// <param name="name">The name of the object. This name can be used to access the object later through <see cref="M:System.ComponentModel.Design.Serialization.IDesignerSerializationManager.GetInstance(System.String)" />. If null is passed, the object is still created but cannot be accessed by name. </param>
/// <param name="addToContainer">true to add this object to the design container. The object must implement <see cref="T:System.ComponentModel.IComponent" /> for this to have any effect. </param>
object IDesignerSerializationManager.CreateInstance(Type type, ICollection arguments, string name, bool addToContainer)
{
this.CheckSession();
if (name != null && this.instancesByName != null && this.instancesByName.ContainsKey(name))
{
throw new SerializationException(SR.GetString("SerializationManagerDuplicateComponentDecl",
[
name
]))
{
HelpLink = "SerializationManagerDuplicateComponentDecl"
};
}
object obj = this.CreateInstance(type, arguments, name, addToContainer);
if (name != null && (obj is not IComponent || !this.RecycleInstances))
{
if (this.instancesByName == null)
{
this.instancesByName = [];
this.namesByInstance = new Hashtable(new DesignerSerializationManager.ReferenceComparer());
}
this.instancesByName[name] = obj;
this.namesByInstance[obj] = name;
}
return obj;
}
/// <summary>Retrieves an instance of a created object of the specified name.</summary>
/// <returns>An instance of the object with the given name, or null if no object by that name can be found.</returns>
/// <param name="name">The name of the object to retrieve.</param>
/// <exception cref="T:System.ArgumentNullException">
/// <paramref name="name" /> is null.</exception>
/// <exception cref="T:System.InvalidOperationException">This property was accessed outside of a serialization session.</exception>
object IDesignerSerializationManager.GetInstance(string name)
{
object obj = null;
if (name == null)
{
throw new ArgumentNullException("name");
}
this.CheckSession();
if (this.instancesByName != null)
{
obj = this.instancesByName[name];
}
if (obj == null && this.PreserveNames && this.Container != null)
{
obj = this.Container.Components[name];
}
if (obj == null)
{
ResolveNameEventArgs resolveNameEventArgs = new(name);
this.OnResolveName(resolveNameEventArgs);
obj = resolveNameEventArgs.Value;
}
return obj;
}
/// <summary>Retrieves a name for the specified object.</summary>
/// <returns>The name of the object, or null if the object is unnamed.</returns>
/// <param name="value">The object for which to retrieve the name.</param>
/// <exception cref="T:System.ArgumentNullException">
/// <paramref name="value" /> is null.</exception>
/// <exception cref="T:System.InvalidOperationException">This property was accessed outside of a serialization session.</exception>
string IDesignerSerializationManager.GetName(object value)
{
string text = null;
if (value == null)
{
throw new ArgumentNullException("value");
}
this.CheckSession();
if (this.namesByInstance != null)
{
text = (string)this.namesByInstance[value];
}
if (text == null && value is IComponent component)
{
ISite site = component.Site;
if (site != null)
{
text = site is INestedSite nestedSite
? nestedSite.FullName
: site.Name;
}
}
return text;
}
/// <summary>Gets a serializer of the requested type for the specified object type.</summary>
/// <returns>An instance of the requested serializer, or null if no appropriate serializer can be located.</returns>
/// <param name="objectType">The type of the object to get the serializer for.</param>
/// <param name="serializerType">The type of the serializer to retrieve.</param>
object IDesignerSerializationManager.GetSerializer(Type objectType, Type serializerType)
{
return this.GetSerializer(objectType, serializerType);
}
/// <summary>Gets a type of the specified name.</summary>
/// <returns>An instance of the type, or null if the type cannot be loaded.</returns>
/// <param name="typeName">The fully qualified name of the type to load.</param>
/// <exception cref="T:System.InvalidOperationException">This property was accessed outside of a serialization session.</exception>
Type IDesignerSerializationManager.GetType(string typeName)
{
this.CheckSession();
Type type = null;
while (type == null)
{
type = this.GetType(typeName);
if (type == null && typeName != null && typeName.Length > 0)
{
int num = typeName.LastIndexOf('.');
if (num == -1 || num == typeName.Length - 1)
{
break;
}
typeName = $"{typeName.Substring(0, num)}{typeName.Substring(num + 1, typeName.Length - num - 1)}";
}
}
return type;
}
/// <summary>Removes a previously added serialization provider.</summary>
/// <param name="provider">The <see cref="T:System.ComponentModel.Design.Serialization.IDesignerSerializationProvider" /> to remove.</param>
void IDesignerSerializationManager.RemoveSerializationProvider(IDesignerSerializationProvider provider)
{
this.designerSerializationProviders?.Remove(provider);
}
/// <summary>Used to report a recoverable error in serialization.</summary>
/// <param name="errorInformation">An object containing the error information, usually of type <see cref="T:System.String" /> or <see cref="T:System.Exception" />.</param>
/// <exception cref="T:System.InvalidOperationException">This property was accessed outside of a serialization session.</exception>
void IDesignerSerializationManager.ReportError(object errorInformation)
{
this.CheckSession();
if (errorInformation != null)
{
this.Errors.Add(errorInformation);
}
}
/// <summary>Sets the name for the specified object.</summary>
/// <param name="instance">The object to set the name.</param>
/// <param name="name">A <see cref="T:System.String" /> used as the name of the object.</param>
/// <exception cref="T:System.ArgumentNullException">One or both of the parameters are null.</exception>
/// <exception cref="T:System.ArgumentException">The object specified by instance already has a name, or <paramref name="name" /> is already used by another named object.</exception>
/// <exception cref="T:System.InvalidOperationException">This property was accessed outside of a serialization session.</exception>
void IDesignerSerializationManager.SetName(object instance, string name)
{
this.CheckSession();
if (instance == null)
{
throw new ArgumentNullException("instance");
}
if (name == null)
{
throw new ArgumentNullException("name");
}
if (this.instancesByName == null)
{
this.instancesByName = [];
this.namesByInstance = new Hashtable(new DesignerSerializationManager.ReferenceComparer());
}
if (this.instancesByName[name] != null)
{
throw new ArgumentException(SR.GetString("SerializationManagerNameInUse",
[
name
]));
}
if (this.namesByInstance[instance] != null)
{
throw new ArgumentException(SR.GetString("SerializationManagerObjectHasName",
[
name,
(string)this.namesByInstance[instance]
]));
}
this.instancesByName[name] = instance;
this.namesByInstance[instance] = name;
}
/// <summary>For a description of this member, see the <see cref="M:System.IServiceProvider.GetService(System.Type)" /> method.</summary>
/// <returns>A service object of type <paramref name="serviceType" />.-or-null if there is no service object of type <paramref name="serviceType" />.</returns>
/// <param name="serviceType">An object that specifies the type of service object to get.</param>
object IServiceProvider.GetService(Type serviceType)
{
return this.GetService(serviceType);
}
}
}