【问题标题】:How to set enum Type indexer in Spring.NET configuration xml?如何在 Spring.NET 配置 xml 中设置枚举类型索引器?
【发布时间】:2009-11-04 22:34:06
【问题描述】:

我有这个代码:

public enum StateId { NotSet = 0, AL, ..., WY }

public class EnumBasedArray<I,V>:IEnumerable<V> 
{
  public V this[I index]
  { 
    get { return _data[index]; }
    set { _data[index] = value; }
  }
  // other code to manage values internally
}

public class AnotherObject { ... }

public class ArrayOfAnotherObjectByStateId:EnumBasedArray<StateId, AnotherObject> {}

我遇到的麻烦是通过配置 XML 文件告诉 Spring.NET StateId 索引数组中每个项目的值。

在代码中,我会这样写:

var x = new ArrayOfAnotherObjectByStateId();
x[StateId.AZ] = new AnotherObject(...);
x[StateId.CA] = new AnotherObject(...);

如何在 Spring xml 中执行此操作? 我最接近的是:

<object id="matrix" type="ArrayOfAnotherObjectByStateId">
   <property name="[AZ]" ref="AZ.Matrix">
</object>

<object id="AZ.Matrix" type="AnotherObject" />

这给了我错误 "创建上下文'spring.root'时出错:无法为指定上下文解析'AZ'节点"

【问题讨论】:

    标签: arrays enums spring.net indexer


    【解决方案1】:
    <object id="matrix" type="ArrayOfAnotherObjectByStateId">
       <property name="[T(NamespaceYouUse.StateId).AZ]" ref="AZ.Matrix">
    </object>
    

    使用 Spring.NET 1.2 测试

    【讨论】:

    • 谢谢你——工作就像一个魅力!比工厂解决方法干净得多(很多),这些解决方法是为了让 Spring 快乐而创建的大量对象。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-20
    • 2022-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多