访问泛型类中的属性

huangapple go评论101阅读模式
英文:

Access properties in generic class

问题

以下是您要翻译的代码部分:

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();
    }
}

请注意,您要求只翻译代码部分,不包括问题部分。希望这有所帮助。如果您有其他翻译需求,请告诉我。

英文:

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

得分: 2

以下是翻译好的内容:

通过在类型参数上添加约束

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);

        // 使用反射与 Read() 和 Set() 方法,将每个 T 项目的属性 "NEW" 设置为 false
        Employees.Where(a => (bool)a.Read("NEW") == true).ToList().ForEach(b => b.Set("NEW", false));

        Selected_employee = Employees.Last();
    }
}

IModel 是一个接口(或类),您在 DepartmentBase<T> 中使用的任何类型都必须实现:

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

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

>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&lt;T&gt; : MyBase where T : IModel, new()
{
    public ObservableCollection&lt;T&gt; Employees
    {
        get { return _employees; }
        set { _employees = value; OnPropertyChanged(); }
    }
    private ObservableCollection&lt;T&gt; _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 &quot;NEW&quot; to false
        Employees.Where(a =&gt; (bool)a.Read(&quot;NEW&quot;) == true).ToList().ForEach(b =&gt; b.Set(&quot;NEW&quot;, false));

        Selected_employee = Employees.Last();
    }
}

IModel is an interface (or class) that any type that you use with DepartmentBase&lt;T&gt; must implement:

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

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

huangapple
  • 本文由 发表于 2023年4月13日 21:10:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76005846.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定