首页 新闻 赞助 找找看

LinQ 判断是否为空错误

0
悬赏园豆:20 [已解决问题] 解决于 2014-01-02 20:19

IEnumerable<MethodBase> methods = from p in defaultMembers.OfType<PropertyInfo>()
select p.GetGetMethod() into m
 where m != null
 select m

 

错误 :

无法将类型“System.Collections.Generic.IEnumerable<System.Reflection.MethodInfo>”隐式转换为“System.Collections.Generic.IEnumerable<System.Reflection.MethodBase>”。存在一个显式转换(是否缺少强制转换?) 

fredxiong的主页 fredxiong | 初学一级 | 园豆:178
提问于:2014-01-01 23:35
< >
分享
最佳答案
1

类型转换一下

(from p in ......
 select m).Cast<MethodBase>();

收获园豆:12
Qlin | 老鸟四级 |园豆:2403 | 2014-01-02 16:34

谢谢,就是我要的结果。

fredxiong | 园豆:178 (初学一级) | 2014-01-02 20:16
其他回答(5)
1

用下面的代码试试

var methods = from p in defaultMembers.OfType<PropertyInfo>()
select p.GetGetMethod() into m
 where m != null
 select m

收获园豆:2
诶碧司 | 园豆:1912 (小虾三级) | 2014-01-01 23:40
0

可以用 var 来声明 返回的结果集对象,

也可以强制转换一次在后面

收获园豆:1
Zery | 园豆:6151 (大侠五级) | 2014-01-02 07:45
0

不是 m!=null 判断为空的错误,而是类型转换的错误

收获园豆:1
Yu | 园豆:12980 (专家六级) | 2014-01-02 09:06
0

IEnumerable<MethodBase> methods =... 和你最终返回的m的类型不匹配,p.GetGetMethod() 这个方法获取的对象你需要做类型转化,不过不能转换,则换一种,把MethodBase类型.换成和m相同的,再做遍历

收获园豆:1
邢少 | 园豆:10926 (专家六级) | 2014-01-02 09:14
0

你可以试试:

IEnumerable<MethodInfo> methods = from p in defaultMembers.OfType<PropertyInfo>()
select p.GetGetMethod() into m
 where m != null
 select m

错误是由于类型不匹配造成的。

收获园豆:3
幻天芒 | 园豆:37175 (高人七级) | 2014-01-02 13:24

楼上的方法对,但我要的是返回MethodBase,谢谢

支持(0) 反对(0) fredxiong | 园豆:178 (初学一级) | 2014-01-02 20:16

@fredxiong: 哈哈,类型转换即可。

支持(0) 反对(0) 幻天芒 | 园豆:37175 (高人七级) | 2014-01-02 22:42
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册