I would like to use reflection in order to retrieve a list of fields filter by property Name that contains a certain value
public class StatusType : Enumeration
{
public static readonly StatusType Active = new StatusType (1, "ACTIVE");
public static readonly StatusType Inactive = new StatusType (2, "INACTIVE");
public StatusType (int id, string name)
: base(id, name)
{
}
}
public abstract class Enumeration : IComparable
{
public string Name { get; private set; }
public int Id { get; private set; }
protected Enumeration(int id, string name)
{
Id = id;
Name = name;
}
}
This is what I tried.
public class Test <T>
{
public void Get(string nameValue)
{
Type t = typeof(T);
var res = t.GetFields(BindingFlags.Static
| BindingFlags.FlattenHierarchy
| BindingFlags.Public)
.Where(a => a.GetValue(t).GetType().GetProperties().FirstOrDefault(p => string.Equals(p.Name, nameValue, StringComparison.OrdinalIgnoreCase))
.ToList();
}
}
Aucun commentaire:
Enregistrer un commentaire