2017-10-17 141 views
1

我使用將在我們的實體中動態創建集合實例的方法。我的問題是,當我創建它必須被插入到數據庫中的新記錄,我ICollection導航屬性始終是空,在我安全的方法使用反射創建List的實例c#

我必須使用像下面這樣來創建新的List及其絕對不是好辦法。如果我不創建List的實例,則Mapper會拋出錯誤,表明它不能將Collection的某些內容映射到null,例如它不能將Categories的列表映射爲空。

樣品我的安全方法,它具有如上所示的例如new List<Category>()創建List<T>實例

//check if record exists in database so i know if have to update or insert record (based on entity Id) 
var entity = repository.GetById(detail.Id) 
//....some code removed for brevity 
if (entity.Categories == null) 
{ 
    entity.Categories = new List<Category>(); 
} 
if (entity.UserContacts == null) 
{ 
    entity.UserContacts = new List<UserContact>(); 
} 
//dto => entity 
Mapper.PopulateEntity(dto, entity); 
//update or insert later on. 

擴展方法。

public TEntity InitializeEntity(TEntity entity) 
    { 
     var properties = entity.GetType().GetProperties(); 
     foreach (var prop in properties) 
     { 
      if (typeof(ICollection<TEntity>).Name == (prop.PropertyType.Name)) 
      { 
       var get = prop.GetGetMethod(); 
       //get assembly with entity namespace and class name 
       var fullName = get.GetBaseDefinition().ReturnType.GenericTypeArguments[0].AssemblyQualifiedName; 

       //create instance of object 
       var myObj = Activator.CreateInstance(Type.GetType(fullName)); 

       //check if property is null or if get some value, dont want to rewrite values from database 
       var value = prop.GetValue(entity); 
       if (value == null) 
       { 
        var listType = typeof(List<>); 
        var constructedListType = listType.MakeGenericType(myObj.GetType()); 
        Activator.CreateInstance(constructedListType); 
       } 
      } 
     } 
     return entity; 
    } 

出於某種原因,它不是建立在所有的任何實例,我想不出哪裏的問題。

回答

1

您忘記了通過prop.SetValue爲物業設置價值。這裏是有點清理你的代碼的版本與固定的問題:

public static TEntity InitializeEntity<TEntity>(TEntity entity) 
{ 
    var properties = entity.GetType().GetProperties(); 
    foreach (var prop in properties) 
    { 
     if (prop.PropertyType.IsGenericType && prop.PropertyType.GetGenericTypeDefinition() == typeof(ICollection<>)) 
     {           
      //check if property is null or if get some value, dont want to rewrite values from database 
      var value = prop.GetValue(entity); 
      if (value == null) { 
       var itemType = prop.PropertyType.GetGenericArguments()[0]; 
       var listType = typeof(List<>); 
       var constructedListType = listType.MakeGenericType(itemType); 
       prop.SetValue(entity, Activator.CreateInstance(constructedListType)); 
      } 
     } 
    } 
    return entity; 
    } 
+0

呀它更清潔的版本和完美的作品。謝謝 – Martin