mardi 11 août 2020

Using Reflection to get a collection property in generic type variable c# [duplicate]

I have my classes structure like below:

public abstract class base
{
    public ICollection<Attachment> Files { get; set; }
}

public class a : base
{
    public string Status { get; set; }
    public bool Shared { get; set; }
    public bool Urgent { get; set; }
}

public class a : base
{
    public string Status { get; set; }
    public bool Shared { get; set; }
    public bool Urgent { get; set; }
}

in my method I use generic argument which is like below, entity argument can be type a or b how can I access to Files property in my entity, I use reflection to get it and it gets the Files but it doesnt let me to loop into it.

    public virtual async Task<TEntity> CreateAsync(TEntity entity)
    {
       Type t = entity.GetType();
       var files = t.GetProperty("Files").GetValue(entity);

       //I want to loop into the file variable above
       foreach (var item in files)
       {
          //some code
       }
    }




Aucun commentaire:

Enregistrer un commentaire