获取所有后代类型的基类

use*_*827 0 asp.net devexpress xaf

我有一个被调用的基类BaseEvent和几个后代类:

public class BaseEvent {
    // the some properties
    // ...
}

[MapInheritance(MapInheritanceType.ParentTable)]
public class Film : BaseEvent {
   // the some properties
   // ...
}
[MapInheritance(MapInheritanceType.ParentTable)]
public class Concert : BaseEvent {
    // the some properties
    // ...
}
Run Code Online (Sandbox Code Playgroud)

我有一个代码BaseEvent在运行时创建实例:

BaseEvent event = new BaseEvent();
// assign values for a properties
// ...    
baseEvent.XPObjectType = Database.XPObjectTypes.SingleOrDefault(
    t => t.TypeName == "MyApp.Module.BO.Events.BaseEvent");
Run Code Online (Sandbox Code Playgroud)

现在,此事件将在BaseEvent列表视图中显示.

我想执行以下操作:当用户单击Edit按钮然后在列表视图查找字段中显示所有后代类型.当用户将记录更改保存ObjectType到选定值时.

我怎样才能做到这一点?
谢谢.

PS.这是asp.net应用程序.

sha*_*p00 5

我不确定你的方法对于你想要实现的目标是否正确.首先,我会回答你问的问题,事后我会试着解释如何XAF已经提供你正在努力实现的功能,即如何选择从用户界面创建该记录的子类.

为了创建允许用户Type在应用程序中选择的属性,可以声明TypeConverter:

public class EventClassInfoTypeConverter : LocalizedClassInfoTypeConverter
{
    public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
    {
        List<Type> values = new List<Type>();
        foreach (ITypeInfo info in XafTypesInfo.Instance.PersistentTypes)
        {
            if ((info.IsVisible && info.IsPersistent) && (info.Type != null))
            {
                // select BaseEvent subclasses
                if (info.Type.IsSubclassOf(typeof(BaseEvent))) 
                    values.Add(info.Type);
            }
        }
        values.Sort(this);
        values.Insert(0, null);
        return new TypeConverter.StandardValuesCollection(values);
    }
}
Run Code Online (Sandbox Code Playgroud)

然后你的基本事件类看起来像:

public class BaseEvent: XPObject
{
    public BaseEvent(Session session)
        : base(session)
    { }

    private Type _EventType;
    [TypeConverter(typeof(EventClassInfoTypeConverter))]
    public Type EventType
    {
        get
        {
            return _EventType;
        }
        set
        {
            SetPropertyValue("EventType", ref _EventType, value);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,我怀疑这不是您需要的功能.修改属性的值不会更改记录的基本类型.也就是说,你最终会得到一个类型的记录,BaseEvent其属性Type等于'Concert'或'Film'.

XAF已经提供了一种机制来选择要创建的记录类型.在您的场景中,您会发现该New按钮是一个下拉列表,其中包含不同的子类作为选项:

在此输入图像描述

因此,您无需在对象中创建"类型"属性.如果需要列在列表视图中显示事件类型,则可以按如下方式声明属性

[PersistentAlias("XPObjectType.Name")]
public string EventType
{
    get
    {
        return base.ClassInfo.ClassType.Name;
    }
}
Run Code Online (Sandbox Code Playgroud)