英文:
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<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);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论