+ -
当前位置:首页 → 问答吧 → System.Int32和System.Object的问题

System.Int32和System.Object的问题

时间:2011-12-09

来源:互联网

前些天在网上找到了一些代码可以扩展 Linq 里的 Distinct方法。

无奈小弟不才,对代码里的委托,反射部分的知识还不够,遇到难题,不知如何解决。

先附部分代码:

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
进行强转换ConvertToInt32(你的参数试试);

作者: vurtne0511   发布时间: 2011-12-09

相关阅读 更多