PDA

View Full Version : Factory method System.Type.GetType(string)


Drawer
08-25-2006, 05:13 PM
Hello all!

I have a problem with creating a type object by Spring.NET.

For configurations:

<object id="MyType" type="System.Type" factory-method="GetType">
<constructor-arg index="0" value="SomeClass, SomeAssembly" />
</object>

or

<object id="MyType" type="System.Type" factory-method="GetType">
<constructor-arg value="SomeClass, SomeAssembly" />
</object>

Spring creates RuntimeTypeHandle scructure, instead of Type.

For configs:
<object id="MyType" type="System.Type" factory-method="GetType">
<constructor-arg name="typeName" value="SomeClass, SomeAssembly" />
</object>

or

<object id="MyType" type="System.Type" factory-method="GetType">
<constructor-arg name="string" value="SomeClass, SomeAssembly" />
</object>

Spring throws exception: Cannot find matching factory method 'GetType' on Type [System.Type].

Does anybody know why?

.ben
08-26-2006, 12:35 PM
In the configuration file:


<object id="myObject" type="Spring.Objects.Factory.Config.MethodInvokingFactor yObject, Spring.Core">
<property name="TargetType" value="System.Type"/>
<property name="TargetMethod" value="GetType"/>
<property name="Arguments">
<list>
<value>Spring.Examples.MovieFinder.SimpleMovieFinder, MovieFinder</value>
</list>
</property>


Appliction:

IApplicationContext ctx = ContextRegistry.GetContext();
System.Type myType = ctx.GetObject("myObject") as System.Type;


Documentation (http://www.springframework.net/doc/reference/html/objects.html#objects-methodfactory)

Drawer
08-28-2006, 08:50 AM
Thanks, Ben! My respects!

But isn't a bug in Spring?!

.ben
08-28-2006, 10:38 AM
It's the only way to pass arguments to a method being invoked by Spring, constructor-args are only used when you call the constructor of an object explicitly.

If the factorymethod took no arguments, it would work but since you have to pass a string in your case the only way to make it work is like in the example I supplied.

Bruno Baia
09-01-2006, 01:40 PM
Hi,

There is a issue (http://opensource.atlassian.com/projects/spring/browse/SPRNET-358) related to object creation via factory methods reported by another topic (http://forum.springframework.net/showthread.php?p=3104) in the forum.
It has been resolved, so now you can do something like that :

<object id="MyType" type="System.Type" factory-method="GetType">
<constructor-arg name="typeName" value="SomeClass, SomeAssembly" />
</object>



It's the only way to pass arguments to a method being invoked by Spring, constructor-args are only used when you call the constructor of an object explicitly.

If the factorymethod took no arguments, it would work but since you have to pass a string in your case the only way to make it work is like in the example I supplied.
I know it's not really intuitive but constructor-args are also used to pass arguments to the factory method.


-Bruno