Fix bugs in sizing TableLayoutPanel (Xamarin bug 18638)
[mono.git] / mcs / class / System.ComponentModel.Composition / src / ComponentModel / Microsoft / Internal / Collections / CollectionServices.CollectionOfObject.cs
1 // -----------------------------------------------------------------------\r
2 // Copyright (c) Microsoft Corporation.  All rights reserved.\r
3 // -----------------------------------------------------------------------\r
4 using System;\r
5 using System.Collections;\r
6 using System.Collections.Generic;\r
7 using System.Reflection;\r
8 \r
9 namespace Microsoft.Internal.Collections\r
10 {\r
11     internal static partial class CollectionServices\r
12     {\r
13         public static ICollection<object> GetCollectionWrapper(Type itemType, object collectionObject)\r
14         {\r
15             Assumes.NotNull(itemType, collectionObject);\r
16 \r
17             if (itemType == typeof(object))\r
18             {\r
19                 return (ICollection<object>)collectionObject;\r
20             }\r
21 \r
22             // Most common .Net collections implement IList as well so for those\r
23             // cases we can optimize the wrapping instead of using reflection to create\r
24             // a generic type.\r
25             if (typeof(IList).IsAssignableFrom(collectionObject.GetType()))\r
26             {\r
27                 return new CollectionOfObjectList((IList)collectionObject);\r
28             }\r
29 \r
30             Type collectionType = typeof(CollectionOfObject<>).MakeGenericType(itemType);\r
31 \r
32             return (ICollection<object>)Activator.CreateInstance(collectionType, collectionObject);           \r
33         }\r
34 \r
35         private class CollectionOfObjectList : ICollection<object>\r
36         {\r
37             private readonly IList _list;\r
38 \r
39             public CollectionOfObjectList(IList list)\r
40             {\r
41                 this._list = list;\r
42             }\r
43 \r
44             public void Add(object item)\r
45             {\r
46                 this._list.Add(item);\r
47             }\r
48 \r
49             public void Clear()\r
50             {\r
51                 this._list.Clear();\r
52             }\r
53 \r
54             public bool Contains(object item)\r
55             {\r
56                 return Assumes.NotReachable<bool>();\r
57             }\r
58 \r
59             public void CopyTo(object[] array, int arrayIndex)\r
60             {\r
61                 Assumes.NotReachable<object>();\r
62             }\r
63 \r
64             public int Count\r
65             {\r
66                 get { return Assumes.NotReachable<int>(); }\r
67             }\r
68 \r
69             public bool IsReadOnly\r
70             {\r
71                 get { return this._list.IsReadOnly; }\r
72             }\r
73 \r
74             public bool Remove(object item)\r
75             {\r
76                 return Assumes.NotReachable<bool>();\r
77             }\r
78 \r
79             public IEnumerator<object> GetEnumerator()\r
80             {\r
81                 return Assumes.NotReachable<IEnumerator<object>>();\r
82             }\r
83 \r
84             IEnumerator IEnumerable.GetEnumerator()\r
85             {\r
86                 return Assumes.NotReachable<IEnumerator>();\r
87             }\r
88         }\r
89 \r
90         private class CollectionOfObject<T> : ICollection<object>\r
91         {\r
92             private readonly ICollection<T> _collectionOfT;\r
93 \r
94             public CollectionOfObject(object collectionOfT)\r
95             {\r
96                 this._collectionOfT = (ICollection<T>)collectionOfT;\r
97             }\r
98 \r
99             public void Add(object item)\r
100             {\r
101                 this._collectionOfT.Add((T) item);\r
102             }\r
103 \r
104             public void Clear()\r
105             {\r
106                 this._collectionOfT.Clear();\r
107             }\r
108 \r
109             public bool Contains(object item)\r
110             {\r
111                 return Assumes.NotReachable<bool>();\r
112             }\r
113 \r
114             public void CopyTo(object[] array, int arrayIndex)\r
115             {\r
116                 Assumes.NotReachable<object>();\r
117             }\r
118 \r
119             public int Count\r
120             {\r
121                 get { return Assumes.NotReachable<int>(); }\r
122             }\r
123 \r
124             public bool IsReadOnly\r
125             {\r
126                 get { return this._collectionOfT.IsReadOnly; }\r
127             }\r
128 \r
129             public bool Remove(object item)\r
130             {\r
131                 return Assumes.NotReachable<bool>();\r
132             }\r
133 \r
134             public IEnumerator<object> GetEnumerator()\r
135             {\r
136                 return Assumes.NotReachable<IEnumerator<object>>();\r
137             }\r
138 \r
139             IEnumerator IEnumerable.GetEnumerator()\r
140             {\r
141                 return Assumes.NotReachable<IEnumerator>();\r
142             }\r
143         }\r
144     }\r
145 }\r