/
SchemaNameCollection.cs
237 lines (202 loc) · 7.56 KB
/
SchemaNameCollection.cs
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections;
namespace System.DirectoryServices
{
/// <devdoc>
/// Contains a list of schema names used for the <see cref='System.DirectoryServices.DirectoryEntries.SchemaFilter'/> property of a <see cref='System.DirectoryServices.DirectoryEntries'/>.
/// </devdoc>
public class SchemaNameCollection : IList
{
private readonly VariantPropGetter _propGetter;
private readonly VariantPropSetter _propSetter;
internal SchemaNameCollection(VariantPropGetter propGetter, VariantPropSetter propSetter)
{
_propGetter = propGetter;
_propSetter = propSetter;
}
/// <devdoc>
/// Gets or sets the object at the given index.
/// </devdoc>
public string? this[int index]
{
get
{
object?[] values = GetValue();
return (string?)values[index];
}
set
{
object?[] values = GetValue();
values[index] = value;
_propSetter(values);
}
}
/// <devdoc>
/// Gets the number of objects available on this entry.
/// </devdoc>
public int Count
{
get
{
object?[] values = GetValue();
return values.Length;
}
}
/// <devdoc>
/// Appends the value to the collection.
/// </devdoc>
public int Add(string? value)
{
object?[] oldValues = GetValue();
object?[] newValues = new object[oldValues.Length + 1];
for (int i = 0; i < oldValues.Length; i++)
newValues[i] = oldValues[i];
newValues[newValues.Length - 1] = value;
_propSetter(newValues);
return newValues.Length - 1;
}
/// <devdoc>
/// Appends the values to the collection.
/// </devdoc>
public void AddRange(string?[] value)
{
if (value == null)
{
throw new ArgumentNullException(nameof(value));
}
object?[] oldValues = GetValue();
object?[] newValues = new object[oldValues.Length + value.Length];
for (int i = 0; i < oldValues.Length; i++)
newValues[i] = oldValues[i];
for (int i = oldValues.Length; i < newValues.Length; i++)
newValues[i] = value[i - oldValues.Length];
_propSetter(newValues);
}
public void AddRange(SchemaNameCollection value)
{
if (value == null)
{
throw new ArgumentNullException(nameof(value));
}
object?[] oldValues = GetValue();
object?[] newValues = new object?[oldValues.Length + value.Count];
for (int i = 0; i < oldValues.Length; i++)
newValues[i] = oldValues[i];
for (int i = oldValues.Length; i < newValues.Length; i++)
newValues[i] = value[i - oldValues.Length];
_propSetter(newValues);
}
/// <devdoc>
/// Removes all items from the collection.
/// </devdoc>
public void Clear()
{
_propSetter(Array.Empty<object>());
}
/// <devdoc>
/// Determines if the collection contains a specific value.
/// </devdoc>
public bool Contains(string? value) => IndexOf(value) != -1;
public void CopyTo(string?[] stringArray, int index)
{
object?[] values = GetValue();
values.CopyTo(stringArray, index);
}
public IEnumerator GetEnumerator()
{
object?[] values = GetValue();
return values.GetEnumerator();
}
private object?[] GetValue()
{
object? value = _propGetter();
if (value == null)
return Array.Empty<object>();
else
return (object?[])value;
}
/// <devdoc>
/// Determines the index of a specific item in the collection.
/// </devdoc>
public int IndexOf(string? value)
{
object?[] values = GetValue();
for (int i = 0; i < values.Length; i++)
{
if (value == (string?)values[i])
return i;
}
return -1;
}
/// <devdoc>
/// Inserts an item at the specified position in the collection.
/// </devdoc>
public void Insert(int index, string? value)
{
ArrayList tmpList = new ArrayList((ICollection)GetValue());
tmpList.Insert(index, value);
_propSetter(tmpList.ToArray());
}
/// <devdoc>
/// Removes an item from the collection.
/// </devdoc>
public void Remove(string? value)
{
// this does take two scans of the array, but value isn't guaranteed to be there.
int index = IndexOf(value);
RemoveAt(index);
}
/// <devdoc>
/// Removes the item at the specified index from the collection.
/// </devdoc>
public void RemoveAt(int index)
{
object?[] oldValues = GetValue();
if (index >= oldValues.Length || index < 0)
throw new ArgumentOutOfRangeException(nameof(index));
object?[] newValues = new object[oldValues.Length - 1];
for (int i = 0; i < index; i++)
newValues[i] = oldValues[i];
for (int i = index + 1; i < oldValues.Length; i++)
newValues[i - 1] = oldValues[i];
_propSetter(newValues);
}
bool IList.IsReadOnly => false;
bool IList.IsFixedSize => false;
void ICollection.CopyTo(Array array, int index)
{
object?[] values = GetValue();
values.CopyTo(array, index);
}
bool ICollection.IsSynchronized => false;
object ICollection.SyncRoot => this;
object? IList.this[int index]
{
get => this[index];
set => this[index] = (string?)value;
}
int IList.Add(object? value) => Add((string?)value);
bool IList.Contains(object? value) => Contains((string?)value);
int IList.IndexOf(object? value) => IndexOf((string?)value);
void IList.Insert(int index, object? value) => Insert(index, (string?)value);
void IList.Remove(object? value) => Remove((string?)value);
internal delegate object? VariantPropGetter();
internal delegate void VariantPropSetter(object? value);
// this class and HintsDelegateWrapper exist only because you can't create
// a delegate to a property's accessors. You have to supply methods. So these
// classes wrap an object and supply properties as methods.
internal sealed class FilterDelegateWrapper
{
private readonly UnsafeNativeMethods.IAdsContainer _obj;
internal FilterDelegateWrapper(UnsafeNativeMethods.IAdsContainer wrapped)
{
_obj = wrapped;
}
public VariantPropGetter Getter => new VariantPropGetter(GetFilter);
public VariantPropSetter Setter => new VariantPropSetter(SetFilter);
private object? GetFilter() => _obj.Filter;
private void SetFilter(object? value) => _obj.Filter = value;
}
}
}