forked from Hercules-NET/ZeusFiscal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReflexao.cs
91 lines (81 loc) · 4.18 KB
/
Reflexao.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
namespace DFe.Utils
{
public static class Reflexao
{
/// <summary>
/// Copia o valor das propriedades comuns entre dois objetos
/// </summary>
/// <typeparam name="TOrigem"></typeparam>
/// <typeparam name="TDestino"></typeparam>
/// <param name="objetoOrigem"></param>
/// <param name="objetoDestino"></param>
public static void CopiarPropriedades<TDestino, TOrigem>(this TDestino objetoDestino, TOrigem objetoOrigem) where TDestino: class where TOrigem : class
{
foreach (var attributo in objetoOrigem.GetType().GetProperties().Where(p => p.CanRead))
{
var propertyInfo = objetoDestino.GetType().GetProperty(attributo.Name, BindingFlags.Public | BindingFlags.Instance);
if (propertyInfo != null && propertyInfo.CanWrite)
propertyInfo.SetValue(objetoDestino, attributo.GetValue(objetoOrigem, null), null);
}
}
/// <summary>
/// Obtém informações de uma propriedade de um objeto.
/// <example>var propinfo = Funcoes.ObterPropriedadeInfo(_cfgServico, c => c.DiretorioSalvarXml);</example>
/// </summary>
/// <typeparam name="TSource"></typeparam>
/// <typeparam name="TProperty"></typeparam>
/// <param name="source"></param>
/// <param name="propertyLambda"></param>
/// <returns>Retorna um objeto do tipo PropertyInfo com as informações da propriedade, como nome, tipo, etc</returns>
public static PropertyInfo ObterPropriedadeInfo<TSource, TProperty>(this TSource source, Expression<Func<TSource, TProperty>> propertyLambda)
{
var type = typeof(TSource);
var member = propertyLambda.Body as MemberExpression;
if (member == null)
throw new ArgumentException(string.Format("A expressão '{0}' se refere a um método, não a uma propriedade!", propertyLambda));
var propInfo = member.Member as PropertyInfo;
if (propInfo == null)
throw new ArgumentException(string.Format("A expressão '{0}' se refere a um campo, não a uma propriedade!", propertyLambda));
if (propInfo.ReflectedType != null && (type != propInfo.ReflectedType && !type.IsSubclassOf(propInfo.ReflectedType)))
throw new ArgumentException(string.Format("A expressão '{0}' refere-se a uma propriedade, mas não é do tipo {1}!", propertyLambda, type));
return propInfo;
}
/// <summary>
/// Obtém as propriedades de um determinado objeto
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="objeto"></param>
/// <returns>Retorna um objeto Dictionary contendo o nome da propriedade e seu valor</returns>
public static Dictionary<string, object> LerPropriedades<T>(this T objeto) where T : class
{
//A função pode ser melhorada para trazer recursivamente as propriedades dos objetos filhos
var dicionario = new Dictionary<string, object>();
foreach (var attributo in objeto.GetType().GetProperties())
{
var value = attributo.GetValue(objeto, null);
dicionario.Add(attributo.Name, value);
}
return dicionario;
}
/// <summary>
/// Obtém uma lista contendo os nomes das propriedades cujo valor não foi definido ou está vazio, de um determinado objeto
/// passado como parâmetro
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="objeto"></param>
/// <returns>Retorna uma lista de strings</returns>
public static List<string> ObterPropriedadesEmBranco<T>(this T objeto)
{
return
(from attributo in objeto.GetType().GetProperties()
let value = attributo.GetValue(objeto, null)
where value == null || string.IsNullOrEmpty(value.ToString())
select attributo.Name).ToList();
}
}
}