System.Int32和System.Object的问题
时间:2011-12-09
来源:互联网
前些天在网上找到了一些代码可以扩展 Linq 里的 Distinct方法。
无奈小弟不才,对代码里的委托,反射部分的知识还不够,遇到难题,不知如何解决。
先附部分代码:
C# code
这是 自定义类 DetPropertyComparer<T> 参数getPropertyValueFunc和构造方法
当我给参数 propertyName 赋值在数据库中属于System.Int32类型的字段名称的时候
getPropertyValueFunc = Expression.Lambda<Func<T, object>>(me, expPara).Compile();
这段代码会报错 原因是 System.Int32和System.Object 无法进行转换 。
我知道System.Int32属于结构类型,不知Int32是否可以转换System.Object?
在此想请教各位高手,可否让这段代码支持 结构类型。
如果可以该如何改动,希望各位高手不吝赐教,小弟不胜感激。
附此类全部代码:
C# code
无奈小弟不才,对代码里的委托,反射部分的知识还不够,遇到难题,不知如何解决。
先附部分代码:
C# code
private Func<T, Object> getPropertyValueFunc = null; /// <summary> /// 通过propertyName 获取PropertyInfo对象 /// </summary> /// <param name="propertyName"></param> public DetPropertyComparer(string propertyName) { PropertyInfo _PropertyInfo = typeof(T).GetProperty(propertyName, BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.Public); if (_PropertyInfo == null) { throw new ArgumentException(string.Format("{0} is not a property of type {1}.", propertyName, typeof(T))); } ParameterExpression expPara = Expression.Parameter(typeof(T), "obj"); MemberExpression me = Expression.Property(expPara, _PropertyInfo); getPropertyValueFunc = Expression.Lambda<Func<T, object>>(me, expPara).Compile(); }
这是 自定义类 DetPropertyComparer<T> 参数getPropertyValueFunc和构造方法
当我给参数 propertyName 赋值在数据库中属于System.Int32类型的字段名称的时候
getPropertyValueFunc = Expression.Lambda<Func<T, object>>(me, expPara).Compile();
这段代码会报错 原因是 System.Int32和System.Object 无法进行转换 。
我知道System.Int32属于结构类型,不知Int32是否可以转换System.Object?
在此想请教各位高手,可否让这段代码支持 结构类型。
如果可以该如何改动,希望各位高手不吝赐教,小弟不胜感激。
附此类全部代码:
C# code
public class DetPropertyComparer<T> : IEqualityComparer<T> { private Func<T, Object> getPropertyValueFunc = null; /// <summary> /// 通过propertyName 获取PropertyInfo对象 /// </summary> /// <param name="propertyName"></param> public DetPropertyComparer(string propertyName) { PropertyInfo _PropertyInfo = typeof(T).GetProperty(propertyName, BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.Public); if (_PropertyInfo == null) { throw new ArgumentException(string.Format("{0} is not a property of type {1}.", propertyName, typeof(T))); } ParameterExpression expPara = Expression.Parameter(typeof(T), "obj"); MemberExpression me = Expression.Property(expPara, _PropertyInfo); getPropertyValueFunc = Expression.Lambda<Func<T, object>>(me, expPara).Compile(); } #region IEqualityComparer<T> Members public bool Equals(T x, T y) { object xValue = getPropertyValueFunc(x); object yValue = getPropertyValueFunc(y); if (xValue == null) return yValue == null; return xValue.Equals(yValue); } public int GetHashCode(T obj) { object propertyValue = getPropertyValueFunc(obj); if (propertyValue == null) return 0; else return propertyValue.GetHashCode(); } #endregion }
作者: vurtne0511 发布时间: 2011-12-09
该回复于2011-12-09 13:00:38被管理员删除
- 对我有用[0]
- 丢个板砖[0]
- 引用
- 举报
- 管理
- TOP
|
#2楼 得分:0回复于:2011-12-09 14:52:13
|
作者: vurtne0511 发布时间: 2011-12-09
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28