S#arp架构模型绑定器枚举错误
-
07-07-2019 - |
题
我收到SharpArchitecture模型绑定器的错误。 “收集被修改;枚举操作可能不会执行。 (底部的堆栈跟踪)。
用于创建和编辑 SettingsModel 的MVC页面会抛出错误。当我们升级到S#arp Architecture 1.0的发布版本时,就开始发生这种情况。我的班级有一些列表作为属性。其中一个列表包含其他列表作为属性的类。我不确定哪个List会抛出错误。任何人都可以提供方向来指导如何解决这个问题,或者在我的模型中寻找可能导致它的东西吗?
这是我的SettingsModel类:
public class SettingsModel : Entity
{
public SettingsModel()
{
AttributeSettingsList = new List<AttributeSettingsModel>();
}
public virtual void AddAttributeSettings(AttributeSettingsModel attSettings)
{
AttributeSettingsList.Add(attSettings);
attSettings.Settings = this;
}
[NotNullNotEmpty(Message = "Description must be provided")]
public virtual string Description { get; set; }
[DomainSignature]
[Range(0, 100, Message = "ModelAPercentage must be between 0 and 100")]
public virtual int ModelAPercentage { get; set; }
[DomainSignature]
[Range(0, 100, Message = "ModelBPercentage must be between 0 and 100")]
public virtual int ModelBPercentage { get; set; }
public virtual IList<AttributeSettingsModel> AttributeSettingsList { get; set; }
public virtual IList<EntityMappingModel> EntityMappingList { get; set; }
public SettingsModel(string Description, int ModelAPercentage, int ModelBPercentage)
: this()
{
this.Description = Description;
this.ModelAPercentage = ModelAPercentage;
this.ModelBPercentage = ModelBPercentage;
}
}
这是抛出错误的SharpArchitecture方法:
private void SetEntityCollectionProperty(ModelBindingContext bindingContext,
PropertyDescriptor propertyDescriptor, object value) {
if (value as IEnumerable != null &&
IsSimpleGenericBindableEntityCollection(propertyDescriptor.PropertyType)) {
object entityCollection = propertyDescriptor.GetValue(bindingContext.Model);
Type entityCollectionType = entityCollection.GetType();
foreach (object entity in (value as IEnumerable)) {
entityCollectionType.InvokeMember("Add",
BindingFlags.Public | BindingFlags.Instance | BindingFlags.InvokeMethod, null, entityCollection,
new object[] { entity });
}
}
}
这是堆栈跟踪:
[InvalidOperationException:Collection已被修改;枚举操作可能无法执行。] System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)+51 System.Collections.Generic.Enumerator.MoveNextRare()+ 7661017 System.Collections.Generic.Enumerator.MoveNext()+61 SharpArch.Web.ModelBinder.SharpModelBinder.SetEntityCollectionProperty(ModelBindingContext bindingContext,PropertyDescriptor propertyDescriptor,Object value)+358 SharpArch.Web.ModelBinder.SharpModelBinder.SetProperty(ControllerContext controllerContext,ModelBindingContext bindingContext,PropertyDescriptor propertyDescriptor,Object value)+61 System.Web.Mvc.DefaultModelBinder.BindProperty(ControllerContext controllerContext,ModelBindingContext bindingContext,PropertyDescriptor propertyDescriptor)+265 SharpArch.Web.ModelBinder.SharpModelBinder.BindProperty(ControllerContext controllerContext,ModelBindingContext bindingContext,PropertyDescriptor propertyDescriptor)+225 System.Web.Mvc.DefaultModelBinder.BindProperties(ControllerContext controllerContext,ModelBindingContext bindingContext)+125 System.Web.Mvc.DefaultModelBinder.BindComplexElementalModel(ControllerContext controllerContext,ModelBindingContext bindingContext,Object model)+293 System.Web.Mvc.DefaultModelBinder.BindComplexModel(ControllerContext controllerContext,ModelBindingContext bindingContext)+772 System.Web.Mvc.DefaultModelBinder.BindModel(ControllerContext controllerContext,ModelBindingContext bindingContext)+345 SharpArch.Web.ModelBinder.SharpModelBinder.BindModel(ControllerContext controllerContext,ModelBindingContext bindingContext)+39 System.Web.Mvc.DefaultModelBinder.UpdateCollection(ControllerContext controllerContext,ModelBindingContext bindingContext,Type elementType)+408 System.Web.Mvc.DefaultModelBinder.BindComplexModel(ControllerContext controllerContext,ModelBindingContext bindingContext)+756 System.Web.Mvc.DefaultModelBinder.BindModel(ControllerContext controllerContext,ModelBindingContext bindingContext)+345 SharpArch.Web.ModelBinder.SharpModelBinder.BindModel(ControllerContext controllerContext,ModelBindingContext bindingContext)+39 System.Web.Mvc.DefaultModelBinder.BindProperty(ControllerContext controllerContext,ModelBindingContext bindingContext,PropertyDescriptor propertyDescriptor)+231 SharpArch.Web.ModelBinder.SharpModelBinder.BindProperty(ControllerContext controllerContext,ModelBindingContext bindingContext,PropertyDescriptor propertyDescriptor)+225 System.Web.Mvc.DefaultModelBinder.BindProperties(ControllerContext controllerContext,ModelBindingContext bindingContext)+125 System.Web.Mvc.DefaultModelBinder.BindComplexElementalModel(ControllerContext controllerContext,ModelBindingContext bindingContext,Object model)+293 System.Web.Mvc.DefaultModelBinder.BindComplexModel(ControllerContext controllerContext,ModelBindingContext bindingContext)+772 System.Web.Mvc.DefaultModelBinder.BindModel(ControllerContext controllerContext,ModelBindingContext bindingContext)+345
解决方案
在尖锐的架构SetEntityCollectionProperty方法中似乎存在一个错误。它在最后四行:
foreach (object entity in (value as IEnumerable)) {
entityCollectionType.InvokeMember("Add",
BindingFlags.Public | BindingFlags.Instance | BindingFlags.InvokeMethod, null, entityCollection,
new object[] { entity });
}
它的意图似乎是从作为值传入的IEnumerable中的值填充模型集合类(entityCollection)。但是,这两个对象是相同的参考。它已经填充了。当“添加”时调用方法,修改entityCollection,它与正在迭代的对象相同。然后抛出异常。
评论或删除该代码块,您的代码应该运行。
其他提示
注释这段代码会影响S#arp的其他功能吗?