123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- using CommonLang.Property;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- namespace CommonFroms.G2D
- {
- public class G2DCreateInstanceDialog : G2DListSelectEditor<G2DCreateInstanceDialog.ClassDefine>
- {
- private G2DCreateInstanceDialog(List<G2DCreateInstanceDialog.ClassDefine> list, ClassDefine selected)
- : base(list, selected)
- {
- }
- public static object ShowCreateInstanceDialog(Type type, IWin32Window owner=null)
- {
- if (type.IsAbstract || type.IsInterface)
- {
- List<Type> types = ReflectionUtil.GetNoneVirtualSubTypes(type);
- List<ClassDefine> defines = new List<ClassDefine>();
- foreach (Type subType in types)
- {
- defines.Add(new ClassDefine(subType));
- }
- G2DCreateInstanceDialog dialog = new G2DCreateInstanceDialog(defines, null);
- if (dialog.ShowDialog(owner) == DialogResult.OK)
- {
- ClassDefine define = dialog.SelectedObject;
- return ReflectionUtil.CreateInstance(define.ValueType);
- }
- return null;
- }
- else
- {
- return ReflectionUtil.CreateInstance(type);
- }
- }
- public class ClassDefine
- {
- public Type ValueType { get; private set; }
- public DescAttribute Desc { get; private set; }
- public ClassDefine(Type subType)
- {
- ValueType = subType;
- Desc = PropertyUtil.GetAttribute<DescAttribute>(subType);
- }
- public override string ToString()
- {
- if (Desc != null)
- {
- return Desc.Desc;
- }
- else
- {
- return ValueType.Name;
- }
- }
- }
- }
- }
|