Esempio n. 1
0
        /// <summary>
        /// 將檢視模型的內容抄寫到非資料實體模型物件。
        /// </summary>
        /// <typeparam name="T">要抄寫的目標物件型別</typeparam>
        /// <param name="entity"></param>
        /// <returns></returns>
        protected void CopyToModel <TViewB, TB>(TB entity, TViewB ViewModel) where TViewB : IBaseViewModel where TB : class
        {
            if (!Dispatcher.CheckAccess())
            {
                Dispatcher.Invoke(new Action <TB, TViewB>(CopyToModel), DispatcherPriority.Normal, entity, ViewModel);
            }
            else
            {
                try
                {
                    if (entity == null)
                    {
                        entity = Activator.CreateInstance <TB>();
                    }

                    Type CurrentViewModelType = ViewModel.GetType();
                    Type TargetEntity         = entity.GetType();
#if DEBUG
                    Debug.WriteLine("CopyToModel");
                    Debug.WriteLine(string.Format("資料實體{0},檢視模型為{1}", TargetEntity.Name, CurrentViewModelType.Name));
                    Debug.WriteLine("開始抄寫.");
#endif
                    var CurrentViewModel_Property = CurrentViewModelType.GetProperties();

                    foreach (var ViewModelProperty in CurrentViewModel_Property)
                    {
                        try
                        {
                            var EntityProperty = TargetEntity.GetProperty(ViewModelProperty.Name);
                            if (EntityProperty != null)
                            {
                                var entityvalue = EntityProperty.GetValue(entity);
                                var value       = ViewModelProperty.GetValue(ViewModel);

                                if (EntityProperty.PropertyType.IsGenericType && EntityProperty.PropertyType.GetGenericTypeDefinition().Name == (typeof(ICollection <>).Name))
                                {
                                    continue;
                                }

                                if (ViewModelProperty.PropertyType.IsGenericType && ViewModelProperty.PropertyType.GetGenericTypeDefinition().Name == (typeof(ObservableCollection <>).Name))
                                {
                                    continue;
                                }

                                if (ViewModelProperty.PropertyType.BaseType.IsGenericType && ViewModelProperty.PropertyType.BaseType.GetGenericTypeDefinition().Name == (typeof(ObservableCollection <>).Name))
                                {
                                    continue;
                                }

                                if (ViewModelProperty.PropertyType.BaseType.IsGenericType && ViewModelProperty.PropertyType.BaseType.GetGenericTypeDefinition().Name == (typeof(BaseViewModelCollection <>).Name))
                                {
                                    continue;
                                }

#if DEBUG
                                Debug.WriteLine(string.Format("資料實體屬性 {0}({2}) 內容值為 {1}.\n", EntityProperty.Name, entityvalue, EntityProperty.PropertyType.Name));
                                Debug.WriteLine(string.Format("檢視模型屬性 {0}({2}) 內容值為 {1}.\n", ViewModelProperty.Name, value, ViewModelProperty.PropertyType.Name));
#endif
                                if (value != null && !value.Equals(entityvalue))
                                {
                                    EntityProperty.SetValue(entity, value);
                                }
#if DEBUG
                                Debug.WriteLine(string.Format("抄寫後資料實體屬性 {0}({2}) 內容值為 {1}.\n", EntityProperty.Name, EntityProperty.GetValue(entity), EntityProperty.PropertyType.Name));
#endif
                            }
                        }
                        catch (Exception ex)
                        {
                            setErrortoModel(ViewModel, ex);
                            continue;
                        }
                    }
#if DEBUG
                    Debug.WriteLine("結束抄寫.");
#endif
                }
                catch (Exception ex)
                {
                    if (ViewModel != null)
                    {
                        ViewModel.Errors = new string[] { ex.Message + "," + ex.StackTrace }
                    }
                    ;
                }
            }
        }