mercredi 10 avril 2019

C# loopthrough Class properties which contains list of list

I have one class whih contains a list of an other class. This list contains an other list of class. I would like to access of theses properties. My goal is to compare my value SQL with propertyInfo.Name. If the result is null that means I put a null value.

I have done a loop for my first object and the compare between SQL/propertyInfo.Name works. The real problem is to through all my properties

public class Appareil
{
    public int? Id { get; set; }
    public Marque marque { get; set; }
    public string modele { get; set; }
    public List<AppareilClient> appareil_client { get; set; }


}

public class Marque
{
    public int? Id { get; set; }
    public string marque { get; set; }      
}

public class AppareilClient
{
    public int? Id { get; set; }
    public List<AppareilReparation> appareils_reparations { get; set; }
    public string numero_serie { get; set; }     

}

public class AppareilReparation
{
    public string Num_dossier { get; set; }
    public int? Id { get; set; }    

}

here my function :

 public object AppliquerDroits(object e, string username)
    {
        // here I have a List with all name of colonne allow to show in my object
        //If the name of colonne doesn't exist in my object, I didn't put the value to null
        List<Droits> droit_utilisateur = GetDroits(username);
        Type myType = e.GetType();
        IList<PropertyInfo> props = new List<PropertyInfo>(myType.GetProperties());


        foreach (PropertyInfo prop in e.GetType().GetProperties())
        {
            if (prop.PropertyType.FullName.Contains("SavApi.Models"))
            {
                //the code comes here but i don't know what im doing

            }
            else
            {
                object propValue = prop.Name;
                var result = droit_utilisateur.Find(s => s.nom_colonne == prop.Name);

                if (result == null)
                // if (result.nom_colonne.Equals(prop.Name/*, StringComparison.OrdinalIgnoreCase)*/) == false)
                {
                    try
                    {
                        Type t = Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType;
                        object safeValue = (null == null) ? null : Convert.ChangeType(null, t);

                        prop.SetValue(e, safeValue, null);
                    }
                    catch (InvalidCastException z)
                    {
                        Console.WriteLine(z.Message.ToString());
                    }

                }
            } 

        }


            return e;
    }
}





Aucun commentaire:

Enregistrer un commentaire