public static Dictionary <String, String> obterMapeamento(Type tipo, String sufixoMapeamento = "") { Dictionary <string, string> mapeamento = new Dictionary <string, string>(); //MapeamentoImplicito mi = tipo.GetCustomAttribute<MapeamentoImplicito>(true); //object[] attributes = tipo.GetCustomAttributes(typeof (MapeamentoImplicito), true); MapeamentoImplicito mi = tipo.obterAtributo <MapeamentoImplicito>(true); //attributes.FirstOrDefault() as MapeamentoImplicito; PropertyInfo[] propriedades = tipo.GetProperties(); if (mi == null) { foreach (PropertyInfo p in propriedades) { Mapear mapear = p.obterAtributo <Mapear>(true); if (mapear == null) { continue; } mapeamento.Add(p.Name, converteMapeamento(String.IsNullOrEmpty(mapear.ColunaBanco) ? p.Name : mapear.ColunaBanco, sufixoMapeamento)); } } else { foreach (PropertyInfo p in propriedades) { NaoMapear naoMapear = p.obterAtributo <NaoMapear>(true); if (naoMapear != null) { continue; } Mapear mapear = p.obterAtributo <Mapear>(true); if (mapear == null) { mapeamento.Add(p.Name, converteMapeamento(p.Name, sufixoMapeamento)); } else { mapeamento.Add(p.Name, converteMapeamento(String.IsNullOrEmpty(mapear.ColunaBanco) ? p.Name : mapear.ColunaBanco, sufixoMapeamento)); } } } return(mapeamento); }
public Uniao(SqlConstrutor <Tipo> construtor, String nome, Type tipo, String tabela, String apelido = null) { sqlConstrutor = construtor; Nome = nome; Tipo = tipo; Tabela = tabela; Apelido = apelido ?? tabela; ConstructorInfo constructorInfo = Tipo.GetConstructor(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[0], null); instancia = (IEntidade)constructorInfo.Invoke(null); mapear = sqlConstrutor.entidade.GetType().GetProperty(nome).obterAtributo <Mapear>(true);//.GetRuntimeProperty(nome).GetCustomAttribute<Mapear>(); if (mapear == null) { throw new Exception(String.Format("Propriedade {0} da entidade {1} não possui a assinatura Mapear.", Nome, sqlConstrutor.entidade.GetType().FullName)); } Mapeamento = MapeamentoUtils.obterMapeamento(instancia, Apelido); }
public static IList <MapeamentoInfo> obterMapeamento(Type tipo, String sufixoMapeamento = "") { IList <MapeamentoInfo> mapeamento = new List <MapeamentoInfo>(); MapeamentoImplicito mi = tipo.obterAtributo <MapeamentoImplicito>(true); PropertyInfo[] propriedades = tipo.GetProperties(); if (mi == null) { foreach (PropertyInfo p in propriedades) { Mapear mapear = p.obterAtributo <Mapear>(true); if (mapear == null) { continue; } //mapeamento.Add(p.Name, converteMapeamento(String.IsNullOrEmpty(mapear.ColunaBanco) ? p.Name : mapear.ColunaBanco, sufixoMapeamento)); } } else { foreach (PropertyInfo p in propriedades) { NaoMapear naoMapear = p.obterAtributo <NaoMapear>(true); if (naoMapear != null) { continue; } Mapear mapear = p.obterAtributo <Mapear>(true); //if (mapear == null) // mapeamento.Add(p.Name, converteMapeamento(p.Name, sufixoMapeamento)); //else // mapeamento.Add(p.Name, converteMapeamento(String.IsNullOrEmpty(mapear.ColunaBanco) ? p.Name : mapear.ColunaBanco, sufixoMapeamento)); } } return(mapeamento); }
//private String converteMapeamento(String entrada) //{ // String saida = entrada; // if (!String.IsNullOrEmpty(sufixoMapeamento)) // { // saida = String.Format("{0}.{1}", sufixoMapeamento, entrada); // } // return saida; //} protected void converterValor(PropertyInfo pInfo, Type tipoCampo, Object vlObj) { if (vlObj == DBNull.Value) { return; } if (tipoCampo.IsSubclassOf(typeof(AbstractEntidade))) { Mapear mapear = pInfo.obterAtributo <Mapear>(true); if (mapear == null || String.IsNullOrEmpty(mapear.SufixoTabela)) { return; } ConstructorInfo constructorInfo = tipoCampo.GetConstructor(Type.EmptyTypes); if (constructorInfo == null) { throw new Exception(String.Format("Não foi possível instanciar {0}", tipoCampo.FullName)); } AbstractEntidade obj = (AbstractEntidade)constructorInfo.Invoke(null); obj.sufixoMapeamento = mapear.SufixoTabela; if (vlObj is IDataReader) { obj.deReader(vlObj as IDataReader); pInfo.SetValue(obterInstancia(), obj, null); return; } if (typeof(Hashtable) == vlObj.GetType()) { obj.deHashtable(vlObj as Hashtable); pInfo.SetValue(obterInstancia(), obj, null); return; } if (typeof(DataRow) == vlObj.GetType()) { obj.deTable(vlObj as DataRow); pInfo.SetValue(obterInstancia(), obj, null); return; } return; } if (tipoCampo.Name.IndexOf("Nullable") > -1) { tipoCampo = Nullable.GetUnderlyingType(pInfo.PropertyType); } if ((tipoCampo == typeof(int)) && (!(vlObj is int))) { pInfo.SetValue(obterInstancia(), int.Parse(vlObj.ToString()), null); } else if ((tipoCampo == typeof(Char)) && (!(vlObj is char))) { pInfo.SetValue(obterInstancia(), Char.Parse(vlObj.ToString()), null); } else if (tipoCampo.IsEnum) { if (vlObj is string) { pInfo.SetValue(obterInstancia(), Enum.Parse(tipoCampo, vlObj.ToString(), true), null); } else { pInfo.SetValue(obterInstancia(), Enum.ToObject(tipoCampo, vlObj), null); } } else { pInfo.SetValue(obterInstancia(), vlObj, null); } }