Access properties in generic class

35 views Asked by At

Suppose I have a class like this:

    public class DepartmentBase<T> : MyBase where T : new()
    {
        public ObservableCollection<T> Employees
        {
           get { return _employees; }
           set { _employees = value; OnPropertyChanged(); }
        }
        private ObservableCollection<T> _employees;
    
        public T Selected_employee
        {
            get { return _selected_employee; }
            set { _selected_employee = value; OnPropertyChanged(); }
        }
        private T _selected_employee;
        
        public void AddEmployee(object parameter)
        { 
           //Using dynamic - each type T has those 3 properties        
           dynamic new_employee = new T();
           new_employee.NEW = true;
           new_employee.START_DATE = DateTime.Now;
    
           Employees.Add(new_employee);
           
           //Using reflection with Read() and Set() methods, to set each T item property "NEW" to false
           Employees.Where(a => (bool)a.Read("NEW") == true).ToList().ForEach(b => b.Set("NEW", false));
    
           Selected_employee = Employees.Last();
        }
    }

As you see, void AddEmployees needs to directly handle with properties of type T in 2 different ways.

In first lines I add items to Collection, using dynamic keyword. This enables me to write code like that.

After that I'm using Linq on Collection, where certain properties meets criteria. For that I use reflection.

Both ways are resolved in run-time (as I am aware of), which has performance hits.

My question is: How to access properties in generic class in a way that I could write code as with dynamic keyword, but also work same way with Linq and keep everything anonymous so that code resolves at compile time?

1

There are 1 answers

6
mm8 On BEST ANSWER

How to access properties in generic class in a way that I could write code as with dynamic keyword, but also work same way with Linq and keep everything anonymous so that code resolves at compile time?

By adding a constraint on the type parameter:

public class DepartmentBase<T> : MyBase where T : IModel, new()
{
    public ObservableCollection<T> Employees
    {
        get { return _employees; }
        set { _employees = value; OnPropertyChanged(); }
    }
    private ObservableCollection<T> _employees;

    public T Selected_employee
    {
        get { return _selected_employee; }
        set { _selected_employee = value; OnPropertyChanged(); }
    }
    private T _selected_employee;

    public void AddEmployee(object parameter)
    {
        T new_employee = new T();
        new_employee.NEW = true;
        new_employee.START_DATE = DateTime.Now;

        Employees.Add(new_employee);

        //Using reflection with Read() and Set() methods, to set each T item property "NEW" to false
        Employees.Where(a => (bool)a.Read("NEW") == true).ToList().ForEach(b => b.Set("NEW", false));

        Selected_employee = Employees.Last();
    }
}

IModel is an interface (or class) that any type that you use with DepartmentBase<T> must implement:

public interface IModel
{
    bool NEW { get; set; }
    DateTime START_DATE { get; set; }

    bool? Read(string s);
    void Set(string s, bool b);
}