Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delegates and lambda expressions using Spring.Net

Is it possible to create a delegate dynamically like

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

using the builtin DelegateFactoryObject and the Spring Expressions delivered with Spring.Net?

I want to create factories without coding. The abstract sample in the spring documentation requires an abstract factory and implements the factory method by config dynamically. I want to define a delegate and the result via 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>

(string is input and My.Interface implementing type is output, theObjectToReturn is passed through)

... but I am not able to 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 by a simple config-defined factory returning theObjectToReturn.

This question is related to this question: How to inject Predicate and Func in Spring.net and you can find more informations about the problem there.

like image 994
Beachwalker Avatar asked Aug 29 '12 14:08

Beachwalker


1 Answers

I believe you're trying to call Asynchronously a method using a delegate, if so, you can use any of these ways 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 following:

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, best.

like image 98
mahmoud Avatar answered Sep 21 '22 17:09

mahmoud