3

我正在使用 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:在“配置 [C:\Users\username\some\path\TaskExecutorDemo\bin\Debug\TaskExecutorDemo.exe.Config#spring/objects] 第 7 行中创建名称为“weeklyCalendar”的对象时出错':对象初始化失败:无法将 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.NETISet正确配置我的属性?

4

2 回答 2

2

Setxml-config 中的部分创建Spring.Collections.ISet对象

尝试这个:

<object id="weeklyCalendar" type="My.Package.TaskExecutorDemo.WeeklyCalendarFactoryObject, TaskExecutorDemo">
  <property name="DaysOfWeekExcluded">
    <object type="System.Collections.Generic.HashSet&lt;System.DayOfWeek>">
      <constructor-arg name="collection" type="System.Collections.Generic.IEnumerable&lt;System.DayOfWeek>">
        <list element-type="System.DayOfWeek">
          <value>Friday</value>
          <value>Saturday</value>
          <value>Sunday</value>
        </list>
      </constructor-arg>
    </object>
  </property>
</object>
于 2013-08-13T11:39:59.937 回答
2

根据不支持设置通用集合值ISet<T>的文档;仅支持通用集合IDictionary<TKey, TValue>IList<T>。尝试HashSet<T>使用带有IEnumerable<T>.

于 2013-08-13T12:14:03.700 回答