/// <summary> /// Get those properties that were adorned by the <see cref="OwlResourceAttribute"/>. /// If there are none, then just get every property (and let later processors take their chances). /// </summary> /// <param name="originalType">the original type that the query was made against.</param> /// <param name="instanceType">the type of the object that must be returned.</param> /// <returns></returns> private IEnumerable <MemberInfo> GetPropertiesToPopulate(Type originalType, Type instanceType) { IEnumerable <MemberInfo> props; if (originalType == instanceType) // i.e. identity projection, meaning we can use GetAllPersistentProperties safely { props = OwlClassSupertype.GetAllPersistentProperties(OriginalType); } else { props = instanceType.GetProperties(); } return(props); }
/// <summary> /// Builds the projection function (<see cref="ProjectionFunction"/>) and extracts the arguments to it /// for use elsewhere. /// </summary> /// <param name="expression">The Select expression.</param> protected void BuildProjection(Expression expression) { if (expression == null) { throw new ArgumentNullException("expression"); } var ue = ((MethodCallExpression)expression).Arguments[1] as UnaryExpression; if (ue == null) { throw new LinqToRdfException("Incompatible expression type found when building ontology projection"); } var projectionFunctionExpression = (LambdaExpression)ue.Operand; if (projectionFunctionExpression == null) { throw new LinqToRdfException("Incompatible expression type found when building ontology projection"); } if (Expressions.ContainsKey("GroupBy")) { throw new NotSupportedException("Group By is not supported by VDS.RDF.Linq"); } // compile the projection's lambda expression into a function that can be used to instantiate new objects ProjectionFunction = projectionFunctionExpression.Compile(); // work out what kind of project is being performed (identity, member or anonymous type) // identity projection is from queries like this: "from a in ctx select a" // member projection from ones like this "from a in ctx select a.b" // anonymous type projection from this "from a in ctx select new {a.b, a.b}" if (projectionFunctionExpression.Body is ParameterExpression) // ie an identity projection { foreach (PropertyInfo i in OwlClassSupertype.GetAllPersistentProperties(OriginalType)) { ProjectionParameters.Add(i); } } else if (projectionFunctionExpression.Body is MemberExpression) // a member projection { var memex = projectionFunctionExpression.Body as MemberExpression; if (memex == null) { throw new LinqToRdfException("Expected MemberExpression was null"); } ProjectionParameters.Add(memex.Member); } else if (projectionFunctionExpression.Body is NewExpression) { // create an anonymous type var mie = projectionFunctionExpression.Body as NewExpression; if (mie == null) { throw new LinqToRdfException("Expected NewExpression was not present"); } foreach (MemberExpression me in mie.Arguments) { ProjectionParameters.Add(me.Member); } } else if (projectionFunctionExpression.Body is MethodCallExpression) { throw new NotSupportedException("Calling a method on the selected variable is not supported - use the Select() extension method on the results of the query to achieve this"); } else { throw new LinqToRdfException("The Projection used in this LINQ expression is not executable by VDS.RDF.Linq"); } }