c# - Spring.NET 无法转换为类型 Spring.Collections.Set

标签 c# spring.net

我正在使用 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: 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'.

但我没有在我的代码中的任何地方引用 Spring.Collections.ISet。我如何让 Spring.NET 正确配置我的 ISet 属性?

最佳答案

根据 setting generic collection values 上的文档ISet<T> 不支持;只有通用集合 IDictionary<TKey, TValue>IList<T>得到支持。尝试通过显式配置显式 HashSet<T>使用 constructor taking an IEnumerable<T> .

关于c# - Spring.NET 无法转换为类型 Spring.Collections.Set,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18189295/

相关文章:

asp.net-mvc - Spring.Net,Nhibernate,ASP.NET MVC3 CannotLoadObjectTypeException

c# - 用于登录 ASP.NET 网站的控制台应用程序

c# - 将激活的事件添加到 Toast 通知时出错

c# - 我可以子类化一个抽象类,它有另一个抽象类也被子类化吗? (C#)

c# - 根据条件从 DataTable 中删除行

c# - 为什么我的系统试图从无效版本的程序集加载类型?

c# - 绘制图像时 : System. Runtime.InteropServices.ExternalException: GDI 中发生一般性错误

.net - 使用 SpecFlow、White、Spring 和 MS Test 测试 WinForms/CompositeUI GUI

Nhibernate + Spring.Net + Transaction + 太多线程

c# - 如何将 WebApi 2 与 Spring.Net 集成