在C#中传递未知数据类型参数

问题描述 投票:1回答:1

我的主程序正在通过另一个类读取电子表格,该类对电子表格的模式(即其中的数据类型)一无所知。我的方法是定义一个定义这些数据类型的电子表格记录,并将该记录作为类或结构传递到被调用的类中,以读取该电子表格。

问题在于,C#编译器无法将main的SpreadsheetRecord数据类型隐式转换为被调用类所知道的一种。当然不能,因为目标类对此数据类型一无所知。那么,如何将电子表格的模式传递给负责读取和保存电子表格数据的类例程?

void class Main
{
    public class SpreadsheetRecord
    {
        public double volAvg;
        public double volOvr10;
        public double sumScore;
    }

    static string[] sheetHeads = { "Volume (10 Day Avg)", "Volume (Today/Avg 10 Day)",
            "Equity Summary Score from StarMine from Refinitiv" };

    SpreadsheetData sheetDat = new SpreadsheetData(new SpreadsheetRecord(), sheetHeads);
    ...
}


public class SpreadsheetData //SpreadsheetData parses an "unknown" spreadsheet.xls file
{
    public Dictionary<string, Record> SheetDB { get; private set; } //declaration of database
    public class Record { };  //schema for incoming spreadsheet data record

    public SpreadsheetData(Record schemaRecord, string[] recordHeadings) //constructor read in spreadsheet
    {
        ...
        using (IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(sheetInputFile.OpenRead()))
        {...
            FieldInfo[] recordFieldInfo = typeof(Record).GetFields();
            for (int i = 1; i < result.Tables[0].Rows.Count; i++)
            {
                for (int j = 0; j < recordHeadings.Length; j++)
                    recordFieldInfo[j].SetValue(schemaRecord, sheet1.Rows[i][Column2RecordCrossIndx[j]]);
                SheetDB.Add(sheet1.Rows[i][indxOfSymbol].ToString(), schemaRecord); //store sheet data record
            }
            ...
        }
    }
}
c# class reflection parameter-passing generic-programming
1个回答
0
投票

嗯,采用通用类似乎可以达到将未知数据类型(即数据库记录)输入电子表格读取类的目的。谢谢你欢迎其他任何建议。

void class Main
{
    public class SpreadsheetRecord
    {
        public double volAvg;
        public double volOvr10;
        public double sumScore;
    }

    static string[] sheetHeads = { "Volume (10 Day Avg)", "Volume (Today/Avg 10 Day)",
            "Equity Summary Score from StarMine from Refinitiv" };

    SpreadsheetData<SpreadsheetRecord> sheetDat = new SpreadsheetData<SpreadsheetRecord>(sheetHeads);
    ...
}


public class SpreadsheetData<T> where T : new() //SpreadsheetData parses an "unknown" spreadsheet.xls file
{
    public Dictionary<string,T> SheetDB { get; private set; } //allow read access to database

    public SpreadsheetData<T>(string[] recordHeadings) //constructor to read in spreadsheet
    {
        ...
        using (IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(sheetInputFile.OpenRead()))
        {...
            SheetDB = new Dictionary<string,T>(sheet1.Rows.Count); //create database instance
            T schemaRecord = new T(); //create database record instance

            FieldInfo[] recordFieldInfo = typeof(T).GetFields();
            for (int i = 1; i < result.Tables[0].Rows.Count; i++)
            {
                for (int j = 0; j < recordHeadings.Length; j++)
                    recordFieldInfo[j].SetValue(schemaRecord, sheet1.Rows[i][Column2RecordCrossIndx[j]]);
                SheetDB.Add(sheet1.Rows[i][indxOfSymbol].ToString(), schemaRecord); //store sheet data record
            }
            ...
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.