0
I have a method, where I will receive an object of any Class, and one of its properties can be a List that can be of a specific object or string, int, decimal and etc.
My question is: How to identify if a list has been received being of any kind and go through its properties taking into account that the list may or may not be a list of objects.
In the current form, it is only possible to identify whether the list is of the same type as the class object. If the list is of another class or if it is string...int etc, give as false always in the comparison.
public bool PropriedadesObjetoDefault<T>(T obj)
{
PropertyInfo[] propriedades = obj.GetType().GetProperties();
int totalPropriedades = propriedades.Count();
int contador = 0;
foreach (PropertyInfo propriedade in propriedades)
{
this.VerificaTipoPropriedade(propriedade, ref obj, ref contador);
}
if (contador == totalPropriedades)
return true;
else
return false;
}
private bool VerificaTipoPropriedade<T>(PropertyInfo propriedade, ref T obj, ref int contador)
{
Type tipo = propriedade.PropertyType;
var valor = propriedade.GetValue(obj);
if (tipo.Equals(typeof(bool)))
{
if (valor.Equals(default(bool)))
contador++;
}
else if (tipo.Equals(typeof(DateTime)))
{
if (valor.Equals(default(DateTime)))
contador++;
}
else if (tipo.Equals(typeof(List<T>)))
{
// percorrer entre os objetos
}
return true;
}
Add your code to the question so we can help you friend
– Victor Laio