我有几个基于标准抽象类的类,它们将从数据表中加载(对于示例,我使用了一个 int 数组)。该类将按类型启动,然后加载该类的特定详细信息。目前我正在使用 switch 语句执行此操作,但是否可以通过其他方式执行此操作?
class Program
{
static void Main(string[] args)
{
var list = new[] {1, 1, 3, 2, 2, 4};
TypeBase typeClass = null;
foreach (var i in list)
{
switch (i)
{
case 1:
{
typeClass = new Type1();
break;
}
case 2:
{
typeClass = new Type2();
break;
}
case 3:
{
typeClass = new Type3();
break;
}
}
}
if (typeClass != null)
{
typeClass.LoadDetails();
}
}
}
public class TypeBase
{
public int Type { get; set; }
public virtual void LoadDetails()
{
throw new NotImplementedException();
}
}
public class Type1 : TypeBase
{
public override void LoadDetails()
{
// Load type Specific details
}
}
public class Type2 : TypeBase
{
public override void LoadDetails()
{
// Load type Specific details
}
}
public class Type3 : TypeBase
{
public override void LoadDetails()
{
// Load type Specific details
}
}