public ActionResult <IEnumerable <Field> > GetDna(string modelName) { //var _dm = new DnaManager(_localizer); Dna dna = null; try { dna = _dm.GetDna(modelName); return(Ok(dna)); } catch (Exception ex) { Console.WriteLine(ex.Message); return(NotFound()); } }
public static void Model2Model <T, T2>(T fromModel, T2 toModel) { PropertyInfo[] to = toModel.GetType().GetProperties(); //モデルからアノテーション情報を取得する var dna = new DnaManager(); var to_ap = dna.GetDna(toModel.GetType().AssemblyQualifiedName).Fields; var fr_ap = dna.GetDna(fromModel.GetType().AssemblyQualifiedName).Fields; // プロパティ情報出力をループで回す foreach (PropertyInfo info in to) { if (_exclusions.Contains(info.Name)) { continue; } var property = typeof(T).GetProperty(info.Name); if (property != null) { try { var val = property.GetValue(fromModel); if (val == null) { info.SetValue(toModel, null); } else { Type to_t = info.PropertyType; Type fr_t = property.PropertyType; //アノテーション属性取得 var to_f = (from x in to_ap where x.Name.Equals(ConvertFirstTextLowerCase(info.Name)) select x).FirstOrDefault(); var fr_f = (from x in fr_ap where x.Name.Equals(ConvertFirstTextLowerCase(property.Name)) select x).FirstOrDefault(); if (to_f == null || fr_f == null) { info.SetValue(toModel, val); // DateTime型から⇒string型 } else if ((fr_t.Equals(typeof(DateTime)) || fr_t.Equals(typeof(DateTime?))) && to_t.Equals(typeof(string))) { if (val == null) { info.SetValue(toModel, null); } else { if (to_f.Type.Equals("date")) { //日付の場合 info.SetValue(toModel, ((DateTime)val).ToString("yyyy-MM-dd")); } else if (to_f.Type.Equals("time")) { //時間の場合 info.SetValue(toModel, ((DateTime)val).ToString("hh:mm:ss")); } else if (to_f.Type.Equals("datetime-local") || to_f.Type.Equals("datetime")) { //日時の場合 info.SetValue(toModel, ((DateTime)val).ToString("yyyy/MM/dd hh:mm:ss")); } } } else if ((fr_t.Equals(typeof(DateTime)) || fr_t.Equals(typeof(DateTime?))) && (to_t.Equals(typeof(DateTime?)) || to_t.Equals(typeof(DateTime)))) { info.SetValue(toModel, val); } else if ((to_t.Equals(typeof(DateTime?)) || to_t.Equals(typeof(DateTime))) && fr_t.Equals(typeof(string))) { info.SetValue(toModel, DateTime.Parse(val.ToString())); } else if (fr_t.Equals(typeof(File)) && to_t.Equals(typeof(string))) { var f = (File)val; info.SetValue(toModel, (f.FileName + "," + f.FileType + "," + f.Url + "," + f.FileSize)); } else if (to_t.Equals(typeof(File)) && fr_t.Equals(typeof(string))) { var f = new File(val.ToString()); info.SetValue(toModel, f); } else { info.SetValue(toModel, val); } } } catch (Exception ex) { Console.WriteLine(info.Name + ":" + ex.Message); throw ex; } } } }