public static PropertyInfo GetIdProperty(Type type) { var flags = BindingFlags.Instance | BindingFlags.Public; PropertyInfo[] pinfos = type.GetProperties(flags); foreach (PropertyInfo pi in pinfos) { object[] customAttStr = pi.GetCustomAttributes(typeof(Newtonsoft.Json.JsonPropertyAttribute), false); if (customAttStr.Length > 0) { Newtonsoft.Json.JsonPropertyAttribute dm = customAttStr[0] as Newtonsoft.Json.JsonPropertyAttribute; if (string.Compare(dm.PropertyName, "Id", StringComparison.OrdinalIgnoreCase) == 0) { return(pi); } } } PropertyInfo piId = type.GetProperty("Id", flags); if (piId == null) { piId = type.GetProperty("id", flags); if (piId == null) { piId = type.GetProperty("ID", flags); } } if (piId != null) { return(piId); } throw new SiaqodbException("Type of object does not have Id property required by Azure Mobile Services"); }
private static IOrderedQueryable <T> ApplyOrder <T>(IQueryable <T> source, string property, string methodName) { var props = property.Split('.'); var type = typeof(T); var arg = Expression.Parameter(type, "x"); Expression expr = arg; foreach (var prop in props) { // Figure out if the property was "renamed" with JsonProperty(PropertyName) // if so find the corresponding property PropertyInfo pi = null; var renamedProperties = type.GetProperties().Where(p => { IEnumerable <Object> re = p.GetCustomAttributes(false).Where(a => a is Newtonsoft.Json.JsonPropertyAttribute); return(re.Count() > 0 ? true : false); }); foreach (var renprop in renamedProperties) { Newtonsoft.Json.JsonPropertyAttribute jpa = (renprop.GetCustomAttributes(false).Where(a => a is Newtonsoft.Json.JsonPropertyAttribute)).FirstOrDefault() as Newtonsoft.Json.JsonPropertyAttribute; if (jpa.PropertyName == prop) { pi = renprop; break; } } if (pi == null) { pi = type.GetProperty(prop); } expr = Expression.Property(expr, pi); type = pi.PropertyType; } var delegateType = typeof(Func <,>).MakeGenericType(typeof(T), type); var lambda = Expression.Lambda(delegateType, expr, arg); var result = typeof(Queryable).GetMethods().Single( method => method.Name == methodName && method.IsGenericMethodDefinition && method.GetGenericArguments().Length == 2 && method.GetParameters().Length == 2) .MakeGenericMethod(typeof(T), type) .Invoke(null, new object[] { source, lambda }); return((IOrderedQueryable <T>)result); }