大家帮忙看看这个错误如何解决?谢谢大家了,很急。描述很清楚但不知道怎么改啊。。无法将类型为“System.Data.Linq.Mapping.AssociationAttribute”的对象强制转换为类型“System.Data.Linq.Mapping.ColumnAttribute”。 
  说明: 执行当前 Web 请求期间,出现未经处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。  异常详细信息: System.InvalidCastException: 无法将类型为“System.Data.Linq.Mapping.AssociationAttribute”的对象强制转换为类型“System.Data.Linq.Mapping.ColumnAttribute”。源错误: 行 23:                 {
行 24:                     //如果自定义属性标识是主键
行 25:                     if (((System.Data.Linq.Mapping.ColumnAttribute)attrArray[0]).IsPrimaryKey == true)
行 26:                     {
行 27:                         continue;
  源文件:  F:\WebSite\Chaoyi.ServiceProvider\DataAccess\Common\BatchEvaluate.cs    行:  25 
public static void Eval(object source, object target)
        {
            //获得源对象所有公共属性信息的数组
            PropertyInfo[] sourcePropertyList = source.GetType().GetProperties();
            //获得目标对象所有公共属性信息的数组
            PropertyInfo[] targetPropertyList = target.GetType().GetProperties();
            //循环目标对象的所有公共属性
            foreach (PropertyInfo targetProp in targetPropertyList)
            {
                //获得目标对象属性的自定义属性
                object[] attrArray = targetProp.GetCustomAttributes(false);
                if (attrArray.Length > 0)    //如果目标对象属性有自定义属性
                {
                    //如果自定义属性标识是主键
                    if (((System.Data.Linq.Mapping.ColumnAttribute)attrArray[0]).IsPrimaryKey == true)
                    {
                        continue;
                    }
                }
                //根据目标对象属性名找到源对象对应的属性
                PropertyInfo sourceProp = sourcePropertyList.First(itm => itm.Name == targetProp.Name);
                if (sourceProp != null)
                {
                    //获得源对象属性值
                    object value = sourceProp.GetValue(source, null);
                    //将源对象的属性值赋给目标对象对应的属性
                    targetProp.SetValue(target, value, null);
                }
            }
        }object

解决方案 »

  1.   

    targetProp 用的是Association特性你改成这样试试
     if (((System.Data.Linq.Mapping.AssociationAttribute)attrArray[0]).IsPrimaryKey == true)               
    {                     
       continue;     
    }
      

  2.   

    AssociationAttribute就没有IsPrimaryKey这一个属性了。
      

  3.   

    那你就把target对象对应的类 把继承AssociationAttribute改成继承ColumnAttribute
      

  4.   

    但是source和target都是一样的对象,是数据库一个表的实体。如何改成ColumnAttribute,怎么操作?
      

  5.   

    你压根就不懂反射,乱说。将这段
    //获得目标对象属性的自定义属性
    object[] attrArray = targetProp.GetCustomAttributes(false);
    改为:
    System.Data.Linq.Mapping.ColumnAttribute[] attrArray = typeof(string).GetCustomAttributes(false)
                                    .Where(t => t is System.Data.Linq.Mapping.ColumnAttribute)
                                    .Cast <System.Data.Linq.Mapping.ColumnAttribute>()
                                    .ToArray();
    因为你反射的对象可能有多个Attribute,而第一个不见得就是ColumnAttribute,而且也不一定就存在ColumnAttribute
      

  6.   

    修正下,把typeof(string)改为targetProp,我写的时候就随便找了一个测试下的。
      

  7.   

    tryvar attrArray = targetProp.GetCustomAttributes(false).OfType<System.Data.Linq.Mapping.ColumnAttribute>().ToArray();
      

  8.   

    肯定不行,你错误的以为所有类型都是System.Data.Linq.Mapping.ColumnAttribute了,100%错了。
      

  9.   

    试了一下,还是报错.同样的异常。
    把使用这个函数的代码贴上来吧。        
    public bool UpdateInfo(User info)
            {
                var query = from item in dc.User
                            where item.UserID == info.UserID
                            select item;            BatchEvaluate.Eval(info, query.First());
                dc.SubmitChanges();            return true;
            }
      

  10.   

    dc.User 每一个对象的每一个属性都有并且只有一个 System.Data.Linq.Mapping.ColumnAttribute 标签声明吗?如果都有,你也不能胡乱写 attrArray[0],你则么确定使用下标 0 呢?看你的代码,是个喜欢盲目乱抄的,因为这个问题的逻辑错误太明显了。你应该多动自己的脑筋,不要人云亦云。
    另外忠告你,能够不用反射尽量不要使用反射。真正懂得使用反射的人不会滥用反射,因为它又慢、又难以调试开发。