private static object PopularObjetoRico(object objvalue, Type tipoRico, string nomePropriedadeCompleta, object objetoPopulado) { RefletorDinamico refletor = new RefletorDinamico(tipoRico); string nomeInstancia = nomePropriedadeCompleta.Split('.')[0]; string nomeProp = nomePropriedadeCompleta.Split('.')[1]; object novo = null; RefletorDinamico refletorPai = new RefletorDinamico(objetoPopulado.GetType()); PropertyInfo piPai = refletorPai.Propriedade(nomeInstancia, false); if (piPai != null) { novo = piPai.GetValue(objetoPopulado, null); } if (novo == null) { novo = Activator.CreateInstance(tipoRico); } PropertyInfo pii = refletor.Propriedade(nomeProp, true); if (pii != null) { pii.SetValue(novo, refletor.GetConvertedObject(pii.PropertyType, objvalue), null); } return(novo); }
public static T Popular <T>(object obj, string nomePropriedade, object valorPropriedade) where T : new() { if (obj == null) { obj = new T(); } RefletorDinamico refletor = new RefletorDinamico(obj.GetType()); object objvalue = valorPropriedade; if (objvalue != null && objvalue.ToString().ToLower() != "null") { if (nomePropriedade.Contains(".")) { string nomeInstancia = nomePropriedade.Split('.')[0]; string nomeProp = nomePropriedade.Split('.')[1]; //PROPRIEDADE RICA PropertyInfo pi = refletor.Propriedade(nomeInstancia, false); PropertyInfo piRico = refletor.Propriedade(nomePropriedade.Split('.')[0], false); Type tipoRico = piRico.PropertyType; objvalue = PopularObjetoRico(objvalue, tipoRico, nomePropriedade, obj); //POPULA PROP RICA pi.SetValue(obj, refletor.GetConvertedObject(pi.PropertyType, objvalue), null); } else { PropertyInfo pi = refletor.Propriedade(nomePropriedade, false); if (pi != null) { pi.SetValue(obj, refletor.GetConvertedObject(pi.PropertyType, objvalue), null); } } } return((T)obj); }
private static void MontarWorksheet <T>(List <T> listaValores, List <FormataExportacao> listaFormatada, ExcelWorksheet ws) { int row = 1; RefletorDinamico refletor = new RefletorDinamico(typeof(T)); try { for (int i = 0; i < listaFormatada.Count; i++) { ws.Cells[row, i + 1].Style.Font.Bold = true; ws.Cells[row, i + 1].Style.WrapText = false; ws.Cells[row, i + 1].Value = listaFormatada[i].NomeColuna; } row++; foreach (T item in listaValores) { for (int i = 0; i < listaFormatada.Count; i++) { string valor = ""; string propriedade = listaFormatada[i].NomePropriedade; bool ehRico = false; if (propriedade.Contains(".")) { ehRico = true; } try { PropertyInfo prop = refletor.Propriedade(propriedade, ehRico); if (!ehRico) { valor = prop.GetValue((T)item, null).ToString(); } else { valor = refletor.GetPropValueObjRico(propriedade, (T)item).ToString(); } if (valor == "True") { valor = "Sim"; } else if (valor == "False") { valor = "Não"; } } catch { valor = ""; } ws.Cells[row, i + 1].RichText.Add(valor); ws.Cells[row, i + 1].IsRichText = true; ws.Cells[row, i + 1].Style.WrapText = false; } row++; } } catch { throw; } }