cee86e0dc11e15115dab53701e7abfa33dd4d4a8
[mono.git] / mcs / class / referencesource / System.Activities.Presentation / System.Activities.Presentation / System / Activities / Presentation / EditorOptionAttribute.cs
1 //----------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation.  All rights reserved.
3 //----------------------------------------------------------------
4
5 namespace System.Activities.Presentation.PropertyEditing
6 {
7     using System.Runtime;
8     using System.Collections;
9
10     [Fx.Tag.XamlVisible(false)]
11     [AttributeUsage(AttributeTargets.Property, AllowMultiple = true)]
12     public sealed class EditorOptionAttribute : Attribute
13     {
14         public string Name { get; set; }
15         public object Value { get; set; }
16         //TypeId is needed so that multiple EditorOptionsAttribute could be added to the same property
17         public override object TypeId
18         {
19             get
20             {
21                 return new EditorOptionsAttributeTypeId
22                 {
23                     BaseTypeId = base.TypeId,
24                     Name = this.Name,
25                     Value = this.Value
26                 };
27             }
28         }
29
30         public static bool TryGetOptionValue(IEnumerable attributes, string optionName, out object optionValue)
31         {            
32             foreach (Attribute attribute in attributes)
33             {
34                 EditorOptionAttribute optionAttribute = attribute as EditorOptionAttribute;
35                 if (optionAttribute != null && optionAttribute.Name.Equals(optionName))
36                 {
37                     optionValue = optionAttribute.Value;
38                     return true;
39                 }
40             }
41             optionValue = null;
42             return false;
43         }
44
45         //A class to uniquely identify a name-value pair
46         class EditorOptionsAttributeTypeId
47         {
48             public object BaseTypeId { get; set; }
49             public string Name { get; set; }
50             public object Value { get; set; }
51
52             public override bool Equals(object obj)
53             {
54                 EditorOptionsAttributeTypeId that = obj as EditorOptionsAttributeTypeId;
55                 if (that == null) return false;
56                 return this.BaseTypeId == that.BaseTypeId &&
57                     string.Equals(this.Name, that.Name) &&
58                     object.Equals(this.Value, that.Value);
59             }
60
61             public override int GetHashCode()
62             {
63                 return
64                     (BaseTypeId == null ? 0 : BaseTypeId.GetHashCode()) ^
65                     (Name == null ? 0 : Name.GetHashCode()) ^
66                     (Value == null ? 0 : Value.GetHashCode());
67             }
68         }
69     }
70 }