大家帮忙看看这个错误如何解决?谢谢大家了,很急。描述很清楚但不知道怎么改啊。。无法将类型为“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
说明: 执行当前 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
if (((System.Data.Linq.Mapping.AssociationAttribute)attrArray[0]).IsPrimaryKey == true)
{
continue;
}
//获得目标对象属性的自定义属性
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
把使用这个函数的代码贴上来吧。
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;
}
另外忠告你,能够不用反射尽量不要使用反射。真正懂得使用反射的人不会滥用反射,因为它又慢、又难以调试开发。