object GetObjectFor(INode subject, IEdgeSet edges) { object result = null; var typeEdge = edges.AllWhere(subject: subject, predicate: Rdf.Schema.Rdf.Type).FirstOrDefault(); if (typeEdge != null) { var typeName = this.Map.GetTypeName(typeEdge.Object); if (typeName != null) { var type = Type.GetType(typeName, throwOnError: false, ignoreCase: true); if (type != null) { result = Activator.CreateInstance(type); } } } return(result ?? new ExpandoObject()); }
public T Deserialize <T>(INode subject, IEdgeSet edges) where T : class { Contract.Requires(subject is IriNode || subject is BlankNode); Contract.Requires(edges != null); var typeEdge = edges.AllWhere(subject: subject, predicate: Rdf.Schema.Rdf.Type).FirstOrDefault(); if (typeEdge == null) { return(null); } var typeName = this.Map.GetTypeName(typeEdge.Object); if (typeName == null) { return(null); } var type = Type.GetType(typeName); if (type == null) { return(null); } if (type != typeof(T) && typeof(T).GetTypeInfo().IsSubclassOf(type)) { return(null); } object result = default(T); this.Deserialize(subject, edges, ref result); return(result as T); }
private void Deserialize(INode subject, IEdgeSet edges, ref object obj, bool throwOnMissing = false) { var literal = subject as LiteralNode; if (literal != null) { if (literal.TryGetValue(out obj)) { return; } } if (obj == null) { obj = this.GetObjectFor(subject, edges); } var propertyEdges = edges.AllWhere(subject: subject); foreach (var propertyEdge in propertyEdges) { if (propertyEdge.Predicate.Equals(Rdf.Schema.Rdf.Type)) { continue; } string propertyName; if (!this.Map.GetPropertyName(propertyEdge.Predicate, out propertyName)) { if (throwOnMissing) { throw new MemberAccessException("Can not get property for predicate " + propertyEdge.Predicate); } continue; } var property = obj.GetType().GetProperty(propertyName, BindingFlags.Instance | BindingFlags.Public); if (property == null || property.CanWrite == false) { if (throwOnMissing) { throw new MemberAccessException("Can not set property " + propertyName); } continue; } object value = null; this.Deserialize(propertyEdge.Object, edges, ref value); try { property.SetValue(obj, value); } catch (Exception e) when(e is ArgumentException || e is MethodAccessException || e is TargetInvocationException) { if (throwOnMissing) { throw; } } } }