public void RemoveAtTests(int index) { var propertyDescriptors = new PropertyDescriptor[] { new MockPropertyDescriptor("propertyDescriptor1"), new MockPropertyDescriptor("propertyDescriptor2"), new MockPropertyDescriptor("propertyDescriptor3"), new MockPropertyDescriptor("propertyDescriptor4"), new MockPropertyDescriptor("propertyDescriptor5"), new MockPropertyDescriptor("propertyDescriptor6"), new MockPropertyDescriptor("propertyDescriptor7"), new MockPropertyDescriptor("propertyDescriptor8"), new MockPropertyDescriptor("propertyDescriptor9") }; // Must send in a copy to the constructor as the array itself is manipulated var collection = new PropertyDescriptorCollection(propertyDescriptors.ToArray()); Assert.True(index >= 0 && index < propertyDescriptors.Length, $"Index '{index}' is out of bounds"); collection.RemoveAt(index); for (int i = 0; i < propertyDescriptors.Length; i++) { if (i == index) { Assert.False(collection.Contains(propertyDescriptors[index]), "Should have removed descriptor"); } else { Assert.True(collection.Contains(propertyDescriptors[i]), $"Descriptor should be in collection: {i}"); } } }
public override PropertyDescriptorCollection GetItemProperties(PropertyDescriptor[] listAccessors) { PropertyDescriptorCollection descriptorCollection = base.GetItemProperties(listAccessors); bool locationColumnsAdded = false; for (int i = 0; i < descriptorCollection.Count; i++) { var descriptor = descriptorCollection[i]; if (locationColumnsAdded) { ((FunctionBindingListPropertyDescriptor)descriptor).index += 1; continue; } if (descriptor.PropertyType == typeof(INetworkLocation)) { descriptorCollection.RemoveAt(i); //remove network location, add two entries instead descriptorCollection.Insert(i, new FunctionBindingListPropertyDescriptor(ColumnNameBranch, DisplayNameBranch, typeof(IBranch), i)); i++; descriptorCollection.Insert(i, new FunctionBindingListPropertyDescriptor(ColumnNameOffset, DisplayNameOffset, typeof(double), i)); locationColumnsAdded = true; } } return(descriptorCollection); }
/// <summary> /// This is overridden to return a property descriptors for the object represented by this type /// descriptor along with extra property descriptors for its child properties. /// </summary> /// <param name="attributes">An array of attributes to use as a filter or null for no filter</param> /// <returns>Returns a <see cref="PropertyDescriptorCollection"/> that contains property descriptors for /// the object and its child properties.</returns> public override PropertyDescriptorCollection GetProperties(Attribute[] attributes) { // This seems to ignore the filter so GetChildProperties() will get rid of all non-browsable // properties. PropertyDescriptorCollection props = base.GetProperties(attributes); List <PropertyDescriptor> newProps = new List <PropertyDescriptor>(); this.GetChildProperties(null, String.Empty, attributes, props, newProps); if (newProps.Count != 0) { // The collection is read-only so we'll need to create a new one PropertyDescriptor[] tempProps = new PropertyDescriptor[props.Count + newProps.Count]; props.CopyTo(tempProps, 0); newProps.CopyTo(tempProps, props.Count); props = new PropertyDescriptorCollection(tempProps); // Now we'll remove hidden top-level properties for (int idx = 0; idx < props.Count; idx++) { if (props[idx].Attributes[typeof(HidePropertyAttribute)] != null) { props.RemoveAt(idx); idx--; } } } return(props); }
public void ReadOnlyThrows() { var collection = new PropertyDescriptorCollection(null, true); // The readonly check occurs before anything else, so we don't need to pass in actual valid values Assert.Throws <NotSupportedException>(() => collection.Add(null)); Assert.Throws <NotSupportedException>(() => collection.Insert(1, null)); Assert.Throws <NotSupportedException>(() => collection.RemoveAt(1)); Assert.Throws <NotSupportedException>(() => collection.Remove(null)); Assert.Throws <NotSupportedException>(() => collection.Clear()); }
public void ReadOnlyThrows() { var collection = new PropertyDescriptorCollection(null, true); // The readonly check occurs before anything else, so we don't need to pass in actual valid values Assert.Throws<NotSupportedException>(() => collection.Add(null)); Assert.Throws<NotSupportedException>(() => collection.Insert(1, null)); Assert.Throws<NotSupportedException>(() => collection.RemoveAt(1)); Assert.Throws<NotSupportedException>(() => collection.Remove(null)); Assert.Throws<NotSupportedException>(() => collection.Clear()); }
public void collection_cannot_be_mutated() { var property0 = PropertyDescriptor.For(typeof(DecoratedType).GetProperty("Property0")); var property1 = PropertyDescriptor.For(typeof(DecoratedType).GetProperty("Property1")); IList<PropertyDescriptor> collection = new PropertyDescriptorCollection(new[] { property0, property1 }); Assert.Throws<NotSupportedException>(() => collection.Add(property1)); Assert.Throws<NotSupportedException>(() => collection.Clear()); Assert.Throws<NotSupportedException>(() => collection.Insert(0, property1)); Assert.Throws<NotSupportedException>(() => collection.Remove(property0)); Assert.Throws<NotSupportedException>(() => collection.RemoveAt(0)); }
public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes) { PropertyDescriptorCollection properties = base.GetProperties(context, value, attributes); for (var i = 0; i < properties.Count; i++) { if (!Fields.Contains(properties[i].Name)) { properties.RemoveAt(i--); } } return(properties); }
private PropertyDescriptorCollection FilterProperties(PropertyDescriptorCollection pdc, ConnectionType ct, bool singleProxy) { var propsToKeep = new List <string>(new[] { ConnType }); var readOnlyProps = new List <string>(new[] { FtpProxy, HttpsProxy, SocksProxy }); switch (ct) { case ConnectionType.AutoConfig: propsToKeep.Add(AutoConfigUrl); break; case ConnectionType.ManualProxy: if (!singleProxy) { propsToKeep.Add(FtpProxy); propsToKeep.Add(HttpsProxy); propsToKeep.Add(SocksProxy); } propsToKeep.Add(HttpProxy); propsToKeep.Add(UseSingleProxy); break; default: break; } for (int i = pdc.Count - 1; i >= 0; i--) { if (!propsToKeep.Contains(pdc[i].Name)) { pdc.RemoveAt(i); continue; } // // http://www.codeproject.com/Articles/152945/Enabling-disabling-properties-at-runtime-in-the-Pr //if (ct == ConnectionType.ManualProxy) //{ // var pd = (PropertyDescriptor)pdc[i]; // var attrib = (ReadOnlyAttribute)pd.Attributes[typeof(ReadOnlyAttribute)]; // var field = attrib.GetType().GetField("isReadOnly", // BindingFlags.NonPublic | BindingFlags.Instance); // field.SetValue(attrib, !(readOnlyProps.Contains(pd.Name) && singleProxy)); //} } return(pdc); }
public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes) { if (context != null && context.Instance != null) { PropertyDescriptorCollection properties = base.GetProperties(context, value, attributes); for (int i = 0; i < properties.Count; i++) { if (properties[i].Name != "X" && properties[i].Name != "Y" && properties[i].Name != "Width" && properties[i].Name != "Height") { properties.RemoveAt(i); i--; } } return(properties); } return(base.GetProperties(context, value, attributes)); }
private void AssertReadOnly(PropertyDescriptorCollection descriptors, string testCase) { MockPropertyDescriptor mockPropertyDescr = new MockPropertyDescriptor( "Date", DateTime.Now); try { descriptors.Add(mockPropertyDescr); Assert.Fail(testCase + "#1"); } catch (NotSupportedException) { // read-only collection cannot be modified } // ensure read-only check if performed before value is checked try { descriptors.Add(null); Assert.Fail(testCase + "#2"); } catch (NotSupportedException) { // read-only collection cannot be modified } try { descriptors.Clear(); Assert.Fail(testCase + "#3"); } catch (NotSupportedException) { // read-only collection cannot be modified } try { descriptors.Insert(0, mockPropertyDescr); Assert.Fail(testCase + "#4"); } catch (NotSupportedException) { // read-only collection cannot be modified } // ensure read-only check if performed before value is checked try { descriptors.Insert(0, null); Assert.Fail(testCase + "#5"); } catch (NotSupportedException) { // read-only collection cannot be modified } try { descriptors.Remove(mockPropertyDescr); Assert.Fail(testCase + "#6"); } catch (NotSupportedException) { // read-only collection cannot be modified } // ensure read-only check if performed before value is checked try { descriptors.Remove(null); Assert.Fail(testCase + "#7"); } catch (NotSupportedException) { // read-only collection cannot be modified } try { descriptors.RemoveAt(0); Assert.Fail(testCase + "#8"); } catch (NotSupportedException) { // read-only collection cannot be modified } IList list = (IList)descriptors; Assert.IsTrue(((IList)descriptors).IsReadOnly, testCase + "#9"); Assert.IsTrue(((IList)descriptors).IsFixedSize, testCase + "#10"); try { list.Add(mockPropertyDescr); Assert.Fail(testCase + "#11"); } catch (NotSupportedException) { // read-only collection cannot be modified } // ensure read-only check if performed before value is checked try { list.Add(null); Assert.Fail(testCase + "#12"); } catch (NotSupportedException) { // read-only collection cannot be modified } try { list.Clear(); Assert.Fail(testCase + "#13"); } catch (NotSupportedException) { // read-only collection cannot be modified } try { list.Insert(0, mockPropertyDescr); Assert.Fail(testCase + "#14"); } catch (NotSupportedException) { // read-only collection cannot be modified } // ensure read-only check if performed before value is checked try { list.Insert(0, null); Assert.Fail(testCase + "#15"); } catch (NotSupportedException) { // read-only collection cannot be modified } try { list.Remove(mockPropertyDescr); Assert.Fail(testCase + "#16"); } catch (NotSupportedException) { // read-only collection cannot be modified } // ensure read-only check if performed before value is checked try { list.Remove(null); Assert.Fail(testCase + "#17"); } catch (NotSupportedException) { // read-only collection cannot be modified } try { list.RemoveAt(0); Assert.Fail(testCase + "#18"); } catch (NotSupportedException) { // read-only collection cannot be modified } try { list[0] = mockPropertyDescr; Assert.Fail(testCase + "#19"); } catch (NotSupportedException) { // read-only collection cannot be modified } // ensure read-only check if performed before value is checked try { list[0] = null; Assert.Fail(testCase + "#20"); } catch (NotSupportedException) { // read-only collection cannot be modified } IDictionary dictionary = (IDictionary)descriptors; Assert.IsTrue(dictionary.IsReadOnly, testCase + "#21"); Assert.IsTrue(dictionary.IsFixedSize, testCase + "#22"); try { dictionary.Add("test", mockPropertyDescr); Assert.Fail(testCase + "#23"); } catch (NotSupportedException) { // read-only collection cannot be modified } // value is checked before read-only check try { dictionary.Add("test", null); Assert.Fail(testCase + "#24"); } catch (ArgumentException) { // read-only collection cannot be modified } try { dictionary.Clear(); Assert.Fail(testCase + "#25"); } catch (NotSupportedException) { // read-only collection cannot be modified } try { dictionary[0] = mockPropertyDescr; Assert.Fail(testCase + "#26"); } catch (NotSupportedException) { // read-only collection cannot be modified } // ensure read-only check if performed before value is checked try { dictionary[0] = null; Assert.Fail(testCase + "#27"); } catch (NotSupportedException) { // read-only collection cannot be modified } }