Dynamic DataType in a Method? RRS feed

  • Question

  • Ok, basically what I have is a group of custom datasets (20 or so of them) and I want to do some processing on them using background threads.  The general flow is that data is read from a flat text file into one of the datasets, the dataset is processed, then this data is put into a SQL database which other software will access.  I want to have all 3 of those steps happen in a background thread.  The problem is that I don't want to write 20 or so methods to do this with the different datasets since it's all basically the same.  I could probably figure it out if I didn't have the threading issue.  I'm using the BackgroundWorker in this case.  The problem is that my method has a specific parameter list (object sender, DoWorkEventArgs e) because it is really an event called by the BackgroundWorker being activated.  So I only get one object as an argument but it's not a parameter, it's in the e.Argument object.  Here is a sample of importing information for a pharmacy dataset.

    Code Snippet

    void ImportPharmacy(object sender, DoWorkEventArgs e)
                BackgroundWorker worker = sender as BackgroundWorker;
                string m_filePath = e.Argument.ToString();
                DataSets.Pharmacy2000 m_pharmacyDataSet = new DataSets.Pharmacy2000();
                TextImporters.Pharmacy2000 m_pharmacyImporter = new TextImporters.Pharmacy2000();
                worker.ReportProgress(0, "Reading Pharmacy Data...");
                object[][] m_fileData = TextImporters.QSTextFiles.ReadTextFile(m_filePath);
                worker.ReportProgress(0, "Adding Pharmacy Records...");
                m_pharmacyImporter.PrepareAndAddRecords(m_pharmacyDataSet, m_fileData);
                DataSets.DatabaseConnector m_databaseConnector = new DataSets.DatabaseConnector();
                worker.ReportProgress(0, "Updating Pharmacy Database...\r\n");
                m_databaseConnector.Insert(ref DatabaseConnection, m_pharmacyDataSet);

    I have a namespace for the DataSets and one for the procedures that import the flat text files (DataSets, TextImporters).  Each of those has a class for each type of data.  In this case the data is about a pharmacy.  However, what if I wanted to make this generic and be able to get it to do any dataset class.  I would pass the type of the dataset to the method via the e.Argument along with the filepath.  I'm thinking of using an array of strings to accomplish this.  But I would need to make my local variables of that type.  So this line:

    Code Snippet

    DataSets.Pharmacy2000 m_pharmacyDataSet = new DataSets.Pharmacy2000();

    to something like:

    mytype m_dataSet = new mytype();

    where mytype was defined higher up possibly by the following where m_argumentString came from the e.Argument:

    Type mytype = typeof(m_argumentString)

    There would need to be several other lines which would be similar as well.

    Am I way off in my thinking or is this even possible?  I think I could do it with generics if I wasn't stuck with the specific parameters.  Any help would be greatly apreciated.  Thanks in advance!

    Friday, August 17, 2007 9:09 PM