Delegates and lambda expressions using Spring.Net
Asked Answered
J

1

6

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.

Jacquijacquie answered 29/8, 2012 at 14:6 Comment(2)
What's your use-case for wanting to do this? Surely there are better ways?Epigraphy
Have you looked at Rosyln CTP for automatic code generation? - pretty cool stuff, although obviously may never be released - msdn.microsoft.com/en-us/vstudio/roslyn.aspxEpigraphy
P
1

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.

Paleoclimatology answered 18/4, 2013 at 2:6 Comment(1)
Thank's for trying to help... but as I wrote: "create factories without coding" using Spring.net. I'm already familar with the constructs you mentioned. But this doesn't really fit to the question. It is about a simple dynamic object with a factory pattern.Jacquijacquie

© 2022 - 2024 — McMap. All rights reserved.