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<string,bool>" />
  <property name="TargetObject" value="aString" />
  <property name="MethodName" value="Equals" />
</object>
<object type="Spring.Objects.Factory.Config.DelegateFactoryObject">
  <property name="DelegateType" value="System.Func<string,My.Interface>" />
  <property name="TargetObject">
    <object id="result" type="My.DelegateContainer<string,My.Interface>">
      <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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With