我有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()那样在子类GetViewModel()中设置所有属性。 我想重用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
};
}
}
如何在不重新设置属性的情况下重用Parent类GetViewModel()。
答案 0 :(得分:1)
你的罪魁祸首是:
object objBase=this.GetViewModel(value,metaField);
你一遍又一遍地递归调用相同的方法,炸毁堆栈。如果要调用父方法,则需要使用base
关键字:
object objBase = base.GetViewModel(value, metaField);