G2DCreateInstanceDialog.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using CommonLang.Property;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Windows.Forms;
  7. namespace CommonFroms.G2D
  8. {
  9. public class G2DCreateInstanceDialog : G2DListSelectEditor<G2DCreateInstanceDialog.ClassDefine>
  10. {
  11. private G2DCreateInstanceDialog(List<G2DCreateInstanceDialog.ClassDefine> list, ClassDefine selected)
  12. : base(list, selected)
  13. {
  14. }
  15. public static object ShowCreateInstanceDialog(Type type, IWin32Window owner=null)
  16. {
  17. if (type.IsAbstract || type.IsInterface)
  18. {
  19. List<Type> types = ReflectionUtil.GetNoneVirtualSubTypes(type);
  20. List<ClassDefine> defines = new List<ClassDefine>();
  21. foreach (Type subType in types)
  22. {
  23. defines.Add(new ClassDefine(subType));
  24. }
  25. G2DCreateInstanceDialog dialog = new G2DCreateInstanceDialog(defines, null);
  26. if (dialog.ShowDialog(owner) == DialogResult.OK)
  27. {
  28. ClassDefine define = dialog.SelectedObject;
  29. return ReflectionUtil.CreateInstance(define.ValueType);
  30. }
  31. return null;
  32. }
  33. else
  34. {
  35. return ReflectionUtil.CreateInstance(type);
  36. }
  37. }
  38. public class ClassDefine
  39. {
  40. public Type ValueType { get; private set; }
  41. public DescAttribute Desc { get; private set; }
  42. public ClassDefine(Type subType)
  43. {
  44. ValueType = subType;
  45. Desc = PropertyUtil.GetAttribute<DescAttribute>(subType);
  46. }
  47. public override string ToString()
  48. {
  49. if (Desc != null)
  50. {
  51. return Desc.Desc;
  52. }
  53. else
  54. {
  55. return ValueType.Name;
  56. }
  57. }
  58. }
  59. }
  60. }