Vorarbeiten:
Nicht generische abstrakte BaseClass und generische Ableitung:
public abstract class ReportFilter { public string Description { get; set; } public Type Type { get; set; } } public class ReportFilter: ReportFilter { public T Value { get; set; } }
Liste mit zwei Einträgen:
var reportFilters = new List() { new ReportFilter<string> {Description="Name",Type=typeof(string),Value="Test"}, new ReportFilter<bool> {Description="IsAktiv",Type=typeof(bool),Value=true} };
Auslesen:
foreach (var reportFilter in reportFilters) { var type = typeof(ReportFilter<>).MakeGenericType(reportFilter.Type); var aktFilter = Convert.ChangeType(reportFilter, type); var propertyInfo = type.GetProperty("Value"); if (propertyInfo != null) { var val = propertyInfo.GetValue(aktFilter, null); } }
Kopierbare Version der Code-Schnipsel:
public abstract class ReportFilter
{
public string Description { get; set; }
public Type Type { get; set; }
}
public class ReportFilter