/// <summary> /// Pushes an array access on the path. /// </summary> /// <param name="descriptor">The descriptor of the array.</param> /// <param name="index">The index in the array.</param> /// <exception cref="System.ArgumentNullException">descriptor</exception> public void Push(ArrayDescriptor descriptor, int index) { if (descriptor == null) { throw new ArgumentNullException(nameof(descriptor)); } AddItem(new ArrayPathItem(descriptor, index)); }
public ArrayPathItem(ArrayDescriptor descriptor, int index) { if (descriptor == null) { throw new ArgumentNullException(nameof(descriptor)); } Index = index; Descriptor = descriptor; }
/// <inheritdoc /> public virtual void VisitArray(Array array, ArrayDescriptor descriptor) { for (var i = 0; i < array.Length; i++) { var value = array.GetValue(i); CurrentPath.Push(descriptor, i); VisitArrayItem(array, descriptor, i, value, TypeDescriptorFactory.Find(value?.GetType() ?? descriptor.ElementType)); CurrentPath.Pop(); } }
/// <summary> /// Creates a type descriptor for the specified type. /// </summary> /// <param name="type">The type.</param> /// <returns>An instance of type descriptor.</returns> protected virtual ITypeDescriptor Create(Type type) { ITypeDescriptor descriptor; // The order of the descriptors here is important if (PrimitiveDescriptor.IsPrimitive(type)) { descriptor = new PrimitiveDescriptor(this, type, emitDefaultValues, namingConvention); } else if (DictionaryDescriptor.IsDictionary(type)) // resolve dictionary before collections, as they are also collections { // IDictionary descriptor = new DictionaryDescriptor(this, type, emitDefaultValues, namingConvention); } else if (CollectionDescriptor.IsCollection(type)) { // ICollection descriptor = new CollectionDescriptor(this, type, emitDefaultValues, namingConvention); } else if (type.IsArray) { if (type.GetArrayRank() == 1) { // array[] - only single dimension array is supported descriptor = new ArrayDescriptor(this, type, emitDefaultValues, namingConvention); } else { // multi-dimension array to be treated as a 'standard' object descriptor = new ObjectDescriptor(this, type, emitDefaultValues, namingConvention); } } else if (NullableDescriptor.IsNullable(type)) { descriptor = new NullableDescriptor(this, type, emitDefaultValues, namingConvention); } else { // standard object (class or value type) descriptor = new ObjectDescriptor(this, type, emitDefaultValues, namingConvention); } return(descriptor); }
/// <summary> /// Creates a type descriptor for the specified type. /// </summary> /// <param name="type">The type.</param> /// <returns>An instance of type descriptor.</returns> protected virtual ITypeDescriptor Create(Type type) { ITypeDescriptor descriptor; // The order of the descriptors here is important try { if (PrimitiveDescriptor.IsPrimitive(type)) { descriptor = new PrimitiveDescriptor(this, type, emitDefaultValues, namingConvention); } else if (DictionaryDescriptor.IsDictionary(type)) // resolve dictionary before collections, as they are also collections { // IDictionary descriptor = new DictionaryDescriptor(this, type, emitDefaultValues, namingConvention); } else if (CollectionDescriptor.IsCollection(type)) { // ICollection descriptor = new CollectionDescriptor(this, type, emitDefaultValues, namingConvention); } else if (type.IsArray) { // array[] descriptor = new ArrayDescriptor(this, type, emitDefaultValues, namingConvention); } else if (NullableDescriptor.IsNullable(type)) { descriptor = new NullableDescriptor(this, type, emitDefaultValues, namingConvention); } else { // standard object (class or value type) descriptor = new ObjectDescriptor(this, type, emitDefaultValues, namingConvention); } } catch (Exception e) { // failed to get a descriptor... instead of crashing, just return nothing return(null); } return(descriptor); }
/// <inheritdoc /> public virtual void VisitArrayItem(Array array, ArrayDescriptor descriptor, int index, object item, ITypeDescriptor itemDescriptor) { Visit(item, itemDescriptor); }
/// <summary> /// Check if an array is of primtive types, these can be skipped in most processors /// </summary> /// <param name="descriptor"></param> /// <returns></returns> protected bool IsArrayOfPrimitiveType(ArrayDescriptor descriptor) => BlittableHelper.IsBlittable(descriptor.ElementType);