Check if it is a list in c#

Asked

Viewed 259 times

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;
    }
  • 1

    Add your code to the question so we can help you friend

1 answer

2


You can check if the type implements the ICollection<>, since this interface is required in all collectible classes.

Put this check above all other checks:

if (valor is ICollection) { // vai direto pelo valor, isso se ele for um object
    foreach(var item in (valor as ICollection<object>)) {
         Console.WriteLine(item);
    }
}

The word is checks whether the first value is implemented from the second value class.

Syntax:

bool (<valor ou tipo> is <tipo>)

private bool VerificaTipoPropriedade<T>(PropertyInfo propriedade, ref T obj, ref int contador)
{
    Type tipo = propriedade.PropertyType;

    var valor = propriedade.GetValue(obj);

    if (valor is ICollection) { // vai direto pelo valor, isso se ele for um object
        foreach(var item in (valor as ICollection<object>) {
            Console.WriteLine(item);
        }
        return;
    }
    else 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;
}
  • 1

    It worked !... thank you very much!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.