/// <summary> /// Initializes a new instance of the <see cref="DictionaryDescriptor" /> class. /// </summary> /// <param name="factory">The factory.</param> /// <param name="type">The type.</param> /// <exception cref="System.ArgumentException">Expecting a type inheriting from System.Collections.IDictionary;type</exception> public DictionaryDescriptor(ITypeDescriptorFactory factory, Type type) : base(factory, type) { if (!IsDictionary(type)) { throw new ArgumentException(@"Expecting a type inheriting from System.Collections.IDictionary", "type"); } Category = DescriptorCategory.Dictionary; // extract Key, Value types from IDictionary<??, ??> var interfaceType = type.GetInterface(typeof(IDictionary <,>)); if (interfaceType != null) { keyType = interfaceType.GetGenericArguments()[0]; valueType = interfaceType.GetGenericArguments()[1]; IsGenericDictionary = true; getEnumeratorGeneric = typeof(DictionaryDescriptor).GetMethod("GetGenericEnumerable").MakeGenericMethod(keyType, valueType); containsKeyMethod = type.GetMethod("ContainsKey", new[] { keyType }); } else { keyType = typeof(object); valueType = typeof(object); containsKeyMethod = type.GetMethod("Contains", new[] { keyType }); } getKeysMethod = type.GetProperty("Keys"); getValuesMethod = type.GetProperty("Values"); indexerProperty = type.GetProperty("Item", valueType, new[] { keyType }); indexerSetter = indexerProperty.SetMethod; removeMethod = type.GetMethod("Remove", new[] { keyType }); }
public bool FinishBuildFromType(ITypeDescriptorFactory factory) { var props = _type !.GetProperties(BindingFlags.Instance | BindingFlags.Public); #if DEBUG CheckObjectTypeIsGoodDto(_type); #endif foreach (var propertyInfo in props) { if (propertyInfo.GetIndexParameters().Length != 0) { continue; } if (ShouldNotBeStored(propertyInfo)) { continue; } var descriptor = factory.Create(propertyInfo.PropertyType); if (descriptor != null) { _fields.Add(new KeyValuePair <string, ITypeDescriptor>(GetPersistentName(propertyInfo), descriptor)); } } _fields.Sort(Comparer <KeyValuePair <string, ITypeDescriptor> > .Create((l, r) => string.Compare(l.Key, r.Key, StringComparison.InvariantCulture))); return(true); }
/// <summary> /// Initializes a new instance of the <see cref="ObjectDescriptor" /> class. /// </summary> public ObjectDescriptor(ITypeDescriptorFactory factory, Type type, bool emitDefaultValues, IMemberNamingConvention namingConvention) { if (factory == null) { throw new ArgumentNullException(nameof(factory)); } if (type == null) { throw new ArgumentNullException(nameof(type)); } if (namingConvention == null) { throw new ArgumentNullException(nameof(namingConvention)); } this.factory = factory; Type = type; IsCompilerGenerated = AttributeRegistry.GetAttribute <CompilerGeneratedAttribute>(type) != null; this.emitDefaultValues = emitDefaultValues; NamingConvention = namingConvention; Attributes = AttributeRegistry.GetAttributes(type); Style = DataStyle.Any; foreach (var attribute in Attributes) { var styleAttribute = attribute as DataStyleAttribute; if (styleAttribute != null) { Style = styleAttribute.Style; } } }
/// <summary> /// Initializes a new instance of the <see cref="ObjectDescriptor" /> class. /// </summary> /// <param name="factory">The factory.</param> /// <param name="type">The type.</param> /// <exception cref="System.ArgumentException">Type [{0}] is not a primitive</exception> public PrimitiveDescriptor(ITypeDescriptorFactory factory, Type type) : base(factory, type) { if (!IsPrimitive(type)) throw new ArgumentException("Type [{0}] is not a primitive"); Category = DescriptorCategory.Primitive; }
public DictionaryDescriptor(ITypeDescriptorFactory factory, Type type, bool emitDefaultValues, IMemberNamingConvention namingConvention) : base(factory, type, emitDefaultValues, namingConvention) { if (!IsDictionary(type)) { throw new ArgumentException(@"Expecting a type inheriting from System.Collections.IDictionary", nameof(type)); } // extract Key, Value types from IDictionary<??, ??> var interfaceType = type.GetInterface(typeof(IDictionary <,>)); if (interfaceType != null) { KeyType = interfaceType.GetGenericArguments()[0]; ValueType = interfaceType.GetGenericArguments()[1]; IsGenericDictionary = true; getEnumeratorGeneric = typeof(DictionaryDescriptor).GetMethod("GetGenericEnumerable").MakeGenericMethod(KeyType, ValueType); containsKeyMethod = interfaceType.GetMethod("ContainsKey", new[] { KeyType }); // Retrieve the other properties and methods from the interface type = interfaceType; } else { KeyType = typeof(object); ValueType = typeof(object); containsKeyMethod = type.GetMethod("Contains", new[] { KeyType }); } addMethod = type.GetMethod("Add", new[] { KeyType, ValueType }); getKeysMethod = type.GetProperty("Keys"); getValuesMethod = type.GetProperty("Values"); indexerProperty = type.GetProperty("Item", ValueType, new[] { KeyType }); indexerSetter = indexerProperty.SetMethod; removeMethod = type.GetMethod("Remove", new[] { KeyType }); }
public PrimitiveDescriptor(ITypeDescriptorFactory factory, Type type, bool emitDefaultValues, IMemberNamingConvention namingConvention) : base(factory, type, emitDefaultValues, namingConvention) { if (!IsPrimitive(type)) throw new ArgumentException("Type [{0}] is not a primitive"); // Handle remap for enum items if (type.IsEnum) { foreach (var member in type.GetFields(BindingFlags.Public | BindingFlags.Static)) { var attributes = AttributeRegistry.GetAttributes(member); foreach (var attribute in attributes) { var aliasAttribute = attribute as DataAliasAttribute; if (aliasAttribute != null) { if (enumRemap == null) { enumRemap = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase); } enumRemap[aliasAttribute.Name] = member.GetValue(null); } } } } }
public SetDescriptor(ITypeDescriptorFactory factory, Type type, bool emitDefaultValues, IMemberNamingConvention namingConvention) : base(factory, type, emitDefaultValues, namingConvention) { if (!IsSet(type)) { throw new ArgumentException(@"Expecting a type inheriting from System.Collections.ISet", nameof(type)); } // extract Key, Value types from ISet<??> var interfaceType = type.GetInterface(typeof(ISet <>)); // Gets the element type ElementType = interfaceType.GetGenericArguments()[0] ?? typeof(object); Type[] ArgTypes = { ElementType }; addMethod = interfaceType.GetMethod("Add", ArgTypes); removeMethod = type.GetMethod("Remove", ArgTypes); clearMethod = interfaceType.GetMethod("Clear"); containsMethod = type.GetMethod("Contains", ArgTypes); countMethod = type.GetProperty("Count").GetGetMethod(); isReadOnlyMethod = type.GetInterface(typeof(ICollection <>)).GetProperty("IsReadOnly").GetGetMethod(); HasAdd = true; HasRemove = true; HasIndexerAccessors = true; HasInsert = false; HasRemoveAt = false; }
public bool FinishBuildFromType(ITypeDescriptorFactory factory) { var props = _type.GetProperties(); #if DEBUG CheckObjectTypeIsGoodDTO(_type); #endif foreach (var propertyInfo in props) { if (propertyInfo.GetIndexParameters().Length != 0) { continue; } if (ShouldNotBeStored(propertyInfo)) { continue; } var descriptor = factory.Create(propertyInfo.PropertyType); if (descriptor != null) { _fields.Add(new KeyValuePair <string, ITypeDescriptor>(GetPersitentName(propertyInfo), descriptor)); } } return(true); }
public PrimitiveDescriptor(ITypeDescriptorFactory factory, Type type, bool emitDefaultValues, IMemberNamingConvention namingConvention) : base(factory, type, emitDefaultValues, namingConvention) { if (!IsPrimitive(type)) { throw new ArgumentException("Type [{0}] is not a primitive"); } // Handle remap for enum items if (type.IsEnum) { foreach (var member in type.GetFields(BindingFlags.Public | BindingFlags.Static)) { var attributes = AttributeRegistry.GetAttributes(member); foreach (var attribute in attributes) { var aliasAttribute = attribute as DataAliasAttribute; if (aliasAttribute != null) { if (enumRemap == null) { enumRemap = new Dictionary <string, object>(StringComparer.OrdinalIgnoreCase); } enumRemap[aliasAttribute.Name] = member.GetValue(null); } } } } }
/// <summary> /// Initializes a new instance of the <see cref="CollectionDescriptor" /> class. /// </summary> /// <param name="factory">The factory.</param> /// <param name="type">The type.</param> /// <exception cref="System.ArgumentException">Expecting a type inheriting from System.Collections.ICollection;type</exception> public CollectionDescriptor(ITypeDescriptorFactory factory, Type type) : base(factory, type) { if (!IsCollection(type)) throw new ArgumentException("Expecting a type inheriting from System.Collections.ICollection", "type"); // Gets the element type var collectionType = type.GetInterface(typeof(IEnumerable<>)); ElementType = (collectionType != null) ? collectionType.GetGenericArguments()[0] : typeof(object); Category = DescriptorCategory.Collection; bool typeSupported = false; // implements ICollection<T> Type itype = type.GetInterface(typeof(ICollection<>)); if (itype != null) { var add = itype.GetMethod("Add", new[] {ElementType}); CollectionAddFunction = (obj, value) => add.Invoke(obj, new[] {value}); var clear = itype.GetMethod("Clear", Type.EmptyTypes); CollectionClearFunction = obj => clear.Invoke(obj, EmptyObjects); var countMethod = itype.GetProperty("Count").GetGetMethod(); GetCollectionCountFunction = o => (int)countMethod.Invoke(o, null); var isReadOnly = itype.GetProperty("IsReadOnly").GetGetMethod(); IsReadOnlyFunction = obj => (bool)isReadOnly.Invoke(obj, null); typeSupported = true; } // implements IList<T> itype = type.GetInterface(typeof(IList<>)); if (itype != null) { var insert = itype.GetMethod("Insert", new[] { typeof(int), ElementType }); CollectionInsertFunction = (obj, index, value) => insert.Invoke(obj, new[] { index, value }); var removeAt = itype.GetMethod("RemoveAt", new[] { typeof(int) }); CollectionRemoveAtFunction = (obj, index) => removeAt.Invoke(obj, new object[] { index }); var getItem = itype.GetMethod("get_Item", new[] { typeof(int) }); var setItem = itype.GetMethod("set_Item", new[] { typeof(int), ElementType }); GetIndexedItem = (obj, index) => getItem.Invoke(obj, new object[] { index }); SetIndexedItem = (obj, index, value) => setItem.Invoke(obj, new[] { index, value }); hasIndexerAccessors = true; } // implements IList if (!typeSupported && typeof(IList).IsAssignableFrom(type)) { CollectionAddFunction = (obj, value) => ((IList)obj).Add(value); CollectionClearFunction = obj => ((IList)obj).Clear(); CollectionInsertFunction = (obj, index, value) => ((IList)obj).Insert(index, value); CollectionRemoveAtFunction = (obj, index) => ((IList)obj).RemoveAt(index); GetCollectionCountFunction = o => ((IList)o).Count; GetIndexedItem = (obj, index) => ((IList)obj)[index]; SetIndexedItem = (obj, index, value) => ((IList)obj)[index] = value; IsReadOnlyFunction = obj => ((IList)obj).IsReadOnly; hasIndexerAccessors = true; typeSupported = true; } if (!typeSupported) { throw new ArgumentException("Type [{0}] is not supported as a modifiable collection".ToFormat(type), "type"); } }
/// <summary> /// Initializes a new instance of the <see cref="ObjectDescriptor" /> class. /// </summary> /// <param name="factory">The factory.</param> /// <param name="type">The type.</param> /// <exception cref="System.ArgumentException">Type [{0}] is not a primitive</exception> public NullableDescriptor(ITypeDescriptorFactory factory, Type type, bool emitDefaultValues, IMemberNamingConvention namingConvention) : base(factory, type, emitDefaultValues, namingConvention) { if (!IsNullable(type)) throw new ArgumentException("Type [{0}] is not a primitive"); UnderlyingType = Nullable.GetUnderlyingType(type); }
public AssetObjectSerializerBackend(ITypeDescriptorFactory typeDescriptorFactory) { if (typeDescriptorFactory == null) { throw new ArgumentNullException(nameof(typeDescriptorFactory)); } this.typeDescriptorFactory = typeDescriptorFactory; }
public CustomObjectSerializerBackend(ITypeDescriptorFactory typeDescriptorFactory) { if (typeDescriptorFactory == null) { throw new ArgumentNullException("typeDescriptorFactory"); } this.typeDescriptorFactory = typeDescriptorFactory; }
public OverrideKeyMappingTransform(ITypeDescriptorFactory typeDescriptorFactory) { if (typeDescriptorFactory == null) { throw new ArgumentNullException("typeDescriptorFactory"); } this.typeDescriptorFactory = typeDescriptorFactory; }
/// <summary> /// Creates <see cref="DataVisitNode"/> from the specified instance. /// </summary> /// <param name="typeDescriptorFactory">The type descriptor factory.</param> /// <param name="rootInstance">The root instance to generate diff nodes.</param> /// <returns>A diff node object.</returns> public static DataVisitObject Run(ITypeDescriptorFactory typeDescriptorFactory, object rootInstance) { if (rootInstance == null) { return(null); } return(new DataVisitNodeBuilder(typeDescriptorFactory, rootInstance).Run()); }
public OverrideKeyMappingTransform(ITypeDescriptorFactory typeDescriptorFactory, bool keepOnlySealedOverrides) { if (typeDescriptorFactory == null) { throw new ArgumentNullException("typeDescriptorFactory"); } this.typeDescriptorFactory = typeDescriptorFactory; this.keepOnlySealedOverrides = keepOnlySealedOverrides; }
/// <summary> /// Initializes a new instance of the <see cref="DataVisitorBase"/> class. /// </summary> /// <param name="typeDescriptorFactory">The type descriptor factory.</param> /// <exception cref="ArgumentNullException">typeDescriptorFactory</exception> protected DataVisitorBase(ITypeDescriptorFactory typeDescriptorFactory) { if (typeDescriptorFactory == null) throw new ArgumentNullException(nameof(typeDescriptorFactory)); TypeDescriptorFactory = typeDescriptorFactory; CustomVisitors = new List<IDataCustomVisitor>(); context.DescriptorFactory = TypeDescriptorFactory; context.Visitor = this; CurrentPath = new MemberPath(16); }
/// <summary> /// Initializes a new instance of the <see cref="Serializer"/> class. /// </summary> /// <param name="settings">The settings.</param> public Serializer(SerializerSettings settings) { this.settings = settings ?? new SerializerSettings(); TypeDescriptorFactory = CreateTypeDescriptorFactory(); RoutingSerializer routingSerializer; ObjectSerializer = CreateProcessor(out routingSerializer); RoutingSerializer = routingSerializer; }
/// <summary> /// Initializes a new instance of the <see cref="ObjectDescriptor" /> class. /// </summary> /// <param name="factory">The factory.</param> /// <param name="type">The type.</param> /// <exception cref="System.ArgumentException">Type [{0}] is not a primitive</exception> public PrimitiveDescriptor(ITypeDescriptorFactory factory, Type type) : base(factory, type) { if (!IsPrimitive(type)) { throw new ArgumentException("Type [{0}] is not a primitive"); } Category = DescriptorCategory.Primitive; }
/// <summary> /// Initializes a new instance of the <see cref="ObjectDescriptor" /> class. /// </summary> /// <param name="factory">The factory.</param> /// <param name="type">The type.</param> /// <exception cref="System.ArgumentException">Type [{0}] is not a primitive</exception> public NullableDescriptor(ITypeDescriptorFactory factory, Type type) : base(factory, type) { if (!IsNullable(type)) throw new ArgumentException("Type [{0}] is not a primitive"); Category = DescriptorCategory.Nullable; UnderlyingType = Nullable.GetUnderlyingType(type); UnderlyingDescriptor = Factory.Find(UnderlyingType); }
/// <summary> /// Creates <see cref="DataVisitNode"/> from the specified instance. /// </summary> /// <param name="typeDescriptorFactory">The type descriptor factory.</param> /// <param name="rootInstance">The root instance to generate diff nodes.</param> /// <param name="customVisitors">Add </param> /// <returns>A diff node object.</returns> public static DataVisitObject Run(ITypeDescriptorFactory typeDescriptorFactory, object rootInstance, List<IDataCustomVisitor> customVisitors = null) { if (rootInstance == null) return null; var builder = new DataVisitNodeBuilder(typeDescriptorFactory, rootInstance); if (customVisitors != null) { builder.CustomVisitors.AddRange(customVisitors); } return builder.Run(); }
/// <summary> /// Initializes a new instance of the <see cref="ObjectDescriptor" /> class. /// </summary> /// <param name="factory">The factory.</param> /// <param name="type">The type.</param> /// <exception cref="System.ArgumentException">Type [{0}] is not a primitive</exception> public NullableDescriptor(ITypeDescriptorFactory factory, Type type, bool emitDefaultValues, IMemberNamingConvention namingConvention) : base(factory, type, emitDefaultValues, namingConvention) { if (!IsNullable(type)) { throw new ArgumentException("Type [{0}] is not a primitive"); } UnderlyingType = Nullable.GetUnderlyingType(type); }
/// <summary> /// Initializes a new instance of the <see cref="SerializerContext"/> class. /// </summary> /// <param name="serializer">The serializer.</param> internal SerializerContext(Serializer serializer) { Serializer = serializer; settings = serializer.Settings; tagTypeRegistry = settings.AssemblyRegistry; ObjectFactory = settings.ObjectFactory; ObjectSerializerBackend = settings.ObjectSerializerBackend; Schema = Settings.Schema; ObjectSerializer = serializer.ObjectSerializer; typeDescriptorFactory = serializer.TypeDescriptorFactory; }
/// <summary> /// Initializes a new instance of the <see cref="FieldDescriptor"/> class. /// </summary> /// <param name="fieldInfo">The property information.</param> public FieldDescriptor(ITypeDescriptorFactory factory, FieldInfo fieldInfo) : base(factory, fieldInfo) { if (fieldInfo == null) { throw new ArgumentNullException("fieldInfo"); } this.fieldInfo = fieldInfo; this.TypeDescriptor = Factory.Find(fieldInfo.FieldType); }
public bool FinishBuildFromType(ITypeDescriptorFactory factory) { var descriptor = factory.Create(_itemType); if (descriptor == null) { return(false); } InitFromItemDescriptor(descriptor); return(true); }
/// <summary> /// Initializes a new instance of the <see cref="DataVisitorBase"/> class. /// </summary> /// <param name="typeDescriptorFactory">The type descriptor factory.</param> /// <exception cref="System.ArgumentNullException">typeDescriptorFactory</exception> protected DataVisitorBase(ITypeDescriptorFactory typeDescriptorFactory) { if (typeDescriptorFactory == null) { throw new ArgumentNullException("typeDescriptorFactory"); } TypeDescriptorFactory = typeDescriptorFactory; CustomVisitors = new List <IDataCustomVisitor>(); context.DescriptorFactory = TypeDescriptorFactory; context.Visitor = this; CurrentPath = new MemberPath(16); }
public void FinishBuildFromType(ITypeDescriptorFactory factory) { var props = _type.GetProperties(); foreach (var propertyInfo in props) { var descriptor = factory.Create(propertyInfo.PropertyType); if (descriptor != null) { _fields.Add(new KeyValuePair<string, ITypeDescriptor>(GetPersitentName(propertyInfo), descriptor)); } } }
/// <summary> /// Initializes a new instance of the <see cref="CollectionDescriptor" /> class. /// </summary> /// <param name="factory">The factory.</param> /// <param name="type">The type.</param> /// <exception cref="System.ArgumentException">Expecting a type inheriting from System.Collections.ICollection;type</exception> public CollectionDescriptor(ITypeDescriptorFactory factory, Type type) : base(factory, type) { if (!IsCollection(type)) { throw new ArgumentException("Expecting a type inheriting from System.Collections.ICollection", "type"); } // Gets the element type var collectionType = type.GetInterface(typeof(IEnumerable <>)); ElementType = (collectionType != null) ? collectionType.GetGenericArguments()[0] : typeof(object); Category = DescriptorCategory.Collection; bool typeSupported = false; // implements ICollection<T> Type itype = type.GetInterface(typeof(ICollection <>)); if (itype != null) { var add = itype.GetMethod("Add", new[] { ElementType }); CollectionAddFunction = (obj, value) => add.Invoke(obj, new[] { value }); var countMethod = itype.GetProperty("Count").GetGetMethod(); GetCollectionCountFunction = o => (int)countMethod.Invoke(o, null); var isReadOnly = itype.GetProperty("IsReadOnly").GetGetMethod(); IsReadOnlyFunction = obj => (bool)isReadOnly.Invoke(obj, null); typeSupported = true; } // implements IList<T> itype = type.GetInterface(typeof(IList <>)); if (itype != null) { var insert = itype.GetMethod("Insert", new[] { typeof(int), ElementType }); CollectionInsertFunction = (obj, index, value) => insert.Invoke(obj, new[] { index, value }); var removeAt = itype.GetMethod("RemoveAt", new[] { typeof(int) }); CollectionRemoveAtFunction = (obj, index) => removeAt.Invoke(obj, new object[] { index }); } // implements IList if (!typeSupported && typeof(IList).IsAssignableFrom(type)) { CollectionAddFunction = (obj, value) => ((IList)obj).Add(value); CollectionInsertFunction = (obj, index, value) => ((IList)obj).Insert(index, value); CollectionRemoveAtFunction = (obj, index) => ((IList)obj).RemoveAt(index); GetCollectionCountFunction = o => ((IList)o).Count; IsReadOnlyFunction = obj => ((IList)obj).IsReadOnly; hasIndexerSetter = true; typeSupported = true; } if (!typeSupported) { throw new ArgumentException("Type [{0}] is not supported as a modifiable collection".ToFormat(type), "type"); } }
/// <summary> /// Initializes a new instance of the <see cref="ObjectDescriptor" /> class. /// </summary> /// <param name="factory">The factory.</param> /// <param name="type">The type.</param> /// <exception cref="System.ArgumentException">Type [{0}] is not a primitive</exception> public NullableDescriptor(ITypeDescriptorFactory factory, Type type) : base(factory, type) { if (!IsNullable(type)) { throw new ArgumentException("Type [{0}] is not a primitive"); } Category = DescriptorCategory.Nullable; UnderlyingType = Nullable.GetUnderlyingType(type); UnderlyingDescriptor = Factory.Find(UnderlyingType); }
/// <summary> /// Initializes a new instance of the <see cref="SerializerContext"/> class. /// </summary> /// <param name="serializer">The serializer.</param> /// <param name="serializerContextSettings">The serializer context settings.</param> internal SerializerContext(Serializer serializer, SerializerContextSettings serializerContextSettings) { Serializer = serializer; settings = serializer.Settings; tagTypeRegistry = settings.AssemblyRegistry; ObjectFactory = settings.ObjectFactory; ObjectSerializerBackend = settings.ObjectSerializerBackend; Schema = Settings.Schema; ObjectSerializer = serializer.ObjectSerializer; typeDescriptorFactory = serializer.TypeDescriptorFactory; contextSettings = serializerContextSettings ?? SerializerContextSettings.Default; }
/// <summary> /// Initializes a new instance of the <see cref="DataVisitNodeBuilder"/> class. /// </summary> /// <param name="typeDescriptorFactory">The type descriptor factory.</param> /// <param name="rootInstance">The root instance of the object to visit.</param> /// <exception cref="System.ArgumentNullException">rootInstance</exception> private DataVisitNodeBuilder(ITypeDescriptorFactory typeDescriptorFactory, object rootInstance) : base(typeDescriptorFactory) { CustomVisitors.AddRange(AssetRegistry.GetDataVisitNodeBuilders()); if (rootInstance == null) throw new ArgumentNullException("rootInstance"); this.rootInstance = rootInstance; var objectDescriptor = typeDescriptorFactory.Find(rootInstance.GetType()) as ObjectDescriptor; if (objectDescriptor == null) throw new ArgumentException("Expecting an object", "rootInstance"); stackItems.Push(new DataVisitObject(rootInstance, objectDescriptor)); }
public ArrayDescriptor(ITypeDescriptorFactory factory, Type type, bool emitDefaultValues, IMemberNamingConvention namingConvention) : base(factory, type, emitDefaultValues, namingConvention) { if (!type.IsArray) throw new ArgumentException(@"Expecting array type", nameof(type)); if (type.GetArrayRank() != 1) { throw new ArgumentException("Cannot support dimension [{0}] for type [{1}]. Only supporting dimension of 1".ToFormat(type.GetArrayRank(), type.FullName)); } ElementType = type.GetElementType(); }
/// <summary> /// Initializes a new instance of the <see cref="ListDescriptor" /> class. /// </summary> /// <param name="factory">The factory.</param> /// <param name="type">The type.</param> /// <exception cref="System.ArgumentException">Expecting a type inheriting from System.Collections.IList;type</exception> public ListDescriptor(ITypeDescriptorFactory factory, Type type, bool emitDefaultValues, IMemberNamingConvention namingConvention) : base(factory, type, emitDefaultValues, namingConvention) { if (!IsList(type)) { throw new ArgumentException(@"Expecting a type inheriting from System.Collections.IList", nameof(type)); } // Gets the element type ElementType = type.GetInterface(typeof(IEnumerable <>))?.GetGenericArguments()[0] ?? typeof(object); // implements IList if (typeof(IList).IsAssignableFrom(type)) { // implements IList ListAddFunction = (obj, value) => ((IList)obj).Add(value); ListClearFunction = obj => ((IList)obj).Clear(); ListInsertFunction = (obj, index, value) => ((IList)obj).Insert(index, value); ListRemoveAtFunction = (obj, index) => ((IList)obj).RemoveAt(index); GetListCountFunction = o => ((IList)o).Count; GetIndexedItem = (obj, index) => ((IList)obj)[index]; SetIndexedItem = (obj, index, value) => ((IList)obj)[index] = value; IsReadOnlyFunction = obj => ((IList)obj).IsReadOnly; } else // implements IList<T> { var add = type.GetMethod(nameof(IList <object> .Add), new[] { ElementType }); ListAddFunction = (obj, value) => add.Invoke(obj, new[] { value }); var remove = type.GetMethod(nameof(IList <object> .Remove), new[] { ElementType }); ListRemoveFunction = (obj, value) => remove.Invoke(obj, new[] { value }); var clear = type.GetMethod(nameof(IList <object> .Clear), Type.EmptyTypes); ListClearFunction = obj => clear.Invoke(obj, EmptyObjects); var countMethod = type.GetProperty(nameof(IList <object> .Count)).GetGetMethod(); GetListCountFunction = o => (int)countMethod.Invoke(o, null); var isReadOnly = type.GetInterface(typeof(ICollection <>)).GetProperty(nameof(IList <object> .IsReadOnly)).GetGetMethod(); IsReadOnlyFunction = obj => (bool)isReadOnly.Invoke(obj, null); var insert = type.GetMethod(nameof(IList <object> .Insert), new[] { typeof(int), ElementType }); ListInsertFunction = (obj, index, value) => insert.Invoke(obj, new[] { index, value }); var removeAt = type.GetMethod(nameof(IList <object> .RemoveAt), new[] { typeof(int) }); ListRemoveAtFunction = (obj, index) => removeAt.Invoke(obj, new object[] { index }); var getItem = type.GetMethod("get_Item", new[] { typeof(int) }); GetIndexedItem = (obj, index) => getItem.Invoke(obj, new object[] { index }); var setItem = type.GetMethod("set_Item", new[] { typeof(int), ElementType }); SetIndexedItem = (obj, index, value) => setItem.Invoke(obj, new[] { index, value }); } HasAdd = true; HasRemove = true; HasInsert = true; HasRemoveAt = true; HasIndexerAccessors = true; }
/// <summary> /// Initializes a new instance of the <see cref="ObjectDescriptor" /> class. /// </summary> /// <param name="factory">The factory.</param> /// <param name="type">The type.</param> /// <exception cref="System.ArgumentNullException">type</exception> /// <exception cref="System.InvalidOperationException">Failed to get ObjectDescriptor for type [{0}]. The member [{1}] cannot be registered as a member with the same name is already registered [{2}].ToFormat(type.FullName, member, existingMember)</exception> public ObjectDescriptor(ITypeDescriptorFactory factory, Type type) { if (factory == null) throw new ArgumentNullException("factory"); if (type == null) throw new ArgumentNullException("type"); this.factory = factory; Category = DescriptorCategory.Object; this.AttributeRegistry = factory.AttributeRegistry; this.type = type; var styleAttribute = AttributeRegistry.GetAttribute<DataStyleAttribute>(type); this.style = styleAttribute != null ? styleAttribute.Style : DataStyle.Any; this.IsCompilerGenerated = AttributeRegistry.GetAttribute<CompilerGeneratedAttribute>(type) != null; }
public void FinishBuildFromType(ITypeDescriptorFactory factory) { var props = _type.GetProperties(); foreach (var propertyInfo in props) { var descriptor = factory.Create(propertyInfo.PropertyType); if (descriptor != null) { _fields.Add(new KeyValuePair <string, ITypeDescriptor>(GetPersitentName(propertyInfo), descriptor)); } } }
/// <summary> /// Creates <see cref="DataVisitNode"/> from the specified instance. /// </summary> /// <param name="typeDescriptorFactory">The type descriptor factory.</param> /// <param name="rootInstance">The root instance to generate diff nodes.</param> /// <param name="customVisitors">Add </param> /// <returns>A diff node object.</returns> public static DataVisitObject Run(ITypeDescriptorFactory typeDescriptorFactory, object rootInstance, List <IDataCustomVisitor> customVisitors = null) { if (rootInstance == null) { return(null); } var builder = new DataVisitNodeBuilder(typeDescriptorFactory, rootInstance); if (customVisitors != null) { builder.CustomVisitors.AddRange(customVisitors); } return(builder.Run()); }
protected MemberDescriptorBase(ITypeDescriptorFactory factory, string name) { if (factory == null) { throw new ArgumentNullException("factory"); } if (name == null) { throw new ArgumentNullException("name"); } Factory = factory; Name = name; }
public ArrayDescriptor(ITypeDescriptorFactory factory, Type type, bool emitDefaultValues, IMemberNamingConvention namingConvention) : base(factory, type, emitDefaultValues, namingConvention) { if (!type.IsArray) { throw new ArgumentException(@"Expecting array type", nameof(type)); } if (type.GetArrayRank() != 1) { throw new ArgumentException("Cannot support dimension [{0}] for type [{1}]. Only supporting dimension of 1".ToFormat(type.GetArrayRank(), type.FullName)); } ElementType = type.GetElementType(); }
/// <summary> /// Initializes a new instance of the <see cref="ObjectDescriptor" /> class. /// </summary> /// <param name="factory">The factory.</param> /// <param name="type">The type.</param> /// <exception cref="System.ArgumentException">Expecting arrat type;type</exception> public ArrayDescriptor(ITypeDescriptorFactory factory, Type type) : base(factory, type) { if (!type.IsArray) throw new ArgumentException("Expecting array type", "type"); if (type.GetArrayRank() != 1) { throw new ArgumentException("Cannot support dimension [{0}] for type [{1}]. Only supporting dimension of 1".ToFormat(type.GetArrayRank(), type.FullName)); } Category = DescriptorCategory.Array; elementType = type.GetElementType(); listType = typeof(List<>).MakeGenericType(ElementType); toArrayMethod = listType.GetMethod("ToArray"); }
/// <summary> /// Initializes a new instance of the <see cref="ObjectDescriptor" /> class. /// </summary> public ObjectDescriptor(ITypeDescriptorFactory factory, Type type, bool emitDefaultValues, IMemberNamingConvention namingConvention) { if (factory == null) { throw new ArgumentNullException(nameof(factory)); } if (type == null) { throw new ArgumentNullException(nameof(type)); } if (namingConvention == null) { throw new ArgumentNullException(nameof(namingConvention)); } this.factory = factory; Type = type; IsCompilerGenerated = AttributeRegistry.GetAttribute <CompilerGeneratedAttribute>(type) != null; this.emitDefaultValues = emitDefaultValues; NamingConvention = namingConvention; Attributes = AttributeRegistry.GetAttributes(type); Style = DataStyle.Any; foreach (var attribute in Attributes) { var styleAttribute = attribute as DataStyleAttribute; if (styleAttribute != null) { Style = styleAttribute.Style; } } // Get DefaultMemberMode from DataContract DefaultMemberMode = DataMemberMode.Default; var currentType = type; while (currentType != null) { var dataContractAttribute = AttributeRegistry.GetAttribute <DataContractAttribute>(currentType); if (dataContractAttribute != null && (dataContractAttribute.Inherited || currentType == type)) { DefaultMemberMode = dataContractAttribute.DefaultMemberMode; break; } currentType = currentType.BaseType; } }
protected MemberDescriptorBase(ITypeDescriptorFactory factory, MemberInfo memberInfo) { if (factory == null) { throw new ArgumentNullException("factory"); } if (memberInfo == null) { throw new ArgumentNullException("memberInfo"); } Factory = factory; MemberInfo = memberInfo; Name = MemberInfo.Name; DeclaringType = memberInfo.DeclaringType; }
/// <summary> /// Initializes a new instance of the <see cref="DataVisitNodeBuilder"/> class. /// </summary> /// <param name="typeDescriptorFactory">The type descriptor factory.</param> /// <param name="rootInstance">The root instance of the object to visit.</param> /// <exception cref="System.ArgumentNullException">rootInstance</exception> private DataVisitNodeBuilder(ITypeDescriptorFactory typeDescriptorFactory, object rootInstance) : base(typeDescriptorFactory) { if (rootInstance == null) { throw new ArgumentNullException("rootInstance"); } this.rootInstance = rootInstance; var objectDescriptor = typeDescriptorFactory.Find(rootInstance.GetType()) as ObjectDescriptor; if (objectDescriptor == null) { throw new ArgumentException("Expecting an object", "rootInstance"); } stackItems.Push(new DataVisitObject(rootInstance, objectDescriptor)); }
public bool FinishBuildFromType(ITypeDescriptorFactory factory) { var keyDescriptor = factory.Create(_keyType); if (keyDescriptor == null) { return(false); } var valueDescriptor = factory.Create(_valueType); if (valueDescriptor == null) { return(false); } InitFromKeyValueDescriptors(keyDescriptor, valueDescriptor); return(true); }
/// <summary> /// Initializes a new instance of the <see cref="ObjectDescriptor" /> class. /// </summary> public ObjectDescriptor(ITypeDescriptorFactory factory, Type type, bool emitDefaultValues, IMemberNamingConvention namingConvention) { if (factory == null) throw new ArgumentNullException(nameof(factory)); if (type == null) throw new ArgumentNullException(nameof(type)); if (namingConvention == null) throw new ArgumentNullException(nameof(namingConvention)); this.factory = factory; Type = type; IsCompilerGenerated = AttributeRegistry.GetAttribute<CompilerGeneratedAttribute>(type) != null; this.emitDefaultValues = emitDefaultValues; NamingConvention = namingConvention; Attributes = AttributeRegistry.GetAttributes(type); Style = DataStyle.Any; foreach (var attribute in Attributes) { var styleAttribute = attribute as DataStyleAttribute; if (styleAttribute != null) { Style = styleAttribute.Style; } } }
/// <summary> /// Initializes a new instance of the <see cref="SerializerContext"/> class. /// </summary> /// <param name="serializer">The serializer.</param> internal SerializerContext(Serializer serializer) { Serializer = serializer; settings = serializer.Settings; tagTypeRegistry = settings.tagTypeRegistry; ObjectFactory = settings.ObjectFactory; ObjectSerializerBackend = settings.ObjectSerializerBackend; Schema = Settings.Schema; ObjectSerializer = serializer.ObjectSerializer; typeDescriptorFactory = serializer.TypeDescriptorFactory; }
/// <summary> /// Initializes a new instance of the <see cref="SerializerContext"/> class. /// </summary> /// <param name="serializer">The serializer.</param> internal SerializerContext(Serializer serializer) { Serializer = serializer; settings = serializer.Settings; tagTypeRegistry = settings.tagTypeRegistry; ObjectFactory = settings.ObjectFactory; ObjectSerializerBackend = settings.ObjectSerializerBackend; Schema = Settings.Schema; typeDescriptorFactory = new TypeDescriptorFactory(Settings.Attributes, Settings.EmitDefaultValues); }
protected AssetVisitorBase(ITypeDescriptorFactory typeDescriptorFactory) : base(typeDescriptorFactory) { // Add automatically registered custom data visitors CustomVisitors.AddRange(AssetRegistry.GetDataVisitNodes()); }
/// <summary> /// Initializes a new instance of the <see cref="CollectionDescriptor" /> class. /// </summary> /// <param name="factory">The factory.</param> /// <param name="type">The type.</param> /// <exception cref="System.ArgumentException">Expecting a type inheriting from System.Collections.ICollection;type</exception> public CollectionDescriptor(ITypeDescriptorFactory factory, Type type, bool emitDefaultValues, IMemberNamingConvention namingConvention) : base(factory, type, emitDefaultValues, namingConvention) { if (!IsCollection(type)) throw new ArgumentException(@"Expecting a type inheriting from System.Collections.ICollection", nameof(type)); // Gets the element type var collectionType = type.GetInterface(typeof(IEnumerable<>)); ElementType = (collectionType != null) ? collectionType.GetGenericArguments()[0] : typeof(object); var typeSupported = false; // implements IList if (typeof(IList).IsAssignableFrom(type)) { CollectionAddFunction = (obj, value) => ((IList)obj).Add(value); CollectionClearFunction = obj => ((IList)obj).Clear(); CollectionInsertFunction = (obj, index, value) => ((IList)obj).Insert(index, value); CollectionRemoveAtFunction = (obj, index) => ((IList)obj).RemoveAt(index); GetCollectionCountFunction = o => ((IList)o).Count; GetIndexedItem = (obj, index) => ((IList)obj)[index]; SetIndexedItem = (obj, index, value) => ((IList)obj)[index] = value; IsReadOnlyFunction = obj => ((IList)obj).IsReadOnly; HasIndexerAccessors = true; typeSupported = true; IsList = true; } var itype = type.GetInterface(typeof(ICollection<>)); // implements ICollection<T> if (!typeSupported && itype != null) { var remove = itype.GetMethod(nameof(ICollection<object>.Remove), new[] { ElementType }); CollectionRemoveFunction = (obj, value) => remove.Invoke(obj, new[] { value }); var add = itype.GetMethod(nameof(ICollection<object>.Add), new[] {ElementType}); CollectionAddFunction = (obj, value) => add.Invoke(obj, new[] {value}); var clear = itype.GetMethod(nameof(ICollection<object>.Clear), Type.EmptyTypes); CollectionClearFunction = obj => clear.Invoke(obj, EmptyObjects); var countMethod = itype.GetProperty(nameof(ICollection<object>.Count)).GetGetMethod(); GetCollectionCountFunction = o => (int)countMethod.Invoke(o, null); var isReadOnly = itype.GetProperty(nameof(ICollection<object>.IsReadOnly)).GetGetMethod(); IsReadOnlyFunction = obj => (bool)isReadOnly.Invoke(obj, null); typeSupported = true; // implements IList<T> itype = type.GetInterface(typeof(IList<>)); if (itype != null) { var insert = itype.GetMethod(nameof(IList<object>.Insert), new[] { typeof(int), ElementType }); CollectionInsertFunction = (obj, index, value) => insert.Invoke(obj, new[] { index, value }); var removeAt = itype.GetMethod(nameof(IList<object>.RemoveAt), new[] { typeof(int) }); CollectionRemoveAtFunction = (obj, index) => removeAt.Invoke(obj, new object[] { index }); var getItem = itype.GetMethod("get_Item", new[] { typeof(int) }); GetIndexedItem = (obj, index) => getItem.Invoke(obj, new object[] { index }); var setItem = itype.GetMethod("set_Item", new[] { typeof(int), ElementType }); SetIndexedItem = (obj, index, value) => setItem.Invoke(obj, new[] { index, value }); HasIndexerAccessors = true; IsList = true; } else { // Attempt to retrieve IList<> accessors from ICollection. var insert = type.GetMethod(nameof(IList<object>.Insert), new[] { typeof(int), ElementType }); if (insert != null) CollectionInsertFunction = (obj, index, value) => insert.Invoke(obj, new[] { index, value }); var removeAt = type.GetMethod(nameof(IList<object>.RemoveAt), new[] { typeof(int) }); if (removeAt != null) CollectionRemoveAtFunction = (obj, index) => removeAt.Invoke(obj, new object[] { index }); var getItem = type.GetMethod("get_Item", new[] { typeof(int) }); if (getItem != null) GetIndexedItem = (obj, index) => getItem.Invoke(obj, new object[] { index }); var setItem = type.GetMethod("set_Item", new[] { typeof(int), ElementType }); if (setItem != null) SetIndexedItem = (obj, index, value) => setItem.Invoke(obj, new[] { index, value }); HasIndexerAccessors = getItem != null && setItem != null; } } if (!typeSupported) { throw new ArgumentException($"Type [{(type)}] is not supported as a modifiable collection"); } }
public void FinishBuildFromType(ITypeDescriptorFactory factory) { }
public OverrideKeyMappingTransform(ITypeDescriptorFactory typeDescriptorFactory) { if (typeDescriptorFactory == null) throw new ArgumentNullException("typeDescriptorFactory"); this.typeDescriptorFactory = typeDescriptorFactory; }
public void FinishBuildFromType(ITypeDescriptorFactory factory) { InitFromItemDescriptor(factory.Create(_itemType)); }
public OverrideKeyMappingTransform(ITypeDescriptorFactory typeDescriptorFactory, bool keepOnlySealedOverrides) { if (typeDescriptorFactory == null) throw new ArgumentNullException("typeDescriptorFactory"); this.typeDescriptorFactory = typeDescriptorFactory; this.keepOnlySealedOverrides = keepOnlySealedOverrides; }
public void FinishBuildFromType(ITypeDescriptorFactory factory) { InitFromKeyValueDescriptors(factory.Create(_keyType), factory.Create(_valueType)); }
public bool FinishBuildFromType(ITypeDescriptorFactory factory) { var descriptor = factory.Create(_itemType); if (descriptor == null) return false; InitFromItemDescriptor(descriptor); return true; }
public CustomObjectSerializerBackend(ITypeDescriptorFactory typeDescriptorFactory) { if (typeDescriptorFactory == null) throw new ArgumentNullException("typeDescriptorFactory"); this.typeDescriptorFactory = typeDescriptorFactory; }
public bool FinishBuildFromType(ITypeDescriptorFactory factory) { var props = _type.GetProperties(); foreach (var propertyInfo in props) { if (propertyInfo.GetIndexParameters().Length != 0) continue; var descriptor = factory.Create(propertyInfo.PropertyType); if (descriptor != null) { _fields.Add(new KeyValuePair<string, ITypeDescriptor>(GetPersitentName(propertyInfo), descriptor)); } } return true; }
public bool FinishBuildFromType(ITypeDescriptorFactory factory) { var keyDescriptor = factory.Create(_keyType); if (keyDescriptor == null) return false; var valueDescriptor = factory.Create(_valueType); if (valueDescriptor == null) return false; InitFromKeyValueDescriptors(keyDescriptor, valueDescriptor); return true; }
/// <summary> /// Creates <see cref="DataVisitNode"/> from the specified instance. /// </summary> /// <param name="typeDescriptorFactory">The type descriptor factory.</param> /// <param name="rootInstance">The root instance to generate diff nodes.</param> /// <returns>A diff node object.</returns> public static DataVisitObject Run(ITypeDescriptorFactory typeDescriptorFactory, object rootInstance) { if (rootInstance == null) return null; return new DataVisitNodeBuilder(typeDescriptorFactory, rootInstance).Run(); }
public bool FinishBuildFromType(ITypeDescriptorFactory factory) { return true; }
public AssetVisitor(ITypeDescriptorFactory typeDescriptorFactory, Asset rootAsset) : base(typeDescriptorFactory) { this.rootAsset = rootAsset; }