How do I create a spring .Net standalone object of type Int32 defined in the IOC context file?

StackOverflow https://stackoverflow.com/questions/17437040

  •  02-06-2022
  •  | 
  •  

Question

Kind of a simple newbie question...I see how I can create a string object, but how would I create an int object?

Here is the xml code fragment from my context file:

<object id="myString" type="System.String">
  <constructor-arg value="foo" />    
</object>
<object id="myInt" type="System.Int32">
   <<<**** how do I set this ****>>>>
</object>
Was it helpful?

Solution

Try this:

<object id="MyInt" type="System.Int32" factory-method="Copy">
  <constructor-arg index="0">
    <value>123</value>
  </constructor-arg>
</object>

OTHER TIPS

Try this:

<object id="MyInt" type="System.Int32" factory-method="Parse">
  <constructor-arg index="0">
    <value>123</value>
  </constructor-arg>
</object>

For creating an object of primitive type System.Int32, you must use the factory-method="Parse". The attribute factory-method="Copy" doesn't works because it not exists in the type System.Int32 and you have to use a static method to do it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top