我有一个包含几个自定义表单元素的表单类。
我有一个实体对象,它为元素提供特定属性,这些是从 XML 文件中解析的。所有元素都将实体作为其构造函数中的参数,但之后在其构造函数中需要不同的参数。
对于元素创建,我目前使用如下 switch 语句。但是,我想将其转换为使用 Activator.CreateInstance。但是,在构造函数中使用不同的参数,我知道处理此问题的唯一方法是创建一个包含所有参数的 DTO,将其传递给构造函数,然后让每个构造函数请求它需要的任何信息。我想要一个替代方案,因为我的理解是 DTO 在现代实施中是不受欢迎的。
switch (entity.GetPropertyValue("Class"))
{
case "FormCheckBox":
newElement = new FormCheckBox(entity, BaseElementHeight);
break;
case "RowSeparator":
newElement = new RowSeperator(entity, RowHeight, _mainCanvas);
break;
case "FormLabel":
newElement = new FormLabel(entity, BaseElementHeight);
break;
case "FormEditBox":
newElement = new FormEditBox(entity, _mainCanvas);//, BaseElementHeight, 600);
break;
case "FormComboBox":
newElement = new FormComboBox(entity, BaseElementHeight);
break;
case "FormTextBox":
newElement = new FormTextBox(entity, BaseElementHeight, TextFontSize, MaxFontBoxSize);
break;
default:
return null;
}
有没有人对此有想法和/或想法?