如何使用泛型类中的类的属性 [英] How to use a property from a Class in a Generic Class
问题描述
我有时会面临我的想法无法解决的问题。
我有一份包含多份报告的申请。
在网页中,我使用 GridView
来显示由按钮
触发的报告数据。
由于所有报告都具有相同的功能:通用类应该可用。
WebPage
- GridView
- 按钮
- 一些用作DB参数的选项
报告
- 带有GridView记录的DataTable
- parameterArray
数据访问层
- 通用报告类 - >内部。我有一个公共密封,应该由业务逻辑调用
I am facing sometimes the problems that my ideas do not get to work.
I have an application with several reports.
In a Webpage I use a GridView
to show the report data triggered by a Button
.
Since all reports have infact the same functionality: a generic class should be usable.
WebPage
- GridView
- Button
- Some selection item used as parameters for DB
Report
- DataTable with Records for GridView
- parameterArray
Data Access Layer
- Generic Report class -> internal. I have a public sealed that should be called by the business logic
internal class SimpleReport
{
internal string StoredProcedure {get; set;}
}
internal class OtherReport
{
internal string StoredProcedure {get; set;}
}
internal class Report<T> where T : class
{
public event Action<DataTable> GeneratedReport;
/// <summary>
/// Generates the specified t.
/// </summary>
/// <param name="t">The t.</param>
internal void Generate()
{
DataTable aTable = new DataTable();
/// How to use property StoredProcedure from SimpleReport/OtherReport here?
/// The class SimpleReport/OtherReport is in the 'T'
if (GeneratedReport != null)
GeneratedReport(aTable);
}
}
在Generic类中,T可以是SimpleReport。在SimpleReport中,我有一个DataTable属性。 T也可以是WeeklySalesreport并具有相同的DataTable属性(按名称 - > public DataTable值;
如何根据给定事件使用Generic类中的属性值Action< datatable> GeneratedReport ?
此外,每个报告类可以有不同的参数集(它们都有reportperiodID -int- common)我如何通过Generic类映射它们?
或者我的想法完全错了?
In the Generic class the T could be SimpleReport. In SimpleReport I have a DataTable property. T could also be WeeklySalesreport and has the same DataTable property (by name -> public DataTable Values;
How can I use the property Values in the Generic class based on the given event Action<datatable> GeneratedReport?
Furthermore each report class could have different set of parameters (they all have reportperiodID -int- common) How could I map them via the Generic class?
Or is my idea totally wrong?
推荐答案
public class Cell
{
private int cell_id;
private string cell_name;
private string cell_supervisor;
private double cell_target;
private double cell_routing_target, cell_attended_target, cell_lost_time_target, cell_unaccounted_time_target, cell_absence_target;
private List<string> cell_machinists;
private List<double> cell_routing_actual, cell_attended_actual, cell_lost_time_actual, cell_unaccounted_time_actual, cell_absence_actual;
private List<int> cell_ncr_qty;
private List<double> cell_ncr_cost;
private List<int> cell_timelost;
public Cell(int id, string name, double target)
{
cell_id = id;
cell_name = name;
cell_target = target;
}
public void setSupervisor(string name){
cell_supervisor = name;
}
public void setTargets(double routings, double attended, double losttime, double unacccounted, double abscence)
{
cell_routing_target = routings;
cell_attended_target = attended;
cell_lost_time_target = losttime;
cell_unaccounted_time_target = unacccounted;
cell_absence_target = abscence;
}
public void setMachinist(string name, double routings, double attended, double lost_time, double unaccounted_time, int ncr_qty, double ncr_cost, int time_lost, double absence)
{
cell_machinists.Add(name);
cell_routing_actual.Add(routings);
cell_attended_actual.Add(attended);
cell_lost_time_actual.Add(lost_time);
cell_unaccounted_time_actual.Add(unaccounted_time);
cell_ncr_qty.Add(ncr_qty);
cell_ncr_cost.Add(ncr_cost);
cell_timelost.Add(time_lost);
cell_absence_actual.Add(absence);
}
public string CellName
{
get {
return cell_name;
}
}
public string Supervisor
{
get
{
return cell_supervisor;
}
}
public List<string> Machinists
{
get
{
return cell_machinists;
}
}
}
使用嵌套列表和子报告是简短回答!
Use nested lists and sub reports is the short answer!
我使用泛型类中的构造函数修复它
i fixed it by using a constructor in the generic class
这篇关于如何使用泛型类中的类的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!