优化大型 switch 语句
我有一个大型 switch 语句,其中我根据 XElement 的输入值创建 UIElement:
public static UIElement CreateElement(XElement element) {
var name = element.Attribute("Name").Value;
var text = element.Attribute("Value").Value;
var width = Convert.ToDouble(element.Attribute("Width").Value);
var height = Convert.ToDouble(element.Attribute("Height").Value);
//...
switch (element.Attribute("Type").Value) {
case "System.Windows.Forms.Label":
return new System.Windows.Controls.Label() {
Name = name,
Content = text,
Width = width,
Height = height
};
case "System.Windows.Forms.Button":
return new System.Windows.Controls.Button() {
Name = name,
Content = text,
Width = width,
Height = height
};
//...
default:
return null;
}
}
我正在创建很多这样的控件,正如您所看到的,正在进行太多重复。
有什么办法可以避免这种重复吗?预先感谢您的想法。
I have large switch statement in which I create UIElements based on input value from XElement:
public static UIElement CreateElement(XElement element) {
var name = element.Attribute("Name").Value;
var text = element.Attribute("Value").Value;
var width = Convert.ToDouble(element.Attribute("Width").Value);
var height = Convert.ToDouble(element.Attribute("Height").Value);
//...
switch (element.Attribute("Type").Value) {
case "System.Windows.Forms.Label":
return new System.Windows.Controls.Label() {
Name = name,
Content = text,
Width = width,
Height = height
};
case "System.Windows.Forms.Button":
return new System.Windows.Controls.Button() {
Name = name,
Content = text,
Width = width,
Height = height
};
//...
default:
return null;
}
}
I am creating a lot controls like this and as you can see, too much repetition is going on.
Is there some way to avoid this repetition? Thanks in advance for ideas.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以创建一个通用函数来执行以下操作:
您的开关然后变为:
您还可以调整它以传入 XElement,无论您喜欢哪种。
如果 Type 属性始终是您想要的 System.Type 的名称,那么您可以这样做
如果属性的值和您想要的类型之间存在一对一的映射,那么您可以使用以下方法声明一个只读静态字段映射:
并使用
You could create a generic function that does the create:
Your switch then becomes:
You could also adapt this to pass in the XElement, whichever you prefer.
If the Type attribute is always the name of the System.Type you want, then you could just do
If there's a one to one mapping between the value of the attribute and the type you want, then you can declare a readonly static field with the mapping:
And use
像这样的东西可以工作......:)
Something like this could work... :)
这些不同的控件具有继承树。例如,宽度、高度、名称是在 FrameworkElement 上定义的。因此,您可以执行如下操作:
请注意,通过使用这种方法,与 Flynn 的答案相比,您还可以轻松添加代码,例如“当控件是 ItemsControl 时,执行此操作”,即不适用于每种类型,但仅限于其中某些类型。
Those different controls have inheritance trees. So for example Width, Height, Name are defined on FrameworkElement. So you could do something like the following:
Note that by using this approach, in comparison to Flynn's answer, you can also easily add code such as "when the control is an ItemsControl, do this", i.e. code which won't apply to every type, but only to some of them.
可以用反射+表达式来实现。
要使其与字符串以外的其他类型一起使用,您可以使用 Expression.Convert。留作练习。
You can do it with reflection + expressions.
To make it work with other types then string, you can use Expression.Convert. Left as an exercise.
您可以使用反射来实现这一点,或者您可以创建一个字符串字典(您现在正在打开的字典)和您创建控件的 Funcs(或操作)。
对于您发布的特定代码,您可以在 switch 语句之后分配高度和宽度,因为它们直接存在于
Control
上。You could to this with reflection instead, or you could create a Dictionary of strings (what you're switching on now) and Funcs (or actions rather) where you create the controls.
for the specific code you posted, you can assign height and width after the switch statement, since they exsist on
Control
directly.