Вы можете создать универсальную функцию для создания:
private static Create<T>(string name, string text, double width, double height) where T: Control, new()
{
return new T { Name = name, Content = text, Width = width, Height = height }
}
Ваш переключатель становится:
switch (element.Attribute("Type").Value) {
case "System.Windows.Forms.Label" : return Create<System.Windows.Forms.Label>(name, text, width, height);
etc.
}
Вы также можете адаптировать это для передачи в XElement, в зависимости от того, что вы предпочитаете.
Если атрибут Type - это всегда имя System.Type, которое вы хотите, тогда вы можете просто сделать
Control ctrl = (Control) Activator.CreateInstance(Type.GetType(element.Attribute("Type").Value));
ctrl.Name = name;
etc.
Если между значением атрибута и желаемым типом есть сопоставление один к одному, вы можете объявить статическое поле только для чтения с отображением:
private static readonly uiTypeMapping = new Dictionary<string,Type> {
{ "System.Windows.Forms.Label", typeof(System.Windows.Controls.Label) },
{ "System.Windows.Forms.Button", typeof(System.Windows.Controls.Button) },
{ etc. }
};
И используйте
UIElement elem = (UIElement) Activator.CreateInstance(uiTypeMapping[element.Attribute("Type").Value]);
etc.