在泛型方法中分配通过反射获得的实例变量

Assigning instance variables obtained through reflection in generic method

我有制表符分隔值 (TSV) 文本文件中的数据,我想读取这些数据并(最终)存储在数据库表中。对于 TSV 文件,每一行包含一条记录,但在一个文件中记录可以有 2 个字段,在另一个文件中有 4 个字段,等等。我编写了工作代码来处理 2 字段记录,但我认为这可能是一个很好的一个(或两个)通用方法的案例,而不是为每种记录编写新方法。但是,由于两个问题,我无法对此进行编码:我无法创建一个新对象来保存记录数据,而且我不知道如何使用反射来一般地填充我的对象的实例变量。

我看了其他几个类似的post,包括Datatable to object by using reflection and linq

下面是有效的代码(如果重要的话,这在 Windows 中)以及无效的代码。

public class TSVFile
{
    public class TSVRec
    {
        public string item1;
        public string item2;
    }

    private string fileName = "";

    public TSVFile(string _fileName)
    {
        fileName = _fileName;
    }

    public TSVRec GetTSVRec(string Line)
    {
        TSVRec rec = new TSVRec();

        try
        {
            string[] fields = Line.Split(new char[1] { '\t' });

            rec.item1 = fields[0];
            rec.item2 = fields[1];
        }
        catch (Exception ex)
        {
            System.Windows.Forms.MessageBox.Show("Bad import data on line: " + 
                Line + "\n" + ex.Message, "Error",
                System.Windows.Forms.MessageBoxButtons.OK,
                System.Windows.Forms.MessageBoxIcon.Error);
        }

        return rec;
    }

    public List<TSVRec> ImportTSVRec()
    {
        List<TSVRec> loadedData = new List<TSVRec>();
        using (StreamReader sr = File.OpenText(fileName))
        {
            string Line = null;
            while ((Line = sr.ReadLine()) != null)
            {
                loadedData.Add(GetTSVRec(Line));
            }
        }

        return loadedData;
    }

    // *** Attempted generic methods ***
    public T GetRec<T>(string Line)
    {
        T rec = new T();  // compile error!
        Type t = typeof(T);

        FieldInfo[] instanceVars = t.GetFields();

        string[] fields = Line.Split(new char[1] { '\t' });

        for (int i = 0; i < instanceVars.Length - 1; i++)
        {
            rec. ??? = fields[i];   // how do I finish this line???
        }

        return rec;
    }

    public List<T> Import<T>(Type t)
    {
        List<T> loadedData = new List<T>();
        using (StreamReader sr = File.OpenText(fileName))
        {
            string Line = null;
            while ((Line = sr.ReadLine()) != null)
            {
                loadedData.Add(GetRec<T>(Line));
            }
        }

        return loadedData;
    }
}

我看到了这条线 T rec = new T(); 在上面提到的post,但它对我不起作用...

如果可能的话,我将不胜感激关于如何使这项工作的任何建议。我想了解更多关于将反射与泛型一起使用的信息,所以我不仅想了解如何,还想了解原因。

我希望@EdPlunkett 把他的建议作为答案发布,而不是评论,这样我就可以将其标记为答案...

总而言之:做自己想做的事,不需要"Assigning instance variables obtained through reflection in generic method"。其实我不用泛型方法也能有泛型解决方案:

public class GenRec
{
    public List<string> items = new List<string>();
}

public GenRec GetRec(string Line)
{
    GenRec rec = new GenRec();

    try
    {
        string[] fields = Line.Split(new char[1] { '\t' });

        for (int i = 0; i < fields.Length; i++)
            rec.items.Add(fields[i]);
    }
    catch (Exception ex)
    {
        System.Windows.Forms.MessageBox.Show("Bad import data on line: " + Line + "\n" + ex.Message, "Error",
            System.Windows.Forms.MessageBoxButtons.OK,
            System.Windows.Forms.MessageBoxIcon.Error);
    }

    return rec;
}

public List<GenRec> Import()
{
    List<GenRec> loadedData = new List<GenRec>();
    using (StreamReader sr = File.OpenText(fileName))
    {
        string Line = null;

        while ((Line = sr.ReadLine()) != null)
            loadedData.Add(GetRec(Line));
    }

    return loadedData;
}

我刚刚对此进行了测试,效果非常好!

当然,这并不能帮助我学习如何编写泛型方法或使用反射,但我会接受...