/// <summary> /// This writes the public contents of the Entity in the properties element. /// </summary> /// <param name="entity">Entity</param> /// <returns>XElement representation of the properties element</returns> XElement WriteEntityContents(IOfflineEntity entity) { XElement contentElement = new XElement(FormatterConstants.ODataMetadataNamespace + FormatterConstants.PropertiesElementName); // Write only the primary keys if its an tombstone PropertyInfo[] properties = ReflectionUtility.GetPropertyInfoMapping(entity.GetType()); // Write individual properties to the feed, foreach (PropertyInfo fi in properties) { string edmType = FormatterUtilities.GetEdmType(fi.PropertyType); object value = fi.GetValue(entity, null); Type propType = fi.PropertyType; if (fi.PropertyType.IsGenericType && fi.PropertyType.Name.Equals(FormatterConstants.NullableTypeName, StringComparison.InvariantCulture)) { // Its a Nullable<T> property propType = fi.PropertyType.GetGenericArguments()[0]; } if (value == null) { contentElement.Add( new XElement(FormatterConstants.ODataDataNamespace + fi.Name, new XAttribute(FormatterConstants.ODataMetadataNamespace + FormatterConstants.AtomPubTypeElementName, edmType), new XAttribute(FormatterConstants.ODataMetadataNamespace + FormatterConstants.AtomPubIsNullElementName, true))); } else if (propType == FormatterConstants.DateTimeType || propType == FormatterConstants.TimeSpanType) { contentElement.Add( new XElement(FormatterConstants.ODataDataNamespace + fi.Name, new XAttribute(FormatterConstants.ODataMetadataNamespace + FormatterConstants.AtomPubTypeElementName, edmType), FormatterUtilities.ConvertDateTimeForType_Atom(value, propType))); } else if (propType != FormatterConstants.ByteArrayType) { contentElement.Add( new XElement(FormatterConstants.ODataDataNamespace + fi.Name, new XAttribute(FormatterConstants.ODataMetadataNamespace + FormatterConstants.AtomPubTypeElementName, edmType), value)); } else { byte[] bytes = (byte[])value; contentElement.Add( new XElement(FormatterConstants.ODataDataNamespace + fi.Name, new XAttribute(FormatterConstants.ODataMetadataNamespace + FormatterConstants.AtomPubTypeElementName, edmType), Convert.ToBase64String(bytes))); } } return(contentElement); }
public static IOfflineEntity GetObjectForType(EntryInfoWrapper wrapper, Type[] knownTypes) { Type entityType = null; ConstructorInfo ctorInfo = null; // See if its cached first. if (!_stringToCtorInfoMapping.TryGetValue(wrapper.TypeName, out ctorInfo)) { // Its not cached. Try to look for it then in list of known types. if (knownTypes != null) { entityType = knownTypes.Where((e) => e.FullName.Equals(wrapper.TypeName, StringComparison.InvariantCulture)).FirstOrDefault(); if (entityType == null) { throw new InvalidOperationException(string.Format("Unable to find a matching type for entry '{0}' in list of KnownTypes.", wrapper.TypeName)); } } else { // Try to look for the type in the list of all loaded assemblies. Assembly[] loadedAssemblies = new Assembly[] { Assembly.GetExecutingAssembly(), Assembly.GetCallingAssembly() }; foreach (Assembly assembly in loadedAssemblies) { entityType = assembly.GetTypes().Where((e) => e.FullName.Equals(wrapper.TypeName, StringComparison.InvariantCulture) && e.GetInterfaces().Count((e1) => e1.Name.Equals("IOfflineEntity")) == 1).FirstOrDefault(); if (entityType != null) { break; } } if (entityType == null) { throw new InvalidOperationException(string.Format("Unable to find a matching type for entry '{0}' in the loaded assemblies. Specify the type name in the KnownTypes argument to the SyncReader instance.", wrapper.TypeName)); } } // Reflect this entity and get necessary info ReflectionUtility.GetPropertyInfoMapping(entityType); ctorInfo = _stringToCtorInfoMapping[wrapper.TypeName]; } else { entityType = ctorInfo.ReflectedType; } // Invoke the ctor object obj = ctorInfo.Invoke(null); // Set the parameters only for non tombstone items if (!wrapper.IsTombstone) { PropertyInfo[] props = GetPropertyInfoMapping(entityType); foreach (PropertyInfo pinfo in props) { string value = null; if (wrapper.PropertyBag.TryGetValue(pinfo.Name, out value)) { pinfo.SetValue(obj, GetValueFromType(pinfo.PropertyType, value), null); } } } IOfflineEntity entity = (IOfflineEntity)obj; entity.ServiceMetadata = new OfflineEntityMetadata(wrapper.IsTombstone, wrapper.Id, wrapper.ETag, wrapper.EditUri); return(entity); }