public bool TryMapToDynamicObject(IMapToDynamicObjectContext context, out DynamicObject dto) { if (context.EntityEntry == null) { dto = null; return(false); } dto = new DynamicObject(context.Object.GetType()); context.AddToCache(dto); dto.Add(EntityType, context.EntityEntry.EntityType.DisplayName()); if (context.EntityEntry.EntityState != EntityState.Detached) { dto.Add( EntityLoadedNavigations, context.MapToDynamicObjectGraph( context.EntityEntry.EntityType.GetNavigations() .Where(n => context.EntityEntry.IsLoaded(n)) .Select(n => n.Name) .ToList())); } foreach (MemberEntry prop in context.EntityEntry.ToEntityEntry().Members) { DynamicObject value = context.MapToDynamicObjectGraph( Utils.ConvertToProvider(prop.CurrentValue, prop.Metadata as IProperty)); dto.Add(prop.Metadata.Name, value); } return(true); }
public bool TryMapToDynamicObject(IMapToDynamicObjectContext context, out DynamicObject dto) { if (!(context.Object is IGeometry geometry)) { dto = null; return(false); } dto = new DynamicObject(typeof(IGeometry)); context.AddToCache(dto); dto.Add(Data, new GeoJsonWriter().Write(geometry)); return(true); }
public bool TryMapToDynamicObject(IMapToDynamicObjectContext context, out DynamicObject dto) { var groupingType = Utils.GetGenericTypeImplementations(context.Object.GetType(), typeof(IGrouping <,>)).SingleOrDefault(); if (groupingType == null) { dto = null; return(false); } dto = new DynamicObject(groupingType); context.AddToCache(dto); dto.Add(Key, context.MapToDynamicObjectGraph(groupingType.GetProperty(Key)?.GetValue(context.Object))); dto.Add( Elements, new DynamicObject(((IEnumerable)context.Object).Cast <object>().Select(context.MapToDynamicObjectGraph).ToList())); return(true); }
public bool TryMapToDynamicObject(IMapToDynamicObjectContext context, out DynamicObject dto) { Type objType = context.Object.GetType(); if (!objType.IsArray) { dto = null; return(false); } dto = new DynamicObject(); context.AddToCache(dto); dto.Add(ArrayType, new TypeInfo(objType, includePropertyInfos: false)); dto.Add( Elements, ((IEnumerable)context.Object).Cast <object>().Select(context.MapToDynamicObjectGraph).ToArray()); return(true); }
public bool TryMapToDynamicObject(IMapToDynamicObjectContext context, out DynamicObject dto) { Type objType = context.Object.GetType(); if (objType == typeof(string)) { dto = null; return(false); } Type elementType = Utils.TryGetSequenceType(objType); if (elementType == null || elementType == typeof(DynamicObject)) { dto = null; return(false); } dto = new DynamicObject(typeof(IEnumerable <>).MakeGenericType(elementType)); context.AddToCache(dto); dto.Add( Elements, new DynamicObject(((IEnumerable)context.Object).Cast <object>().Select(context.MapToDynamicObjectGraph).ToList())); if (objType != typeof(List <>).MakeGenericType(elementType)) { bool hasDefaultCtor = objType.GetTypeInfo().DeclaredConstructors .Any(c => !c.IsStatic && c.IsPublic && c.GetParameters().Length == 0); if (hasDefaultCtor) { dto.Add(CollectionType, new TypeInfo(objType, includePropertyInfos: false)); } } return(true); }