如何将参数传递给c#.net中的Generic类构造函数?

时间:2016-02-01 06:13:51

标签: c# generics

public class SampleFile<Toriginal> 
{
    public SampleFile(File filename,string sname)
    {
    }
}

如何将值传递给Samplefile构造函数?

1 个答案:

答案 0 :(得分:2)

File是一个静态类,您无法将其作为参数传递。

将其更改为string

public class SampleFile<Toriginal> 
{
    public SampleFile(string filename,string sname)
    {
         //Here you can use File as it is static
         var lines = File.ReadAllLines(filename);
    }
}

然后只需正常调用它:

var sample = new SampleFile<Type>(@"c:\hi.txt", "bye.txt");