首页 文章

Spring.Core.TypeMismatchException:无法转换Dictionary类型的属性值

提问于
浏览
0

我正在努力解决问题,非常感谢一些帮助 .

public interface ISolver<T>
{
        void Solve();
}

public class Solver : ISolve<IEntity>
{
  void Solve()
{
...code that solves the given problem
}
}

 public class Client : IClient<IEntity>
{

        public Dictionary<string, ISolver<IEntity>> Solvers { get; set; }
}

All are part of namespace solution.

在Spring.Net配置中:

<object id="Client" type="solution.Client, solution">
<property name="Solvers">
      <dictionary key-type="string" value-type="solution.Solver, solution">
        <entry  key="SolverMp" value-ref="SolverMp"></entry>
      </dictionary>
    </property>
</object>

<object id="SolverMp" type="solution.Solver, solution">
</object>

我得到了例外:

Spring.Core.TypeMismatchException:无法转换类型的属性值[System.Collections.Generic.Dictionary2 [[System.String,mscorlib,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089],[solution.Solver] ,solution,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null]]]到必需的类型[System.Collections.Generic.Dictionary2 [[System.String,mscorlib,Version = 4.0.0.0,Culture = neutral, PublicKeyToken = b77a5c561934e089],[solution.ISolver`1 [[solution.IEntity,solution,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null]],solution,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null ]]]属性'Solvers' .

2 回答

  • 1
    <objects xmlns="http://www.springframework.net">
      <object id="Client" type="solution.Client, solution">
        <property name="Solvers">
          <dictionary key-type="string" value-type="solution.ISolver&lt;solution.IEntity&gt;, solution">
            <entry key="SolverMp" value-ref="SolverMp"></entry>
          </dictionary>
        </property>
      </object>
    </objects>
    
  • 3

    至少有一个问题是您的配置中的此条目:

    value-type="solution.Solver
    

    Solvers字典的值类型是

    ISolver<IEntity>>
    

    这就是异常所抱怨的 .

相关问题