Search code examples
c#springspring.net

How to instantiate a type with many constructors with the same amount of parameters in Spring.Net


I want to instantiate a System.Net.IPEndPoint with Spring.Net. Here is my xml code:

<object id="MulticastAddress" type="System.Net.IPAddress" factory-method="Parse">
  <constructor-arg value="239.239.239.1"/>
</object>
<object id="DestinationEndPoint" type="System.Net.IPEndPoint">
  <constructor-arg name="address" ref="MulticastAddress"/>
  <constructor-arg name="port" value="2010"/>
</object>

But this causes a Spring.Core.TypeMismatchException with additional information: Cannot convert property value of type [System.Net.IPAddress] to required type [System.Int64] for property ''.

IPEndPoint has two constructors:

public IPEndPoint(long address, int port);
public IPEndPoint(IPAddress address, int port);

It seems that spring uses the first constructor which is actually a bad idea.

So how can I tell spring that it should use the second constructor?

Update :

I figured out how to convert the IPAddress into a long:

<object id="DestinationEndPoint" type="System.Net.IPEndPoint">
  <constructor-arg name="address" expression="T(System.Net.IPAddress).Parse('239.239.239.1')"/>
  <constructor-arg name="port" value="2010"/>
</object>

But now I got the exception: Cannot convert property value of type [System.Int64] to required type [System.Net.IPAddress] for property ''.

Now it seems that Spring uses the other constructor. What is it!?


Solution

  • I found the solution by my self. Spring.Net is throwing this exceptions if a constructor does not fit to the passed arguments. But Spring.Net is catching this exception later and tries the next constructor. So my problem was that I has to tell VisualStudio that it shouldn't break by this exception -_-.