重写Child类中的ParentModel类方法

本文关键字:ParentModel 类方法 Child 重写 | 更新日期: 2023-09-27 17:59:29

我有Parent Model类和viewModel类。Parent Model类有GetViewModel()方法,我正试图继承Parent类并重写Child类中的GetViewModel。然而,它抛出了一个无限的异常。

父ViewModel类:

public class ParentViewModel
{
    public IEnumerable<string> Choices { get; set; }
    public object Value { get; set; }
    public IMetaField MetaField { get; set; }
    public bool IsRequired { get; set; }
    public bool HasOtherChoice { get; set; }
    public string ValidationAttributes { get; set; }
    public string RequiredViolationMessage { get; set; }
    public string CssClass { get; set; }
}

父模型类:

public class ChildModel : ParentModel
{
    public string CorticonEntityName { get; set; }
    public string CorticonFieldName { get; set; }
    public override object GetViewModel(object value, IMetaField metaField)
    {
        this.Value = value;
        return (object)new ChildViewModel()
        {
            Choices = this.DeserializeChoices(),
            Value = value,
            MetaField = this.MetaField,
            HasOtherChoice = this.HasOtherChoice,
            IsRequired = (this.ValidatorDefinition.Required.HasValue && this.ValidatorDefinition.Required.Value),
            ValidationAttributes = this.BuildValidationAttributesString(),
            RequiredViolationMessage = this.ValidatorDefinition.RequiredViolationMessage,
            CssClass = this.CssClass
        };
    }
}

我创建了两个子类ChildModel类和ChildViewModel类,它们继承了父类。

我想覆盖父类的GetViewModel(),所以我已经完成了下面的操作,并且工作得很好。

ChildViewModel类:

public class ChildViewModel:ParentViewModel
{
    public string CorticonEntityName { get; set; }
    public string CorticonFieldName { get; set; }
}

ChildModel类:

public class ChildModel : ParentModel
{
    public string CorticonEntityName { get; set; }
    public string CorticonFieldName { get; set; }
    public override object GetViewModel(object value, IMetaField metaField)
    {
        this.Value = value;
        return (object)new ChildViewModel()
        {
            Choices = this.DeserializeChoices(),
            Value = value,
            MetaField = this.MetaField,
            HasOtherChoice = this.HasOtherChoice,
            IsRequired = (this.ValidatorDefinition.Required.HasValue && this.ValidatorDefinition.Required.Value),
            ValidationAttributes = this.BuildValidationAttributesString(),
            RequiredViolationMessage = this.ValidatorDefinition.RequiredViolationMessage,
            CssClass = this.CssClass,
            CorticonEntityName=this.CorticonEntityName,
            CorticonFieldName=this.CorticonFieldName
        };
    }
}

但我不想像父类GetViewModel()那样再次设置子类GetViewMode()中的所有属性。我想重用Parent类方法并在Child类中设置新的属性,所以我尝试了下面的方法,但它抛出了无限循环。

ChildViewModel类:

public class ChildViewModel:ParentViewModel
{
    public string CorticonEntityName { get; set; }
    public string CorticonFieldName { get; set; }
    CheckboxesFieldViewModel baseObject { get; set; }
}

ChildModel类:

public class ChildModel : ParentModel
{
    public string CorticonEntityName { get; set; }
    public string CorticonFieldName { get; set; }
    CheckboxesFieldViewModel baseObject { get; set; }
    public override object GetViewModel(object value, IMetaField metaField)
    {
        object objBase=this.GetViewModel(value,metaField);
        return (object)new ChildViewModel ()
        {
            objParent=objBase,
            CorticonEntityName = this.CorticonEntityName,
            CorticonFieldName = this.CorticonFieldName
        };
    }
}

如何在不重新设置属性的情况下重用父类GetViewModel()。

重写Child类中的ParentModel类方法

罪魁祸首是:

object objBase=this.GetViewModel(value,metaField);

你一遍又一遍地递归调用同一个方法,破坏了堆栈。如果要调用父方法,则需要使用base关键字:

object objBase = base.GetViewModel(value, metaField);