2013-11-27 82 views
0

对不起,如果标题不反映我真正想要的。如何在给对象作为参数时赋值给属性类?

我正在创建一个通用类,用于选择,更新,插入和删除数据库中的日期。

基本上,我想要一个函数,让我回到ObservableCollection <“可以是任何东西”> ==>凡什么是类而不是字符串。我想知道是否有可能做到这一点,如果是的话,请帮助我如何做到这一点。

这是我的出发点:

//类

public static ObservableCollection<ContactPerson> contactPersons = new ObservableCollection<ContactPerson>(); 

    public static ObservableCollection<ContactPerson> getContactPerson() 
    { 
     contactPersons = (ObservableCollection<ContactPerson>)DBConnection.GetDataOutDatabase(typeof(ContactPerson), "Contactpersoon"); 

     return contactPersons; 
    } 

// b类

 public static Object GetDataOutDatabase(Type myType,String table) 
    {    
     ObservableCollection<Object> objecten = new ObservableCollection<Object>(); 

     string sql = "SELECT * FROM " + table; 
     DbDataReader reader = Database.GetData(sql); 

     while (reader.Read()) 
     { 
      objecten.Add(Create(myType, reader)); 
     } 

     return objecten; 
    } 

    private static Object Create(Type myType, IDataRecord record) 
    { 
     PropertyInfo[] myPropertyInfo = myType.GetProperties(BindingFlags.Public | BindingFlags.Instance); 

     for (int i = 0; i < myPropertyInfo.Length; i++) 
     { 
      PropertyInfo myPropInfo = (PropertyInfo)myPropertyInfo[i]; 
      String name = myPropInfo.Name; 
      Type type = myPropInfo.PropertyType; 
     } 

     return null; 
    } 

而这就是我最终想要得到的。这可能吗?

 //ContactPerson cp = new ContactPerson(); 
     //cp.ID = (record["ID"].ToString()); 
     //cp.Name = record["Name"].ToString(); 
     //cp.Company = record["Company"].ToString(); 
     //cp.JobTitle = new ContactPersonTitle() 
     //{ 
     // Name = record["JobTitle"].ToString(), 
     //}; 
     //cp.JobRole = new ContactPersonType() 
     //{ 
     // Name = record["JobRole"].ToString(), 
     //}; 
     //cp.City = record["City"].ToString(); 
     //cp.Email = record["Email"].ToString(); 
     //cp.Phone = record["Phone"].ToString(); 
     //cp.Cellphone = record["Cellphone"].ToString(); 

非常感谢!

回答

0

实际上,你可以用普通的方法反映这一点。

public class DBConnection 
{ 
    public static ObservableCollection<T> GetDataOutDatabase<T>(string table) 
    { 
     var objecten = new ObservableCollection<T>(); 
     string sql = "SELECT * FROM " + table; 
     DbDataReader reader = Database.GetData(sql); 

     while (reader.Read()) 
     { 
      objecten.Add(Create<T>(reader)); 
     } 
     return objecten; 
    } 

    public static T Create<T>(IDataRecord record) 
    { 
     var properties = typeof(T).GetProperties(); 
     var returnVal = Activator.CreateInstance(typeof(T)); 
     properties.ToList().ForEach(item => 
      { 
       try 
       { 
        if (item.PropertyType.IsPrimitive) 
        { 
         item.SetValue(returnVal, Convert.ChangeType(record[item.Name].ToString(), item.PropertyType),null); 
        } 
        else 
        { 
         object[] parameters = {record}; 
         var value = 
         typeof(DBConnection).GetMethod("Create").MakeGenericMethod(item.PropertyType).Invoke(null, parameters); 
         item.SetValue(returnVal,value,null); 
        } 
       } 
       catch 
       { 
        Write("Property Not Found"); 
       } 
      }); 
     return (T)returnVal; 
    } 
} 

上面的例子假设所有属性名称都与您从数据库通信中检索的列名称匹配。例如在上面的ContactPersonTitle而不是Name中,您需要将JobTitle作为属性名称。

+0

我想这代码,它在这部分崩溃 properties.ToList()的ForEach(项目=> 了var属性,我得到的所有属性和returnVal设置空 – denderp

+0

每个值我认为问题出事实上我有2个属性,我不想用这个函数,我试图去掉var属性的一些属性,但是没有.remove函数,它可以是这样的问题,所以是的我如何从var属性中删除索引,如果它等于我指定的类型? – denderp

+0

在这种情况下,我会将if语句包装在properties.tolist linq表达式中的try catch块中,如果您不想吞下异常我会建议简单地记录它,这将允许你使用的属性被设置,而你不属于的属性将保持为空,而不是n试图从列表中排除属性 – theDarse