[amd64/tramp] hide interpreter specific trampoline behind ifdef
[mono.git] / mcs / class / referencesource / System.Activities.Presentation / System.Activities.Presentation / System / Activities / Presentation / Base / Core / PropertyEditing / newitemtypesattribute.cs
1 namespace System.Activities.Presentation.PropertyEditing
2 {
3     using System;
4     using System.Collections.Generic;
5     using System.Activities.Presentation.Internal;
6     using System.Activities.Presentation.Internal.Properties;
7     using System.Runtime;
8     using System.Diagnostics.CodeAnalysis;
9
10     /// <summary>
11     /// This attributes is used to specify which object Types can be assigned as the value of a given
12     /// property or as the value of a given property type.  If the property represents a collection,
13     /// this attribute specifies the object Types of which instances can be created as the items
14     /// of that collection.
15     /// </summary>
16     [Fx.Tag.XamlVisible(false)]
17     [AttributeUsage(AttributeTargets.Property | AttributeTargets.Class, AllowMultiple = true)]
18     sealed class NewItemTypesAttribute : Attribute
19     {
20
21         private Type _factoryType;
22         private Type[] _types;
23
24         /// <summary>
25         /// Constructor for the attribute
26         /// </summary>
27         /// <param name="type">Type that this attribute declares as being valid new item Type.</param>
28         /// <exception cref="ArgumentNullException">If type is null.</exception>
29         public NewItemTypesAttribute(Type type)
30         {
31             if (type == null)
32                 throw FxTrace.Exception.ArgumentNull("type");
33
34             _factoryType = typeof(NewItemFactory);
35             _types = new Type[] { type };
36         }
37
38         /// <summary>
39         /// Constructor for the attribute
40         /// </summary>
41         /// <param name="types">List of types that this attribute declares as being
42         /// valid new item Types.</param>
43         /// <exception cref="ArgumentNullException">If types is null or emtpy.</exception>
44         public NewItemTypesAttribute(params Type[] types)
45         {
46             if (types == null || types.Length < 1)
47                 throw FxTrace.Exception.ArgumentNull("types");
48
49             _factoryType = typeof(NewItemFactory);
50             _types = types;
51         }
52
53         /// <summary>
54         /// Gets a list of Types that this attribute declares as being valid new item Types.
55         /// Guaranteed to be non-null.
56         /// </summary>
57         [SuppressMessage("Microsoft.Naming", "CA1721:PropertyNamesShouldNotMatchGetMethods", Justification = "Suppress to avoid unnecessary change.")]
58         public Type Type
59         {
60             get
61             {
62                 return _types[0];
63             }
64         }
65
66         /// <summary>
67         /// Gets a list of Types that this attribute declares as being valid new item Types.
68         /// Guaranteed to be non-null.
69         /// </summary>
70         public IEnumerable<Type> Types
71         {
72             get
73             {
74                 return _types;
75             }
76         }
77
78         /// <summary>
79         /// Gets or sets the factory Type associated with this attribute.  The Type is
80         /// guaranteed to derive from NewItemFactory.
81         /// </summary>
82         /// <exception cref="ArgumentException">If type does not derive from NewItemFactory</exception>
83         /// <exception cref="ArgumentNullException">If type is null.</exception>
84         public Type FactoryType
85         {
86             get
87             {
88                 return _factoryType;
89             }
90             set
91             {
92                 if (value == null)
93                     throw FxTrace.Exception.ArgumentNull("value");
94                 if (!typeof(NewItemFactory).IsAssignableFrom(value))
95                     throw FxTrace.Exception.AsError(new ArgumentException(Resources.Error_InvalidFactoryType));
96
97                 _factoryType = value;
98             }
99         }
100
101         /// <summary>
102         /// Gets the TypeId for this attribute.  Returns an equality array unique to this attribute
103         /// type and the contained factory type.  The order in which the type attributes are passed
104         /// into the constructor of this class (if there are more than one) matters and is used in 
105         /// determining the equality of two NewItemTypesAttribute instances.
106         /// </summary>
107         public override object TypeId
108         {
109             get
110             {
111                 object[] typeId = new object[_types.Length + 2];
112                 for (int i = 0; i < _types.Length; i++)
113                 {
114                     typeId[i + 2] = _types[i];
115                 }
116                 typeId[0] = typeof(NewItemTypesAttribute);
117                 typeId[1] = _factoryType;
118                 return new EqualityArray(typeId);
119             }
120         }
121     }
122 }