Delegates and lambda expressions using Spring.Net

Is it possible to create a delegate dynamically, like

arg => { return something; } or arg => someting; 

using the built-in DelegateFactoryObject file and Spring expressions supplied with Spring.Net?

I want to create factories without coding . An abstract sample in the Spring documentation requires an abstract factory and dynamically implements the factory method. I want to define the delegate and result through Spring.Net.

I already use constructs like the following.

 <object type="Spring.Objects.Factory.Config.DelegateFactoryObject"> <property name="DelegateType" value="System.Func&lt;string,bool&gt;" /> <property name="TargetObject" value="aString" /> <property name="MethodName" value="Equals" /> </object> <object type="Spring.Objects.Factory.Config.DelegateFactoryObject"> <property name="DelegateType" value="System.Func&lt;string,My.Interface&gt;" /> <property name="TargetObject"> <object id="result" type="My.DelegateContainer&lt;string,My.Interface&gt;"> <constructor-arg name="objectToReturn" ref="theObjectToReturn" /> </object> </property> <property name="MethodName" value="Evaluate" /> </object> 

(a string is entered and the implementation type My.Interface is displayed, an ObjectToReturn object is passed)

... but I cannot find a solution how to use expressions to define a function that returns an object via xml-config. I want to replace the DelegateContainer in this example with a simple factory configuration that returns an ObjectToReturn object.

This question is related to this question: How to introduce Predicate and Func in Spring.net , and you can find additional information about the problem there.

+6
source share
1 answer

I believe that you are trying to invoke a method asynchronously using a delegate, if so, you can use any of these methods to call functions using Async calls as a delegate:

  Action act = () => { //Function Body }; //call act(); Action<Action> act2 = (x) => { //Function Body }; //call act2(() => { }); var tsk = Task.Factory.StartNew(() => { //Function Body }); //call tsk.Wait(); Parallel.Invoke(() => { //Function Body }, () => { //Function Body }); 

finally, you can use Func <> as follows:

 Func<string, int, string> sayHello = delegate(string name, int age) { return string.Format("my name is {0} and I'm {1} years old.", name, age.ToString()); }; 

Hope this help is best.

+1
source

Source: https://habr.com/ru/post/924082/


All Articles