Spring.NET无法强制转换为Spring.Collections.Set类型
本文关键字:Spring Collections Set 类型 转换 NET | 更新日期: 2023-09-27 18:27:25
我正在使用Spring.NET来配置一些对象,并且我已经编写了一个FactoryObject,以使配置Quartz.NET日历变得可以忍受。
它有一个如下所示的属性,当然我们希望使用Spring.NET 进行配置
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Quartz.Impl.Calendar;
using Spring.Objects.Factory;
namespace My.Package.TaskExecutorDemo
{
/// <summary>
/// TODO:
/// </summary>
public class WeeklyCalendarFactoryObject : WeeklyCalendar, IFactoryObject
{
private ISet<DayOfWeek> _daysOfWeek = new HashSet<DayOfWeek>();
public ISet<DayOfWeek> DaysOfWeekExcluded
{
get { return _daysOfWeek; }
set
{
if (value == null)
{
throw new ArgumentNullException("DaysOfWeekExcluded");
}
_daysOfWeek = value;
}
}
//Everything else ...
}
}
它由以下对象定义进行配置。
<object id="weeklyCalendar" type="My.Package.TaskExecutorDemo.WeeklyCalendarFactoryObject, TaskExecutorDemo">
<property name="DaysOfWeekExcluded">
<set element-type="System.DayOfWeek, mscorlib">
<value>Friday</value>
<value>Saturday</value>
<value>Sunday</value>
</set>
</property>
</object>
但在启动时,这引发了以下异常:
Spring.Objects.Factory.ObjectCreationException:创建名为"weeklyCalendar"的对象时出错,该对象在"config[C:''Users''username''some''path''TaskExecutitorDemo''bin''Debug''TaskExecutiorDemo.exe.config#Spring/Objects]行7"中定义:对象初始化失败:无法将System.Collections.Generic.HashSet"1[System.DayOfWeek]"类型的对象强制转换为"Spring.Collections.ISet".
System.InvalidCastException:无法将"System.Collections.Generic.HashSet `1[System.DayOfWeek]"类型的对象强制转换为"Spring.Collections.ISet".
但是我在代码中的任何地方都没有引用Spring.Collections.ISet
。如何让Spring.NET正确配置ISet
属性?
根据文档,ISet<T>
不支持设置泛型集合值;仅支持通用集合CCD_ 4和CCD_。尝试使用采用IEnumerable<T>
的构造函数传递显式配置显式HashSet<T>
。
Set
部分创建Spring.Collections.ISet
对象
试试这个:
<object id="weeklyCalendar" type="My.Package.TaskExecutorDemo.WeeklyCalendarFactoryObject, TaskExecutorDemo">
<property name="DaysOfWeekExcluded">
<object type="System.Collections.Generic.HashSet<System.DayOfWeek>">
<constructor-arg name="collection" type="System.Collections.Generic.IEnumerable<System.DayOfWeek>">
<list element-type="System.DayOfWeek">
<value>Friday</value>
<value>Saturday</value>
<value>Sunday</value>
</list>
</constructor-arg>
</object>
</property>
</object>