I am trying to implement a multi-purpose user control with generics. I can develop the generic class, and can instantiate concrete child classes inherited from the genric class, but have problems passing the instantiated concrete classes to methods.
Expand|Select|Wrap|Line Numbers
- /// <summary>
- /// Represents an adapter that can take different types of dataset objects and perform some basic functions.
- /// </summary>
- /// <typeparam name="TDataset">Any type of dataset.</typeparam>
- /// <typeparam name="TTableAdapter">Any type of table adapter.</typeparam>
- /// <typeparam name="TDataTable">Any type of datatable.</typeparam>
- public class GridEntity<TDataset, TTableAdapter, TDataTable>
- where TDataset : DataSet, new()
- where TTableAdapter : System.ComponentModel.Component, new()
- where TDataTable : System.Data.DataTable, new()
- {
- protected TDataset dataset;
- protected TTableAdapter mainTableAdapter;
- protected TDataTable mainDataTable;
- protected System.ComponentModel.IContainer components = null;
- protected BindingSource mainBindingSource;
- /// <summary>
- /// Returns the Binding Source Data Member.
- /// </summary>
- public virtual string BindingSourceDataMember
- {
- get{}
- }
- /// <summary>
- /// Constructor
- /// </summary>
- public GridEntity(string datasetName)
- {
- dataset = new TDataset();
- dataset.DataSetName = datasetName;
- dataset.SchemaSerializationMode = System.Data.SchemaSerializationMode.IncludeSchema;
- components = new System.ComponentModel.Container();
- mainBindingSource = new System.Windows.Forms.BindingSource(this.components);
- mainBindingSource.DataSource = dataset;
- mainBindingSource.DataMember = BindingSourceDataMember;
- mainTableAdapter = new TTableAdapter();
- mainDataTable = new TDataTable();
- }
- /// <summary>
- /// Returns the Main Dataset
- /// </summary>
- public TDataset MainDataSet
- {
- get
- {
- return dataset;
- }
- }
- /// <summary>
- /// Returns the Main Table Adapter.
- /// </summary>
- protected internal TTableAdapter MainTableAdapter
- {
- get
- {
- return mainTableAdapter;
- }
- }
- /// <summary>
- /// Returns the Main Data table.
- /// </summary>
- protected internal TDataTable MainDataTable
- {
- get
- {
- return mainDataTable;
- }
- }
- /// <summary>
- /// Returns the Main Binding Source.
- /// </summary>
- public BindingSource MainBindingSource
- {
- get
- {
- return mainBindingSource;
- }
- }
- }
- public static GridEntity<TDataset, TTableAdapter, TDataTable> Instance()
- {
- return new GridEntity<TDataset, TTableAdapter, TDataTable>("MyDataset");
- }
- }
Expand|Select|Wrap|Line Numbers
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Windows.Forms;
- using System.Data;
- public class ChildGridEntity : GridEntity<MyNamespace.Data.MyDataset, MyNamespace.MyData.MyDatasetTableAdapters.ChildAdapter TableAdapter, MyNameSpace.Data.MyDataset.ChildDataTable>
- {
- some specific implementation...
- }
- }
Expand|Select|Wrap|Line Numbers
- public enum Entity
- {Child,
- Child2,
- ...
- }
- public class GridEntityAbstractFactory()
public static GridEntity(Entity entity)
Expand|Select|Wrap|Line Numbers
- {
- switch (entity)
- {
- Case Entity.Child1:
- ChildGridEntity gridEntity=new ChildGridEntity()
- break;
- ...
- }
- }
Expand|Select|Wrap|Line Numbers
- public class Client
- {
- public Client(
- {
- IntializeComponents(gridEntity);
- }
Thanks.