C#中dynamic類型作為泛型參數傳遞過去后,反射出來的對象類型是object,我用老外的這篇博文中的代碼跑起來,得出的結果是:Flying using a Object map (a map),將Fly<T>(T map)方法的代碼改為如下代碼,即可獲取dynamic對象的原始類型:
Type t = typeof(T); if (t == typeof(object)) { t = map.GetType(); } Console.WriteLine("Flying using a {0} map ({1})", t.Name, map);
實際項目中用到了MvcContrib,在調用OrderBy時傳入的是IQueryable<dynmaic>對象,反射此類對象的屬性時,會報異常,MvcContrib.Sorting中代碼是這樣的:
public static IQueryable<T> OrderBy<T>(this IQueryable<T> datasource, string propertyName, SortDirection direction) { if(string.IsNullOrEmpty(propertyName)) { return datasource; } var type = typeof(T); var property = type.GetProperty(propertyName); //這里報異常 if(property == null) { throw new InvalidOperationException(string.Format("Could not find a property called '{0}' on type {1}", propertyName, type)); } var parameter = Expression.Parameter(type, "p"); var propertyAccess = Expression.MakeMemberAccess(parameter, property); var orderByExp = Expression.Lambda(propertyAccess, parameter); const string orderBy = "OrderBy"; const string orderByDesc = "OrderByDescending"; string methodToInvoke = direction == SortDirection.Ascending ? orderBy : orderByDesc; var orderByCall = Expression.Call(typeof(Queryable), methodToInvoke, new[] { type, property.PropertyType }, datasource.Expression, Expression.Quote(orderByExp)); return datasource.Provider.CreateQuery<T>(orderByCall); }
原因是調用此方法時,如果傳入的泛型是dynamic,typeof(T)得出的結果是object,接下來的property將會出異常。
解決的辦法是:在 var type = typeof(T); 后面加一段判斷語句,代碼如下:
var type = typeof(T); if(type == typeof(object)) { type = datasource.FirstOrDefault().GetType(); }
這樣就可以獲取IQueryable<dynmaic>的原始類型。