惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Apple Machine Learning Research
Apple Machine Learning Research
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
G
Google Developers Blog
博客园 - 司徒正美
J
Java Code Geeks
aimingoo的专栏
aimingoo的专栏
A
About on SuperTechFans
博客园 - 三生石上(FineUI控件)
WordPress大学
WordPress大学
T
The Blog of Author Tim Ferriss
D
Docker
大猫的无限游戏
大猫的无限游戏
D
DataBreaches.Net
腾讯CDC
V
Visual Studio Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
C
Check Point Blog
M
MIT News - Artificial intelligence
Jina AI
Jina AI
I
InfoQ
雷峰网
雷峰网
The Cloudflare Blog
美团技术团队
Engineering at Meta
Engineering at Meta

博客园 - 一 缕 阳 光

macOS 安装cocoapods记录 Flowable笔记 VUE项目中同时使用API代理与MockJs Ruoyi-Cloud-服务间的调用 Ruoyi-Cloud-增加单元测试和Mybatis-plus 使用Typro+dotnet-cnblog在博客园写博客 T4模板插件 使用Open Live Writer在博客园中写博文 Windows与CentOS8虚拟机网络共享与互通 因数据库连接串导致的netcore发布到IIS后报500错误 【转】使用ASP.NET Web API构建Restful API 表单设计器的探索 【原创】AltiumDesigner 6 的自定义菜单 估计项目的重要几点 从Project 2007导出WBS图表到Visio 2007 CMS: DNN And Umbraco ORA-01489: result of string concatenation is too long 【原创】长尾关键词的挖掘与使用方法 【原创】Tesseract-OCR 3.02 训练笔记
解决C#中dynamic类型作为泛型参数的反射问题
一 缕 阳 光 · 2017-02-09 · via 博客园 - 一 缕 阳 光

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>的原始类型。