public ComponentImporter(Type type, ICustomTypeDescriptor typeDescriptor) : base(type) { if (typeDescriptor == null) typeDescriptor = new CustomTypeDescriptor(type); int count = 0; PropertyDescriptorCollection properties = typeDescriptor.GetProperties(); IObjectMemberImporter[] importers = new IObjectMemberImporter[properties.Count]; for (int i = 0; i < properties.Count; i++) { IServiceProvider sp = properties[i] as IServiceProvider; if (sp == null) continue; IObjectMemberImporter importer = (IObjectMemberImporter) sp.GetService(typeof(IObjectMemberImporter)); if (importer == null) continue; importers[i++] = importer; count++; } _properties = properties; if (count > 0) _importers = importers; }
public ComponentImporter(Type type, ICustomTypeDescriptor typeDescriptor) : base(type) { if (typeDescriptor == null) { typeDescriptor = new Maticsoft.Json.Conversion.CustomTypeDescriptor(type); } int num = 0; PropertyDescriptorCollection properties = typeDescriptor.GetProperties(); IObjectMemberImporter[] importerArray = new IObjectMemberImporter[properties.Count]; for (int i = 0; i < properties.Count; i++) { IServiceProvider provider = properties[i] as IServiceProvider; if (provider != null) { IObjectMemberImporter service = (IObjectMemberImporter) provider.GetService(typeof(IObjectMemberImporter)); if (service != null) { importerArray[i++] = service; num++; } } } this._properties = properties; if (num > 0) { this._importers = importerArray; } }
protected override object ImportFromObject(ImportContext context, JsonReader reader) { Debug.Assert(context != null); Debug.Assert(reader != null); reader.Read(); object obj = Activator.CreateInstance(OutputType); INonObjectMemberImporter otherImporter = obj as INonObjectMemberImporter; while (reader.TokenClass != JsonTokenClass.EndObject) { string memberName = reader.ReadMember(); PropertyDescriptor property = _properties.Find(memberName, true); // // Skip over the member value and continue with reading if // the property was not found or if the property found cannot // be set. // if (property == null || property.IsReadOnly) { if (otherImporter == null || !otherImporter.Import(context, memberName, reader)) { reader.Skip(); } continue; } // // Check if the property defines a custom import scheme. // If yes, ask it to import the value into the property // and then continue with the next. // if (_importers != null) { int index = _properties.IndexOf(property); IObjectMemberImporter importer = _importers[index]; if (importer != null) { importer.Import(context, reader, obj); continue; } } // // Import from reader based on the property type and // then set the value of the property. // property.SetValue(obj, context.Import(property.PropertyType, reader)); } return(ReadReturning(reader, obj)); }
public ComponentImporter(Type type, ICustomTypeDescriptor typeDescriptor, IObjectConstructor constructor) : base(type) { if (typeDescriptor == null) { typeDescriptor = new CustomTypeDescriptor(type); } int count = 0; PropertyDescriptorCollection properties = typeDescriptor.GetProperties(); IObjectMemberImporter[] importers = new IObjectMemberImporter[properties.Count]; for (int i = 0; i < properties.Count; i++) { IServiceProvider sp = properties[i] as IServiceProvider; if (sp == null) { continue; } IObjectMemberImporter importer = (IObjectMemberImporter)sp.GetService(typeof(IObjectMemberImporter)); if (importer == null) { continue; } importers[i] = importer; count++; } _properties = properties; if (count > 0) { _importers = importers; } _constructor = constructor; }