[reflection] Coop handles icalls in System.Reflection and System.RuntimeTypeHandle...
[mono.git] / mcs / class / referencesource / System.Activities.Presentation / System.Activities.Presentation / System / Activities / Presentation / Base / Core / Internal / PropertyEditing / State / PropertyState.cs
1 //----------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation.  All rights reserved.
3 //----------------------------------------------------------------
4 namespace System.Activities.Presentation.Internal.PropertyEditing.State 
5 {
6     using System;
7     using System.Diagnostics;
8     using System.Diagnostics.CodeAnalysis;
9     using System.Runtime;
10
11     // <summary>
12     // Simple state object that knows how to remember state for a single property.
13     // The only thing it currently remembers is whether the property's sub-properties
14     // are expanded or collapsed.
15     // </summary>
16     internal class PropertyState : PersistedState 
17     {
18
19         private const bool DefaultSubPropertiesExpanded = false;
20
21         private string _propertyName;
22         private bool _subPropertiesExpanded = DefaultSubPropertiesExpanded;
23
24         // <summary>
25         // Basic ctor
26         // </summary>
27         // <param name="propertyName">Property to wrap around</param>
28         [SuppressMessage("Microsoft.Performance", "CA1805:DoNotInitializeUnnecessarily")]
29         public PropertyState(string propertyName) 
30         {
31             Fx.Assert(!string.IsNullOrEmpty(propertyName), "Expected a full property name");
32             _propertyName = propertyName;
33         }
34
35         // <summary>
36         // We key these state objects by their property names
37         // </summary>
38         public override object Key 
39         {
40             get { return _propertyName; }
41         }
42
43         // <summary>
44         // Returns true if any of the contained values differ from the default
45         // </summary>
46         public override bool IsSignificant 
47         {
48             get { return _subPropertiesExpanded != DefaultSubPropertiesExpanded; }
49         }
50
51         // <summary>
52         // Gets or sets a flag indicating whether the sub-properties of the contained
53         // property have been expanded or collapsed.
54         // </summary>
55         public bool SubPropertiesExpanded 
56         {
57             get { return _subPropertiesExpanded; }
58             set { _subPropertiesExpanded = value; }
59         }
60
61         // <summary>
62         // Serializes this object into a simple string (AppDomains like strings).
63         //
64         // Format: PropertyName,SubPropertiesExpanded;NextPropertyName,SubPropertiesExpanded;...
65         // Where bools are recorded as 0 = false and 1 = true
66         // </summary>
67         // <returns>Serialized version of this state object (may be null)</returns>
68         protected override string SerializeCore() 
69         {
70             return string.Concat(
71                 PersistedStateUtilities.Escape(_propertyName),
72                 ',',
73                 PersistedStateUtilities.BoolToDigit(_subPropertiesExpanded));
74         }
75
76         // <summary>
77         // Attempts to deserialize a string into a PropertyState object
78         // </summary>
79         // <param name="propertyStateString">String to deserialize</param>
80         // <returns>Instance of PropertyState if the serialized string was valid, null otherwise.</returns>
81         public static PropertyState Deserialize(string propertyStateString) 
82         {
83             string[] args = propertyStateString.Split(',');
84             if (args == null || args.Length != 2)
85             {
86                 return null;
87             }
88
89             bool? subPropertiesExpanded = PersistedStateUtilities.DigitToBool(args[1]);
90             if (subPropertiesExpanded == null)
91             {
92                 return null;
93             }
94
95             string propertyName = PersistedStateUtilities.Unescape(args[0]);
96             if (string.IsNullOrEmpty(propertyName))
97             {
98                 return null;
99             }
100
101             PropertyState propertyState = new PropertyState(propertyName);
102             propertyState.SubPropertiesExpanded = (bool)subPropertiesExpanded;
103             return propertyState;
104         }
105     }
106 }