private void AddFieldsAndProperties(MemberWrappers list) { const BindingFlags flags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly; // Create a list sorted by name. SortedList properties = new SortedList(new MemberNameComparer()); // Add fields. foreach (FieldInfo field in m_type.GetFields(flags)) { try { FieldWrapper wrapper; if (field.FieldType.IsValueType) { wrapper = new FieldWrapper(field, m_wrapped, m_rootReferenceObject, m_fieldPath); } else { wrapper = new FieldWrapper(field, m_wrapped, null, null); } properties.Add(wrapper.DisplayName, wrapper); } catch (System.Exception) { } } // Add properties. foreach (PropertyInfo property in m_type.GetProperties(flags)) { try { PropertyWrapper wrapper = new PropertyWrapper(property, m_wrapped); properties.Add(wrapper.DisplayName, wrapper); } catch (System.Exception) { } } // Copy the values (now in sorted order) to the MemberWrappers collection. foreach (MemberWrapper wrapper in properties.Values) { list.Add(wrapper); } }
private void AddListElements(MemberWrappers list) { IList wrapped = (IList)m_wrapped; for (int index = 0; index < wrapped.Count && index < MaxListElements; index++) { list.Add(new ListElementWrapper(wrapped, index)); } if (wrapped.Count > MaxListElements) { list.Add(new ListTooLongWrapper(MaxListElements)); } }
public virtual MemberWrappers GetMembers() { if ((AvailableFormats & WrappedValueFormats.Object) != WrappedValueFormats.Object) { throw new InvalidOperationException("Unable to get properties from a GenericWrapper object," + " because it does not contain the actual object value (only XML or string representation)."); } MemberWrappers list = new MemberWrappers(); if (m_type.IsArray) { AddArrayElements(list); // Don't return the fields of an array, just the elements. } else if (typeof(ArrayList).IsAssignableFrom(m_type)) { AddListElements(list); // A pure collection class, just return the list elements. } else if (typeof(Hashtable).IsAssignableFrom(m_type) || typeof(SortedList).IsAssignableFrom(m_type)) { AddDictionaryElements(list); // A pure dictionary class, just return the dictionary entries. } else { AddFieldsAndProperties(list); // Normal object - return its fields and properties. if (typeof(IList).IsAssignableFrom(m_type)) { AddListElements(list); // Implements IList - add the list elements. } if (typeof(IDictionary).IsAssignableFrom(m_type)) { AddDictionaryElements(list); // Implements IDictionary - add the dictionary entries. } } // Add the base type first (if any) before all the other members. Type baseType = m_type.BaseType; if (baseType != null && baseType != typeof(object) && baseType != typeof(Array)) { list.Insert(0, new BaseWrapper(m_type.BaseType, m_wrapped)); } return(list); }
private void AddDictionaryElements(MemberWrappers list) { IDictionary dictionary = (IDictionary)m_wrapped; int count = 0; foreach (object key in dictionary.Keys) { if (++count > MaxListElements) { list.Add(new ListTooLongWrapper("too many entries")); } else { list.Add(new DictionaryElementWrapper(dictionary, key)); } } }
private void AddArrayElements(MemberWrappers list) { Array array = (Array)m_wrapped; int rank = array.Rank; // Create an array of indices and initialise it to the lower bound for each dimension. int[] indices = new int[rank]; for (int i = 0; i < indices.Length; i++) { indices[i] = array.GetLowerBound(i); } // Iterate over every value for every dimension, starting from the last, eg.: // [0,0,0] // [0,0,1] // [0,1,0] // [0,1,1] // ... int dimension = rank - 1; int count = 0; while (true) { if (indices[dimension] > array.GetUpperBound(dimension)) { // We've gone past the upper bound of this dimension, so return to the last dimension // that can be incremented. while (dimension > 0 && indices[dimension] > array.GetUpperBound(dimension)) { dimension--; indices[dimension]++; } if (dimension == 0 && indices[0] > array.GetUpperBound(0)) { break; // Reached the upper bound of the first dimesion - all done. } // Re-initialise the indices for all dimesions after this one to the lower bound. for (int i = dimension + 1; i < indices.Length; i++) { indices[i] = array.GetLowerBound(i); } dimension = rank - 1; // Start iterating over the last dimension again. } // Create an ArrayElementWrapper using a copy of the indices array. int[] copyIndices = new int[rank]; Array.Copy(indices, copyIndices, rank); count++; if (count > MaxListElements) { list.Add(new ListTooLongWrapper(copyIndices)); break; } else { list.Add(new ArrayElementWrapper(array, copyIndices)); } indices[dimension]++; // Go to the next element in this dimension. } }