2017-07-28 61 views
1

我创建了遍历数据从一个重复结构中的主键恢复的方法,但出现以下错误,无法隐式转换类型“System.Attribute”到“布尔”获取主键从数据库

using System; 
    using System.Collections.Generic; 
    using System.ComponentModel; 
    using System.Data.SqlClient; 
    using System.Linq; 
    using System.Reflection; 
    using System.Web; 




    UPDATE 
private void SetSelf(Cliente Entity) 
    { 

     foreach (PropertyInfo pro in Entity.GetType().GetProperties()) 
     { 

      if (this.GetType().GetProperty(pro.Name).GetCustomAttribute(typeof(DataObjectFieldAttribute != null)) 
      this.GetType().GetProperty(pro.Name).SetValue(this, pro.GetValue(Entity)); 

     } 

    } 

回答

0

因为此行:this.GetType().GetProperty(pro.Name).GetCustomAttribute(typeof(DataObjectFieldAttribute)将返回System.Attribute,但if正在寻找bool

难道你不想添加支票吗?

if (this.GetType().GetProperty(pro.Name).GetCustomAttribute(typeof(DataObjectFieldAttribute) != null){ 
} 
+0

我该如何解决这个问题兄弟? – Felipe

+0

可以试试我更新的吗? – zzT

+0

我能解决这个问题,我在typeof亲戚关闭之前放置了不同的null,我放置在亲戚关闭之后,并感谢帮助 – Felipe