According to the docs on setting generic collection values is not supported for ISet<T>
; only the generic collections IDictionary<TKey, TValue>
and IList<T>
are supported. Try passing an explicit configuring an explicit HashSet<T>
using the constructor taking an IEnumerable<T>
.
Spring.NET Unable to cast to type Spring.Collections.Set
-
24-06-2022 - |
Question
I'm using Spring.NET to configure some objects, and I've written a FactoryObject to make configuring a Quartz.NET calendar bearable.
It has a property shown below, that of course we expect to configure with 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 ...
}
}
It is configured by the following object definition.
<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>
But at start up this throws the following exception:
Spring.Objects.Factory.ObjectCreationException: Error creating object with name 'weeklyCalendar' defined in 'config [C:\Users\username\some\path\TaskExecutorDemo\bin\Debug\TaskExecutorDemo.exe.Config#spring/objects] line 7' : Initialization of object failed : Unable to cast object of type System.Collections.Generic.HashSet`1[System.DayOfWeek]' to type 'Spring.Collections.ISet'.
System.InvalidCastException: Unable to cast object of type 'System.Collections.Generic.HashSet`1[System.DayOfWeek]' to type 'Spring.Collections.ISet'.
But I don't reference Spring.Collections.ISet
anywhere in my code. How do I get Spring.NET to configure my ISet
property correctly?
Solution
OTHER TIPS
Set
section in your xml-config creates Spring.Collections.ISet
object
Try this:
<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>