How can I get the type of a property of T in a base class?
The class has a collection of type T called Values
:
public class BaseHistoryViewModel<T>
public ObservableCollection<T> Values { get; set; }
This type T
is always a class derived from another base class BaseHistoryItemViewModel
that has a property Value<T> where T : BaseModel
In my BaseHistoryViewModel I want to determine what the type of the Value property is:
protected virtual async void HandleOnDataChanged(object sender, DataChangedEventArgs e)
{
if (e.DataModelType == typeof(T))
{
await this.LoadDataAsync(true);
}
}
So far this would resolve to Weight == WeightItemViewModel
so I really need something like:
typeof(T.Value)
. I could implement this method in all my derived classes instead but there are nine of them as would rather handle this in the base if possible. Any help would be greatly appreciated.
UPDATE: This is what I did in the end from @Zohar's answer:
protected virtual async void HandleOnDataChanged(object sender, DataChangedEventArgs e)
{
var typeOfValues = typeof(T).GetProperty("Value").PropertyType;
if (e.DataModelType == typeOfValues)
{
await this.LoadDataAsync(true);
}
}