动态地将带有字符串参数的类型实例化到构造函数

Ant*_*ony 1 .net c# c#-4.0

我有以下代码:

if (FileType == typeof(ScanUploadFile))
{
    files.Add(new ScanUploadFile(filePath));
}
if (FileType == typeof(FaxFile))
{
    files.Add(new FaxFile(filePath));
}
if (FileType == typeof(BulkScanFile))
{
    files.Add(new BulkScanFile(filePath));
}
if (FileType == typeof(SpecialCategoryFile))
{
    files.Add(new SpecialCategoryFile(filePath));
}
Run Code Online (Sandbox Code Playgroud)

如果没有IF声明我怎么写呢?

neo*_*pir 8

由于您只对构造函数感兴趣,因此您可以使用:

 Activator.CreateInstance(FileType, new string[] {filePath});
Run Code Online (Sandbox Code Playgroud)

ActivatorSystem库中定义.